[PHP-DB] Re: [PHP] goto - My comments

2010-12-21 Thread Jim Lucas

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
centerSite: 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 //inputbr /br /
Maleinput type=radio name=Sex value = 0/input
Femaleinput type=radio name=Sex value = 1/inputbr /br /br /
input type=submit /br /br /
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 Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP-DB] Re: [PHP] goto - My comments

2010-12-18 Thread 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 Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP-DB] Re: [PHP] goto - My comments

2010-12-18 Thread Thomas Anderson
On Sat, Dec 18, 2010 at 11:44 PM, Robert Cummings rob...@interjinn.com wrote:
 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.

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']) != '':
$sql1 = $sql1 . site = '$ste';
break;
case isset($_POST['MedRec']) trim($_POST['MedRe']) != '':
$sql1 = $sql1 . MedRec = '$req';
break;
// ...
default:
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;
}
}

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