Word Activity Application
am 02.01.2011 23:54:49 von ron.piggott
------=_NextPart_000_0031_01CBAAA6.265BE200
Content-Type: text/plain;
charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
I am working on a word activity --- matching words and their =
definitions. =20
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. =20
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`=20
FROM `Bible_dictionary`=20
WHERE `live` =3D1
ORDER BY RAND( )=20
LIMIT 5=20
Ron
The Verse of the Day
â=9CEncouragement from Godâ=99s Wordâ=9D
http://www.TheVerseOfTheDay.info
------=_NextPart_000_0031_01CBAAA6.265BE200--
Re: Word Activity Application
am 03.01.2011 02:56:58 von ron.piggott
------=_NextPart_000_0046_01CBAABF.98832C80
Content-Type: text/plain;
charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
The FOREACH below is giving me the error:
Invalid argument supplied for foreach()
Does anyone understand what I have done to cause this error?
#query for words
$query =3D "
SELECT `reference` , `word` , `explanation`=20
FROM `Bible_dictionary`=20
WHERE `live` =3D1
ORDER BY RAND( )=20
LIMIT 5
";
$words_match_up_result=3Dmysql_query($query);
$records_found=3Dmysql_numrows($words_match_up_result);
echo $records_found . "
"; # output is 5
#create array from mySQL query
$words =3D array();
$explanations =3D array();
$i=3D1;
while ( $i <=3D $records_found ) {
$words[$i] =3D stripslashes( mysql_result($words_match_up_result,($i =
-1),"word") );
$explanations[$i] =3D stripslashes( =
mysql_result($words_match_up_result,($i -1),"explanation") );
++$i;
}
#shuffle arrays
$match_words =3D shuffle ( $words );
$match_explanations =3D shuffle ( $explanations );
#display words on the screen
foreach($match_words as $word) {
echo $word . "
\r\n";
}
The Verse of the Day
â=9CEncouragement from Godâ=99s Wordâ=9D
http://www.TheVerseOfTheDay.info
From: Ron Piggott=20
Sent: Sunday, January 02, 2011 5:54 PM
To: php-db@lists.php.net=20
Subject: Word Activity Application
I am working on a word activity --- matching words and their =
definitions. =20
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. =20
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`=20
FROM `Bible_dictionary`=20
WHERE `live` =3D1
ORDER BY RAND( )=20
LIMIT 5=20
Ron
The Verse of the Day
â=9CEncouragement from Godâ=99s Wordâ=9D
http://www.TheVerseOfTheDay.info
------=_NextPart_000_0046_01CBAABF.98832C80--
Re: Re: Word Activity Application
am 03.01.2011 15:10:38 von Niel Archer
>=20
> 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?
>=20
> #query for words
>=20
> $query =3D "
> SELECT `reference` , `word` , `explanation`=20
> FROM `Bible_dictionary`=20
> WHERE `live` =3D1
> ORDER BY RAND( )=20
> LIMIT 5
> ";
> $words_match_up_result=3Dmysql_query($query);
> $records_found=3Dmysql_numrows($words_match_up_result);
>=20
> echo $records_found . "
"; # output is 5
>=20
> #create array from mySQL query
>=20
> $words =3D array();
> $explanations =3D array();
>=20
> $i=3D1;
> while ( $i <=3D $records_found ) {
>=20
> $words[$i] =3D stripslashes( mysql_result($words_match_up_result,($i =
-1),"word") );
> $explanations[$i] =3D stripslashes( mysql_result($words_match_up_resu=
lt,($i -1),"explanation") );
>=20
> ++$i;
> }
>=20
> #shuffle arrays
>=20
> $match_words =3D shuffle ( $words );
> $match_explanations =3D shuffle ( $explanations );
change to:
$match_words =3D $words;
shuffle($match_words);
$match_explanations =3D $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
>=20
> foreach($match_words as $word) {
>=20
> echo $word . "
\r\n";
>=20
> }
>=20
> The Verse of the Day
> =93Encouragement from God=92s Word=94
> http://www.TheVerseOfTheDay.info
>=20
>=20
> From: Ron Piggott=20
> Sent: Sunday, January 02, 2011 5:54 PM
> To: php-db@lists.php.net=20
> Subject: Word Activity Application
>=20
>=20
> I am working on a word activity --- matching words and their definitions.=
=20
>=20
> 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 o=
rder than the words so the user submits their answer. =20
>=20
> 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 row=
s? This is the query gives me the 5 results
>=20
> SELECT `reference` , `word` , `explanation`=20
> FROM `Bible_dictionary`=20
> WHERE `live` =3D1
> ORDER BY RAND( )=20
> LIMIT 5=20
>=20
> Ron
>=20
> The Verse of the Day
> =93Encouragement from God=92s Word=94
> 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