goto - My comments

From: Date: Sun, 19 Dec 2010 05:17:47 +0000
Subject: goto - My comments
Groups: php.db php.general 
Request: Send a blank email to php-db+get-47559@lists.php.net to get a copy of this message
Dear 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: <html> <head> <title>Data Search</title> <center><h4></b>Data Search</h3></center> </head> <body> <form action="srchrhsptl2.php" method="post"> <center>Site: <input type="text" name="Site" value="AA" /> Record Number: <input type="text" name="MedRec" /> First Name: <input type="text" name="Fname" /> Last Name: <input type="text" name="Lname" /><br /><br /> Phone: <input type="text" name="Phone" /> Height: <input type="decimal" name="Height" /></input><br /><br /> Male<input type="radio" name="Sex" value = "0"></input> Female<input type="radio" name="Sex" value = "1"></input><br /><br /><br /> <input type="submit" /><br /><br /> <input type="reset" value = "Clear Form" /></center> </form> </body> </html> ******************* This is the program - <html><body> <title>Search of Data</title> <pre> <?php 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("<br />"); //printf($sql1); $result = mysqli_query($cxn, $sql1); if(($num = mysqli_num_rows($result)) == 0)
        die ("No Records Retrieved");
?> <center><b>Search Results</b></center><br /> <center><table border="4" cellpadding="5" cellspacing="55" rules="all" frame="box"> <tr class=\"heading\"> <th>Site</th> <th>Medical Record</th> <th>First Name</th> <th>Last Name</th> <th>Phone</td> <th>Height</td> <th>Sex</td> <th>History</td> </tr> <?php
//      printf("<br />To exit, click the EXIT button below.<br /><br />");
//      printf("%s\t%s\t%s\t%s\t%s<br />","Site","Record", "Weight", "Height", "BMI");
        while($row = mysqli_fetch_array($result))
        {
        echo "<tr>\n";
        echo "<td> $row[0] </td>\n";
        echo "<td> $row[1] </td>\n";
        echo "<td> $row[3] </td>\n";
        echo "<td> $row[4] </td>\n";
        echo "<td> $row[2] </td>\n";
        echo "<td> $row[5] </td>\n";
        echo "<td> $row[6] </td>\n";
        echo "<td> $row[7] </td>\n";
        echo "</tr>\n";
/*              $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<br />", $site2, $record2, $wgt2, $hgt2, $bmi2);*/
        }
/*printf("%s\t%s\t%s\t%s\t%s<br />","Site","Record", "BMI", "Weight", "Height"); printf("<br />"); 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<br />", $site2, $record2, $bmi2, $wgt2, $hgt2);
}*/ mysqli_free_result($result); mysqli_close($cxn); ?> </pre></body></html> ============== THANK YOU EVER SO MUCH FOR YOUR HELP. Ethan

« previous php.db (#47559) next »