View Full Version : Random for displaying rows from database
bahytq
05-10-2003, 09:35 AM
I did query and get all of the rows in my table test . Each row in my database include fields like this : id , ask , a , b , c , d , answer . All are 7 fields . When display on a page i want it to be displayed in inorder , in random the with the field ID and in the row the 4 fields a,b,c,d are all displayed in an inorder too . Telling this is so hard to imaging so i am trying to put an example here . This is a projest of mine , doing the work of a test with a question and 4 choies for ppl to choose the answer .
Display1 :
1. Question A
a. Choice a
b. Choice b
c. Choice c
d. Choice d
2 Question B
a. Choice d
b. Choice a
c. Choice b
d. Choice c
..................
Display 2
1. Question B
a. Choice d
b. Choice a
c. Choice c
d. Choice b
2 Question A
a. Choice b
b. Choice c
c. Choice a
d.Choice d
......................................
This mean that when a user visit this page , they can see this order , but another one visit this page it will change into another oder with the same question thy can see the oder of the questions are not the same .
Have anyone has gotten over this before please tell me what to do . Maybe now i need your algorithsm
firepages
05-10-2003, 01:40 PM
ORDER by RAND()
or similar in your SQL query should help , check out the manual at MySQL
Spookster
05-10-2003, 01:59 PM
This probably belongs in the MySQL forum as per our sticky thread at the top of this forum...
Moving it....
bahytq
05-10-2003, 04:34 PM
Did u all misunderstand me . With the mySQL i did all , i mean that i did the work of getting the rows ( 10 rows ) from the table and pass them into array . Now all i need help from is the way to display it on the page . But it will be different each time ppl visit that page so there must be a function to make the array different each time ppl visit the page . All i need so bad now is the algorithsm .
Please help , this is very urgent .
firepages
05-11-2003, 02:54 AM
.... and if you select in a random order from the database then that will happen automatically ?
perhaps show some code would help us to understand you ... your subject title suggests the supplied answer
bahytq
05-11-2003, 08:03 AM
To tell you the truth that i did not know how to code for this algorithsm yet so i don't have that function now , but the work of getting rows from database i had done , and i did put all the rows into a table . Here is the code of it :
Function List_all_question($testid)
{
$db=mysql_connect($host,"admin","password");
mysql_select_db("test",$db);
$re = mysql_query("SELECT * FROM questions WHERE testid='$testid' ");
$row = "<table BORDER=1 CELLSPACING=2 CELLPADDING=2 WIDTH=610>";
while($kq = mysql_fetch_array($re))
{
$dem++;
$row .= "<tr>"
."<td VALIGN=top BGCOLOR=#770000 width=25 align=center>"
."<font color=#FC6703><b>$dem</b></font>"
."</td></font>"
."<td VALIGN=TOP width=300>
<div align=left><p><font COLOR=#ff0000></font>
<font face=Arial>{$kq["ask"]} <br><br>"
."</font></div></td>"
."<td VALIGN=TOP width=261><div align=left><p><font face=Arial>"
."<strong>A.</strong> <input NAME='pick[$dem][]' TYPE=radio VALUE='{$kq["a"]}'>{$kq["a"]} <br>"
."<strong>B.</strong> <input NAME='pick[$dem][]' TYPE=RADIO VALUE='{$kq["b"]}'>{$kq["b"]}<br>"
."<strong>C.</strong> <input NAME='pick[$dem][]' TYPE=RADIO VALUE='{$kq["c"]}'>{$kq["c"]}<br>"
."<strong>D.</strong> <input NAME='pick[$dem][]' TYPE=RADIO VALUE='{$kq["d"]}'>{$kq["d"]}<br>"
."<input type = hidden value='{$kq["answer"]}' name=answer><br>"
."<font FACE=ARIAL COLOR=#ff0000>"
."</font>"
."</tr>";
}
return $row."</table>";
I want to list the questions , and then when the visitors visit this page ( even they open this page twice at the same time ) the order of the question will be difference . This is a work of randoming the rows getting from database . I think we can query randomly or we pass the rows to array and then we randomize the array before send it to the page are both right to do this . but i really don't know the algorithm to do it ( for both way ) .
I am new in PHP and this project is the first project i do in PHP .
firepages
05-11-2003, 11:57 AM
if you have the choice of doing something in PHP or in MySQL then 9/10 the MySQL way will be much faster, so in this case, whilst you can randomise the returned array with PHP the best way perhaps is just to do...
$re = mysql_query("SELECT * FROM questions WHERE testid='$testid' ORDER BY RAND()");
PHP would give you a 'better' random order but the above should be fine for your purposes ?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.