home
Home button Search button Dr. Hain buttonClinic website Information for Dizzy Patients Fun Opinions (rants/raves)

Dr. Hain's Rants/Raves has helpful information about things that Dr. Hain has discovered by trial and error.

PHP

We are fond of using PHP for simple little (ie. 1000 lines or less) projects. In fact, we have implemented a medical record system in PHP/mysql. However, it does have some major problems.

PHP scripts -- here are a collection of generally useful little routines I wrote to do stuff in PHP.

1. Recreating the command line arguments to a php page:

Sometimes you want to just call the page you are already on again, passing something new through the POST variables. There must be an easier way to do this, probably some obscure PHP $_SERVER variable, but this is how I managed. The first segment goes at the top:

// This piece of PHP goes at the top, to construct a "cloned" command line when one wants to have the same PHP program do something different after a user has clicked on a FORM submit button.

$currentPage = $_SERVER["PHP_SELF"];
$acount=0;
foreach ($_GET as $key => $value) {

if($acount++==0) $currentPage.='?';
else $currentPage.='&';
$currentPage=$currentPage.$key.'='.$value;
}
echo $currentPage;

When one then uses a form, later on, you do it like this:

<form id="form1" name="form1" method="POST" action="<?php echo $currentPage; ?>">, it will send the $_GET array arguments.

2. Stripping out numbers from the beginning of lines, as in 1. This is a line with a number in it.

MYSQL does not have a simple method of identifying lines that begin with numbers. The MYSQL regular expression code simply does not work, because it does not recognize periods. This is the workaround:

This code finds all lines in a field called "DIAGNOSIS" that begin with numbers. We start with this to reduce the work for PHP.

$query_DX = "SELECT * FROM `dx` WHERE DIAGNOSIS REGEXP '^[0-9]+'";
$DX = mysql_query($query_DX, $Mysql)or die(mysql_error());

This code goes through each record, and uses a working regular expression syntax in PHP to strip out numbers.

$row_DX = mysql_fetch_assoc($DX);

do {
$original = $replace = $row_DX['Diagnosis'];
// at start of line, match 1 or more digits, followed by period, followed by one or more white spaces, and strip out. (the regexp does not work in MYSQL)
$replaced = preg_replace('/^[0-9]+\.\s+/', "", $row_DX['Diagnosis'], 1, $count);

if(!$count) continue; // no matches

$u= sprintf("UPDATE dx set Diagnosis=%s, Comment2=%s where ID=%s", "'".$replaced."'","'".$original."'", $row_DX['ID']);
mysql_query($u, $Mysql)or die(mysql_error());
}

© Copyright June 13, 2015 , Timothy C. Hain, M.D. All rights reserved.
Dr Hain's CV Clinic dizziness-and-hearing.com FLW Rant-Rave