goto - My comments

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" />


Phone: <input type="text" name="Phone" />
Height: <input type="decimal" name="Height" /></input>


Male<input type="radio" name="Sex" value = "0"></input>
Female<input type="radio" name="Sex" value = "1"></input>



<input type="submit" />


<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("
");
//printf($sql1);
$result = mysqli_query($cxn, $sql1);

if(($num = mysqli_num_rows($result)) == 0)
die ("No Records Retrieved");
?>
<center><b>Search Results</b></center>


<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("
To exit, click the EXIT button below.
<br
/>");
// printf("%s\t%s\t%s\t%s\t%s
","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
", $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);

?>

</pre></body></html>

==============
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
Ethan Rosenberg [ So, 19 Dezember 2010 06:17 ] [ ID #2051844 ]

Re: goto - My comments

On 10-12-19 12:17 AM, Ethan Rosenberg wrote:
> Dear List -
>
> Thanks to all for your EXCELLENT comments. I definitly agree that
> goto is a command to be avoided at all costs.

Closed-minded drivel (or you're buttering up the popular opinion crowd).
A better approach is that goto should be used with caution.

As for doing your homework for you... ummm no thanks. You should take
the time to do the exercise so you gain the benefit of experience.

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Cummings [ So, 19 Dezember 2010 06:44 ] [ ID #2051845 ]

Re: [PHP] goto - My comments

On Sat, Dec 18, 2010 at 11:44 PM, Robert Cummings <robert [at] interjinn.com> wr=
ote:
> On 10-12-19 12:17 AM, Ethan Rosenberg wrote:
>>
>> Dear List -
>>
>> Thanks to all for your EXCELLENT comments. =A0I definitly agree that
>> goto is a command to be avoided at all costs.
>
> Closed-minded drivel (or you're buttering up the popular opinion crowd). =
A
> better approach is that goto should be used with caution.
>
> As for doing your homework for you... ummm no thanks. You should take the
> time to do the exercise so you gain the benefit of experience.

I would have thought school would have been out on account of Christmas and=
all.

In any event, here's my rewrite:

switch (true)
{
case isset($_POST['Site']) && trim($_POST['Site']) !=3D '':
$sql1 =3D $sql1 . "site =3D '$ste'";
break;
case isset($_POST['MedRec'])&& trim($_POST['MedRe']) !=3D '':
$sql1 =3D $sql1 . "MedRec =3D '$req'";
break;
// ...
default:
if(isset($_Request['Sex'])&& trim($_POST['Sex']) !=3D '' )
{
if ($_REQUEST["Sex"] =3D=3D "0")
$sex =3D 'Male';
else
$sex =3D 'Female';

$sql1 =3D $sql1 . " =3D '$sex'";
$sexdone =3D 1;
}

if(isset($_POST['Hx'])&& trim($_POST['Hx']) !=3D '')
{
$sql1 =3D $sql1 . "Hx =3D '$hx'";
$done =3D 1;
}
}

You could also do an if / else if / else if / ... / else.

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
zelnaga [ So, 19 Dezember 2010 07:55 ] [ ID #2051846 ]

Re: goto - My comments

On 12/18/2010 9:17 PM, Ethan Rosenberg wrote:
> 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:
>
> <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" />


> Phone: <input type="text" name="Phone" />
> Height: <input type="decimal" name="Height" /></input>


> Male<input type="radio" name="Sex" value = "0"></input>
> Female<input type="radio" name="Sex" value = "1"></input>



> <input type="submit" />


> <input type="reset" value = "Clear Form" /></center>
> </form>
>

Not sure if you can change the values for the Sex field to 'Male' &
'Female' respectively, but it would simplify the following example.


Here is my rendition of how I would do it.

<?php

....

$query = "select * from Intake3 where 1 ";

$allowed_fields = array('Site', 'MedRe', 'Fname', 'Lname',
'Phone', 'Sex', 'Height');

# deal with the special case first
# Normally you do not want to modify the _POST/_GET/_REQUEST array, but
# in this case, it is used as an quick example of how to get the data
# passed along. if you can change the field values to Male/Female you
# could remove the following section and have just the foreach() loop.
if ( ! empty($_POST['Sex']) )
{
if ( $_POST['Sex'] === '1' )
$_POST['Sex'] = 'Female';
else
$_POST['Sex'] = 'Male';
}

# Now deal with the rest...
foreach ( $allowed_fields AS $field )
{
if ( ! empty( $_POST[$field] ) )
{
$value = mysql_real_escape_string( $_POST[$field] );
$query .= " AND `{$field}` = '{$value}' ";
}
}

in the end, you will end up with a nicely formatted SQL query to execute.

I would suggest cleaning up the output code some and use *_assoc()
instead of the *_array() function call. It gives you back the array
version of the output. This way instead of calling $row[0], $row[...]
you would call $row['Fname'] or $row['Lname'] instead.

Get rid of all those commented out sections and you will have a good
script to play with.

Let us know what comes of it...

>
> ==============
> 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
List Manager [ Mi, 22 Dezember 2010 07:33 ] [ ID #2051994 ]
PHP » gmane.comp.php.database » goto - My comments

Vorheriges Thema: Bien aime bonjour
Nächstes Thema: Problems w/ goto