Word Matching Application
| From: | Ron Piggott | Date: | Thu, 06 Jan 2011 02:23:57 +0000 |
| Subject: | Word Matching Application | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-47601@lists.php.net to get a copy of this message | ||
I am working on a word matching application. Specifically the user will match a word with it’s
definition. I have made some progress since my last post for help (2 or 3 days ago).
I need help knowing how to alternate between displaying the word and it’s explanation:
===
echo "<p>\r\n";
echo "<span class=\"bible_match_up_left\">\r\n";
echo "WORD\r\n";
echo "</span>\r\n";
echo "<span class=\"bible_match_up_right\">\r\n";
echo "EXPLANATION\r\n";
echo "</span>\r\n";
echo "</p>\r\n";
===
I only know how to do one array at a time, using FOREACH, like this:
===
echo "<ul>\r\n";
foreach($match_words as $word) {
echo "<li>" . $word . "</li>\r\n";
}
echo "</ul>\r\n";
===
How do I do both the word and explanation at once?
The following is how I query the database for the words / explanations and create and shuffle the
arrays:
===
$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);
#create array from mySQL query
$words = array();
$explanations = array();
$i=1;
while ( $i <= $records_found ) {
$reference = mysql_result($words_match_up_result,($i -1),"reference");
$words[$reference] = stripslashes( mysql_result($words_match_up_result,($i -1),"word")
);
$explanations[$reference] = stripslashes( mysql_result($words_match_up_result,($i
-1),"explanation") );
++$i;
}
#shuffle from PHP web site
function custom_shuffle($my_array = array()) {
$copy = array();
while (count($my_array)) {
// takes a rand array elements by its key
$element = array_rand($my_array);
// assign the array and its value to an another array
$copy[$element] = $my_array[$element];
//delete the element from source array
unset($my_array[$element]);
}
return $copy;
}
$match_words = custom_shuffle($words);
$match_explanations = custom_shuffle($explanations);
===
$reference is not in sequential order. $reference is the auto_increment value of the
Bible_dictionary table. It’s significance is for scoring how many the user got
right.
Ron