goto - My comments
am 19.12.2010 06:17:47 von Ethan RosenbergDear List -
Thanks to all for your EXCELLENT comments. I definitly agree that
goto is a command to be avoided at all costs. In this case, I could
not figure out how to acheive the desired result without the
goto. So.... being a newbie, I humbly request that you show [and at
the same time teach] me how to rewrite the code to eleiminate the goto.
Additionally, would you please do the same for the code I list
below. This code runs perfectly.
==============
This is the form:
Data Search
*******************
This is the program -
require '/var/www/pass.inc';
$db = "hospital2";
$cxn = mysqli_connect($host,$user,$password,$db);
$ste = $_POST['Site'];
$req = $_POST['MedRec'];
$fnm = $_POST['Fname'];
$lnm = $_POST['Lname'];
$phn = $_POST['Phone'];
$hgt = $_POST['Height'];
//$sex = $_REQUEST['Sex'];
//print_r($_POST);
$sitedone = 0;
$recdone = 0;
$fnmdone = 0;
$lnmdone = 0;
$phndone = 0;
$hgtdone = 0;
$sexdone = 0;
$sql1 = "select * from Intake3 where ";
if(isset($_POST['Site']) && trim($_POST['Site']) != '')
{
$sql1 = $sql1 . "site = '$ste'";
$sitedone = 1;
goto end;
}
if(isset($_POST['MedRec'])&& trim($_POST['MedRe']) != '')
{
$sql1 = $sql1 . "MedRec = '$req'";
$recdone = 1;
goto end;
}
if(isset($_POST['Fname'])&& trim($_POST['Fname']) != '')
{
$sql1 = $sql1 . "Fname = '$fnm";
$fnmdone = 1;
goto end;
}
if(isset($_POST['Lname'])&& trim($_POST['Lname']) != '')
{
$sql1 = $sql1 . "Lname = '$lnm'";
$lnmdone = 1;
goto end;
}
if(isset($_POST['Phone'])&& trim($_POST['Phone']) != '')
{
$sql1 = $sql1 . "Phone = '$phn'";
$phndone = 1;
}
if(isset($_Request['Sex'])&& trim($_POST['Sex']) != '' )
{
if ($_REQUEST["Sex"] == "0")
$sex = 'Male';
else
$sex = 'Female';
$sql1 = $sql1 . " = '$sex'";
$sexdone = 1;
}
if(isset($_POST['Hx'])&& trim($_POST['Hx']) != '')
{
$sql1 = $sql1 . "Hx = '$hx'";
$done = 1;
}
end:
if ($sitedone == 1)
goto recder;
if ($sitedone == 0)
{
if(isset($_POST['Site']) && trim($_POST['Site']) != '')
{
$sql1 = $sql1 . " && (Site = '$ste')";
goto recder;
}
}
recder:
if ($recdone == 1)
goto fnmer;
if ($recdone == 0)
{
if(isset($_POST['MedRec'])&& trim($_POST['MedRec']) != '')
{
$sql1 = $sql1 . " && (MedRec = '$req')";
$recdone = 1;
goto fnmer;
}
}
fnmer:
if($fnmdone == 1)
goto lnmer;
if($fnmdone == 0)
{
if(isset($_POST['Fname'])&& trim($_POST['Fname']) != '')
{
$sql1 = $sql1 . " && (Fname = '$fnm')";
$fnmdone = 1;
goto lnmer;
}
}
lnmer:
if($lnmdone == 1)
goto phner;
if($lnmdone == 0)
{
if(isset($_POST['Lname'])&& trim($_POST['Lname']) != '')
{
$sql1 = $sql1 . " && (Lname = '$lnm')";
$lnmdone = 1;
goto phner;
}
}
phner:
if($phndone == 1)
goto hgter;
if($phndone == 0)
{
if(isset($_POST['Phone'])&& trim($_POST['Phone']) != '')
{
$sql1 = $sql1 . " && (Phone = '$phn')";
$phndone = 1;
goto hgter;
}
}
hgter:
if($hgtdone == 1)
goto sexer;
if($hgtdone == 0)
{
if(isset($_POST['Height'])&& trim($_POST['Height']) != '')
{
$sql1 = $sql1 . " && (Height = '$hgt')";
$hgtdone = 1;
goto sexer;
}
}
sexer:
if($sexdone == 1)
goto ender;
if($sexdone == 0)
{
if(isset($_REQUEST['Sex'])&& trim($_REQUEST['Sex']) != '')
{
$sql1 = $sql1 . " && (sex = '$sex')";
$done = 1;
goto ender;
}
}
ender:
$sql1 = $sql1 . ";";
printf("
");
//printf($sql1);
$result = mysqli_query($cxn, $sql1);
if(($num = mysqli_num_rows($result)) == 0)
die ("No Records Retrieved");
?>
Search Results
cellspacing="55" rules="all" frame="box">
Site
Medical Record
First Name
Last Name
Phone
Height
Sex
History
// printf("
To exit, click the EXIT button below.
/>");
// printf("%s\t%s\t%s\t%s\t%s
","Site","Record", "Weight",
"Height", "BMI");
while($row = mysqli_fetch_array($result))
{
echo "\n"; \n";
echo "$row[0] \n";
echo "$row[1] \n";
echo "$row[3] \n";
echo "$row[4] \n";
echo "$row[2] \n";
echo "$row[5] \n";
echo "$row[6] \n";
echo "$row[7] \n";
echo "
/* $site2 = $row[0];
$record2 = $row[1];
$bmi2 = $row[2];
$wgt2 = $row[3];
$hgt2 = $row[4];
printf("%s\t%d\t%d\t%d\t%3.1f
", $site2,
$record2, $wgt2, $hgt2, $bmi2);*/
}
/*printf("%s\t%s\t%s\t%s\t%s
","Site","Record", "BMI", "Weight",
"Height");
printf("
");
while( $row = mysqli_fetch_array($result))
{
$site2 = $row[0];
$record2 = $row[1];
$bmi2 = $row[2];
$wgt2 = $row[3];
$hgt2 = $row[4];
printf("%s\t%d\t%3.1f\t%d\t%d
", $site2,
$record2, $bmi2, $wgt2, $hgt2);
}*/
mysqli_free_result($result);
mysqli_close($cxn);
?>
==============
THANK YOU EVER SO MUCH FOR YOUR HELP.
Ethan
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php