Re: Random question selection in PHP w/ MySQL backend.

From: Date: Wed, 01 Aug 2001 02:23:40 +0000
Subject: Re: Random question selection in PHP w/ MySQL backend.
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-10939@lists.php.net to get a copy of this message
> "Adam Lundrigan" <ceo@vpu-virtual.com> wrote in message > news:20010731203402.13490.qmail@pb1.pair.com... > What i need to do is select 10 questions from a database > (db: vpt, table: questions), and make sure that the same > question doesn't appear twice. The questions need to > appear in a random order each time the script is loaded. $mysql_connect("host", "user", "pwd"); $result = $mysql_db_query("vpt", "SELECT id, txt FROM questions " ."ORDER BY RANDOM() LIMIT 10" ); function makeQuestion($qid) { return ( "<input type='radio' name='q[$qid]' value=1>1" ."<input type='radio' name='q[$qid]' value=2>2" ."<input type='radio' name='q[$qid]' value=3>3" ."<input type='radio' name='q[$qid]' value=4>4" ."<input type='radio' name='q[$qid]' value=5>5" ); } function makeRow() { $str = "\n\t<tr>"; $num = func_num_args(); for ($i = 0; $i < $num; $i++) $str .= "\n\t\t<td>".func_get_arg($i)."</td>"; $str .= "\n\t</tr>"; return $str; } echo "\n<form>\n<table>"; while ($row = mysql_fetch_array($result)) echo makeRow(makeQuestion($row[id]), $row[txt]); echo makeRow("<input type='reset'>", "<input type='submit'>"); echo "\n</table>\n</form>"; Voila - they get ten questions at random, in random order; when they submit the form, you get the question id back as the array key and the answer as the value. Hope that helps ;-)

« previous php.db (#10939) next »