goto - My comments

goto - My comments

am 19.12.2010 06:17:47 von Ethan Rosenberg

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:



Data Search

Data Search







Site:
Record Number:
First Name:
Last Name:


Phone:
Height:


Male
Female












*******************
This is the program -


Search of Data


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







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

echo "\n";
echo "\n";
echo "\n";
echo "\n";
echo "\n";
echo "\n";
echo "\n";
echo "\n";
echo "\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);

?>



==============
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

Re: goto - My comments

am 19.12.2010 06:44:56 von Robert Cummings

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

Re: [PHP] goto - My comments

am 19.12.2010 07:55:25 von zelnaga

On Sat, Dec 18, 2010 at 11:44 PM, Robert Cummings 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"] == "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

Re: goto - My comments

am 22.12.2010 07:33:17 von List Manager

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:
>
>

>

Site:
> Record Number:
> First Name:
> Last Name:


> Phone:
> Height:


> Male
> Female



>


>

>
>

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.


....

$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

Site Medical Record First Name Last Name Phone
Height
Sex
History
$row[0] $row[1] $row[3] $row[4] $row[2] $row[5] $row[6] $row[7]