Re: [PHP] goto - My comments
Would you please look at the code you wrote again. I must have botched it, because both the age and kitten form still are on the same page. The age page should appear, the data should be accepted and then the kitten page should appear. Ethan Hi Ethan, I don't mean to get in between you and Jim (or anyone else here). You want your page to just work.. and Jim offered you a working solution.. nice! But my suggestion is to drop that task for a day.. *totally start over* .. SUPER simple.. See if you can make a *tiny* form appear on a page and then when submitted show the next form.. etc.. just as you need the flow to go. Learn the concepts and logic structures involved to make that happen. If you focus on just this one thing then you will get it without too much more trouble.. and then you will OWN it and be able to apply it to your real production task. If you get stuck while writing your SIMPLE strung-together test forms, then ask about where you are stuck; what is it exactly that you do not understand. Just saying, effectively, It does not work shows you are over your head and need to back up and break down the problem into smaller parts. Don't worry, the smaller parts will prove to be easy, once actually seen for what they are! My PHP familiarity is not even 20% of what Jim and others here have, but I can and certainly will help you reach working code, so I guarantee you'll have help, if you start from the beginning. I'll be here all day today, and back on monday... to answer your posts. Govinda -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Fwd: Fwd: Re: [PHP] goto - My comments
At 02:38 PM 12/27/2010, Jim Lucas wrote: On 12/27/2010 10:42 AM, Ethan Rosenberg wrote: snip Now, here is the real puzzler The purpose of this routine is to be able to have two(2) forms on one page,but not simultaneously.Additionally, l do not wish to call a separate program every time a new form is used. The assumption is that the second form depends on the entries in the first form. I realize this is not the case here. The age request and the kitten form both appear on the page together. How do I accomplish having them appear separately? If it requires Java Script or jQuery, what is the code to be used? snip The key is to look at the value of the submit button. This needs to be unique. Change around your logic a little and you will have it. ?php // if form not yet submitted // display form if ( isset($_POST['submit']) $_POST['submit'] === 'Submit' ) { // process form input // split date value into components $dateArr = explode('/', $_POST['dob']); // calculate timestamp corresponding to date value $dateTs = strtotime($_POST['dob']); // calculate timestamp corresponding to 'today' $now = strtotime('today'); // check that the value entered is in the correct format if ( sizeof($dateArr) != 3 ) { die('ERROR: Please enter a valid date of birth'); } // check that the value entered is a valid date if ( !checkdate($dateArr[0], $dateArr[1], $dateArr[2]) ) { die('ERROR: Please enter a valid date of birth'); } // check that the date entered is earlier than 'today' if ( $dateTs = $now ) { die('ERROR: Please enter a date of birth earlier than today'); } // calculate difference between date of birth and today in days // convert to years // convert remaining days to months // print output $ageDays = floor(($now - $dateTs) / 86400); $ageYears = floor($ageDays / 365); $ageMonths = floor(($ageDays - ($ageYears * 365)) / 30); echo You are approximately $ageYears years and $ageMonths months old.; } else if ( isset($_POST['submit']) $_POST['submit'] === 'Submit Kitten' ) { $name_cat = $_POST['cat']; echo Your Kitten is $name_cat; } else { echo HTML form method=post action=agecalc3.php Enter your date of birth, in mm/dd/ format: br / input type=text name=dob / input type=submit name=submit value=Submit / /form br /br / form method=post action=agecalc3.php Enter your kitten's name: br / input type=text name=cat / input type=submit name=submit value=Submit Kitten / /form HTML; } ? Jim Lucas Jim - Thanks. Would you please look at the code you wrote again. I must have botched it, because both the age and kitten form still are on the same page. The age page should appear, the data should be accepted and then the kitten page should appear. Ethan PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Fwd: Fwd: Re: [PHP] goto - My comments
On 12/27/2010 10:42 AM, Ethan Rosenberg wrote: Jim - Thank you ever so much. At 01:58 PM 12/24/2010, you wrote: Here you are using two different arrays. Yes, I know, they are basically the same, but they are truly not the same. In your case, use $_POST This is what I used. As per your suggestion, I changed the == to ===. if(isset($_Request['Sex']) trim($_POST['Sex']) != '' ) { if ($_REQUEST['Sex'] == 0) { $sex = 'Male'; } else { $sex = 'Female'; } } This defaults to Male. I do not always search the Sex field. if ( empty($_POST['Sex']) ) { $_POST['Sex'] = 'Male'; } else { $_POST['Sex'] = 'Female'; } +++ Now, here is the real puzzler The purpose of this routine is to be able to have two(2) forms on one page,but not simultaneously.Additionally, l do not wish to call a separate program every time a new form is used. The assumption is that the second form depends on the entries in the first form. I realize this is not the case here. The age request and the kitten form both appear on the page together. How do I accomplish having them appear separately? If it requires Java Script or jQuery, what is the code to be used? Here is the code == !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN DTD/xhtml1-transitional.dtd html xmlns=http://www.w3.org/1999/xhtml; xml:lang=en lang=en head titleProject 4-5: Age Calculator/title /head body h2Project 4-5: Age Calculator/h2 ?php // if form not yet submitted // display form if (!isset($_POST['dob'])) { echo form method=\post\ action=\agecalc3.php\; echo Enter your date of birth, in mm/dd/ format: br /; echo input type=\text\ name=\dob\ /; echo p; echo input type=\submit\ name=\submit\ value=\Submit\ /; echo /form; } else { // if form submitted // process form input // split date value into components $dateArr = explode('/', $_POST['dob']); // calculate timestamp corresponding to date value $dateTs = strtotime($_POST['dob']); // calculate timestamp corresponding to 'today' $now = strtotime('today'); // check that the value entered is in the correct format if (sizeof($dateArr) != 3) { die('ERROR: Please enter a valid date of birth'); } // check that the value entered is a valid date if (!checkdate($dateArr[0], $dateArr[1], $dateArr[2])) { die('ERROR: Please enter a valid date of birth'); } // check that the date entered is earlier than 'today' if ($dateTs = $now) { die('ERROR: Please enter a date of birth earlier than today'); } // calculate difference between date of birth and today in days // convert to years // convert remaining days to months // print output $ageDays = floor(($now - $dateTs) / 86400); $ageYears = floor($ageDays / 365); $ageMonths = floor(($ageDays - ($ageYears * 365)) / 30); echo You are approximately $ageYears years and $ageMonths months old.; } // SECOND FORM if (!isset($_POST['cat'])) { echo form method=\post\ action=\agecalc3.php\; echo br /br /Enter your kitten's name: br /; echo input type=\text\ name=\cat\ /; echo p; echo input type=\submit\ name=\submit\ value=\Submit Kitten\ /; echo /form; } else { $name_cat = $_POST['cat']; echo Your Kitten is $name_cat; } ? /body /html === Thanks again. Ethan The key is to look at the value of the submit button. This needs to be unique. Change around your logic a little and you will have it. ?php // if form not yet submitted // display form if ( isset($_POST['submit']) $_POST['submit'] === 'Submit' ) { // process form input // split date value into components $dateArr = explode('/', $_POST['dob']); // calculate timestamp corresponding to date value $dateTs = strtotime($_POST['dob']); // calculate timestamp corresponding to 'today' $now = strtotime('today'); // check that the value entered is in the correct format if ( sizeof($dateArr) != 3 ) { die('ERROR: Please enter a valid date of birth'); } // check that the value entered is a valid date if ( !checkdate($dateArr[0], $dateArr[1], $dateArr[2]) ) { die('ERROR: Please enter a valid date of birth'); } // check that the date entered is earlier than 'today' if ( $dateTs = $now ) { die('ERROR: Please enter a date of birth earlier than today'); } // calculate difference between date of birth and today in days // convert to years // convert remaining days to months // print output $ageDays = floor(($now
Re: Fwd: Fwd: Re: [PHP] goto - My comments
On 12/23/2010 10:39 AM, Ethan Rosenberg, PhD wrote: Jim - Thanks ever so much! Here is the code I used, as you suggested. == $query = select * from Intake3 where ; Maybe I missed it, but you need to have a 1 after the where part in your select. So... $query = SELECT * FROM Intake3 WHERE 1 ; $allowed_fields = array('Site', 'MedRec', 'Fname', 'Lname', 'Phone', 'Sex', 'Height'); Here you are using two different arrays. Yes, I know, they are basically the same, but they are truly not the same. In your case, use $_POST if(isset($_Request['Sex']) trim($_POST['Sex']) != '' ) { if ($_REQUEST['Sex'] == 0) { $sex = 'Male'; } else { $sex = 'Female'; } } Looking again at what I sent you before, it would have given you a few errors if ran like that. Here is a better version of it. For the above, you can change the logic and get rid of the isset and trim. Also, make sure when you do a comparison against 0 that it is type strict using === and not ==. I could pass FALSE or NULL as the value and it would get by your test. If you look at the definition of empty() it tells you that if empty is passed: , 0, 0, NULL, FALSE, array(), or var $var; in a class that the return of empty will be false. So, this tells me that I can replace the multiple if/else statements above with a single as below. if ( empty($_POST['Sex']) ) { $_POST['Sex'] = 'Male'; } else { $_POST['Sex'] = 'Female'; } The above does the same but without the extra work involved. Once you have that, the following will work fine. foreach ( $allowed_fields AS $field ) { if ( ! empty( $_POST[$field] ) ) { $value = mysql_real_escape_string( $_POST[$field] ); $query .= AND `{$field}` = '{$value}' ; } } printf($query); This is the result I get for the query: select * from Intake3 where AND `Site` = 'AA' I can't figure out what is happening. Would you please help. Thanks again. Ethan +++ Date: Tue, 21 Dec 2010 22:33:17 -0800 From: Jim Lucasli...@cmsws.com To: Ethan Rosenbergeth...@earthlink.net CC: php-db-lists.php.netphp...@lists.php.net, php-general@lists.php.net Subject: Re: [PHP] 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 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 General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] 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 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 General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] 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 titleData Search/title centerh4/bData Search/h3/center /head body 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 /body /html *** This is the program - htmlbody titleSearch 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); ? centerbSearch Results/b/centerbr / centertable border=4 cellpadding=5 cellspacing=55 rules=all frame=box tr class=\heading\ thSite/th thMedical Record/th thFirst Name/th thLast Name/th thPhone/td thHeight/td thSex/td thHistory/td /tr ?php // printf(br /To exit, click the EXIT button below.br /br /); // printf(%s\t%s\t%s\t%s\t%sbr /,Site,Record, Weight, Height, BMI); while($row = mysqli_fetch_array($result)) {
Re: [PHP] 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
Re: [PHP] goto - My comments
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 General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php