Re: [PHP-DB] Re: Word Activity Application
The FOREACH below is giving me the error: Invalid argument supplied for foreach() Not surprising as the $match_words will contain a boolean result from shuffle. Shuffle works directly on the variable it is given and returns true or false, depending on success. rtfm ;-) Does anyone understand what I have done to cause this error? #query for words $query = SELECT `reference` , `word` , `explanation` FROM `Bible_dictionary` WHERE `live` =1 ORDER BY RAND( ) LIMIT 5 ; $words_match_up_result=mysql_query($query); $records_found=mysql_numrows($words_match_up_result); echo $records_found . br; # output is 5 #create array from mySQL query $words = array(); $explanations = array(); $i=1; while ( $i = $records_found ) { $words[$i] = stripslashes( mysql_result($words_match_up_result,($i -1),word) ); $explanations[$i] = stripslashes( mysql_result($words_match_up_result,($i -1),explanation) ); ++$i; } #shuffle arrays $match_words = shuffle ( $words ); $match_explanations = shuffle ( $explanations ); change to: $match_words = $words; shuffle($match_words); $match_explanations = $explanations; shuffle($match_explanations); However if these need to stay in sync this will not work. i.e if a explanation's index needs to match the corresponding word's index, then you'll have to do this another way. But as I understand your need here, this isn't a problem? #display words on the screen foreach($match_words as $word) { echo $word . br /\r\n; } The Verse of the Day Encouragement from Gods Word http://www.TheVerseOfTheDay.info From: Ron Piggott Sent: Sunday, January 02, 2011 5:54 PM To: php-db@lists.php.net Subject: Word Activity Application I am working on a word activity --- matching words and their definitions. I want to display 5 words on the left hand side and the 5 definitions on the right hand side. But I want the definitions displayed in a different order than the words so the user submits their answer. Should I use PHP to display the definitions in random order? OR Is there a way do this in mySQL that would mix and match results from different rows? This is the query gives me the 5 results SELECT `reference` , `word` , `explanation` FROM `Bible_dictionary` WHERE `live` =1 ORDER BY RAND( ) LIMIT 5 Ron The Verse of the Day Encouragement from Gods Word http://www.TheVerseOfTheDay.info -- Niel Archer niel.archer (at) blueyonder.co.uk -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-DB] Two forms on one page
Dear List - I would like to have two(2) forms in one PHP script. I would like to have the forms appear sequentially; ie, that the first form would appear, the data would be entered, and then the second form would appear, the data would be entered, and the script would exit. The code below displays both forms simultaneously. After the data is entered for the first form, the second form appears again. After the data is entered for the second form, the script displays the statement from the first form. Would you please help me correct the script so that it will perform as required. Thanks. Here is the code: !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd; !-- kitten.php Two forms in one program -- htmlbody ?php global $todo; // if form not yet submitted // display form if ( isset($_POST['cat']) $_POST['submit'] === 'Submit Ktten' ) die(); if ( isset($_POST['submit']) $_POST['submit'] === 'Submit' ) agerdo(); else { echo form method=\post\; echo Enter your date of birth, in mm/dd/ format: br /; echo input type=\text\ name=\dob\ /; echo input type=\submit\ name=\submit\ value=\Submit\ /; echo /form; } function agerdo() { global $todo; // echo $todo; // 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.; } if ( isset($_POST['submit']) $_POST['submit'] === 'Submit Kitten' ) catdo(); if ( !isset($_POST['cat'])) // $_POST['submit'] === 'Submit Kitten' ) { echo HTML form method=post onsubmit=catdo(); Enter your kitten's name: br / input type=text name=cat / input type=submit name=submit value=Submit Kitten / /form HTML; } function catdo() { $name_cat = $_POST['cat']; echo Your Kitten is $name_cat; exit(); } ? /body/html === Ethan MySQL 5.1 PHP 5 Linux [Debian (sid)] -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Two forms on one page
On 1/3/2011 10:52 PM, Ethan Rosenberg wrote: Dear List - I would like to have two(2) forms in one PHP script. I would like to have the forms appear sequentially; ie, that the first form would appear, the data would be entered, and then the second form would appear, the data would be entered, and the script would exit. The code below displays both forms simultaneously. After the data is entered for the first form, the second form appears again. After the data is entered for the second form, the script displays the statement from the first form. Would you please help me correct the script so that it will perform as required. If you're trying to do this without actually submitting the first form, then you'll have to use JavaScript to hide/show the second form. Personally, I'd use jQuery/jQuery UI to get a date picker on the first text box, then after a date is entered, display the second box. Toby -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-DB] Two forms on one page
Oooops - left out the text that was supposed to be in the quotes. Dear List - I would like to have two(2) forms in one PHP script. I would like to have the forms appear sequentially; ie, that the first form would appear, the data would be entered, and then the second form would appear, the data would be entered, and the script would exit. The code below displays both forms simultaneously. After the data is entered for the first form, the second form appears again. After the data is entered for the second form, the script displays the statement Enter your date of birth, in mm/dd/ format: from the first form. Would you please help me correct the script so that it will perform as required. Thanks. Here is the code: !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd; !-- kitten.php Two forms in one program -- htmlbody ?php global $todo; // if form not yet submitted // display form if ( isset($_POST['cat']) $_POST['submit'] === 'Submit Ktten' ) die(); if ( isset($_POST['submit']) $_POST['submit'] === 'Submit' ) agerdo(); else { echo form method=\post\; echo Enter your date of birth, in mm/dd/ format: br /; echo input type=\text\ name=\dob\ /; echo input type=\submit\ name=\submit\ value=\Submit\ /; echo /form; } function agerdo() { global $todo; // echo $todo; // 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.; } if ( isset($_POST['submit']) $_POST['submit'] === 'Submit Kitten' ) catdo(); if ( !isset($_POST['cat'])) // $_POST['submit'] === 'Submit Kitten' ) { echo HTML form method=post onsubmit=catdo(); Enter your kitten's name: br / input type=text name=cat / input type=submit name=submit value=Submit Kitten / /form HTML; } function catdo() { $name_cat = $_POST['cat']; echo Your Kitten is $name_cat; exit(); } ? /body/html === Ethan MySQL 5.1 PHP 5 Linux [Debian (sid)] -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Two forms on one page
On Jan 3, 2011, at 5:17 PM, Ethan Rosenberg wrote: 'Submit Ktten' Shouldn't this be.. 'Submit Kitten' If your asking for it to explicitly equal that (===), then you need to spell Kitten with an i in it? But, that may not be what is actually wrong, just somehting I think I caught. Best, Karl DeSaulniers Design Drumm http://designdrumm.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php