Re: Re: Word Activity Application

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> 
> 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 God?s Word?
> http://www.TheVerseOfTheDay.info
> 
> 
> From: Ron Piggott 
> Sent: Sunday, January 02, 2011 5:54 PM
> To: php-db@xxxxxxxxxxxxx 
> 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 God?s 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




[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux