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: > > 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? > > > > 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. = $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 << Enter your date of birth, in mm/dd/ format: Enter your kitten's name: 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 > == > "DTD/xhtml1-transitional.dtd"> > http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en"> > > Project 4-5: Age Calculator > > > Project 4-5: Age Calculator > // if form not yet submitted > // display form > > > if (!isset($_POST['dob'])) > { > > echo " "; > echo " Enter your date of birth, in mm/dd/ format: "; > echo " "; > echo " "; > echo " "; > echo " "; > > } > 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 " "; > echo " Enter your kitten's name: "; > echo " "; > echo " "; > echo " Kitten\" />"; > echo ""; > } > else > { > $name_cat = $_POST['cat']; > > echo "Your Kitten is $name_cat"; > } > ?> > > > > === > 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. = $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 << Enter your date of birth, in mm/dd/ format: Enter your kitten's name: HTML; } ?> Jim Lucas -- 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/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 Lucas To: Ethan Rosenberg CC: "php-db-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: 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. == 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