GFOnyX Posted July 21, 2013 Share Posted July 21, 2013 (edited) Hey guys. As you can see from my post count i am new here and also new to php. I am trying to modify a code sample of a quiz system that uses mysql database to store all the info such as users,questions,answers,score etc. I have manage to modify the things i want but i am having a hard time making the questions appear in random order when the user takes the test. The table that sores the questions and the answers is named "mst_question" and it has the following structure: que_id | test_id | que_desc | ans1 | ans2 | ans4 | ans4 | true_ans I have tried two things: 1.Modified the query by adding the order by rand() function.I read it is not the fastest way but at this stage i don't care about speed. The results returned were indeed random but the score doesnt seem to work properly and sometimes i get the same question twice. More specific this the query i tried on my effort of eliminating duplicates but as i said it is not working properly: $rs=mysql_query("SELECT DISTINCT * FROM mst_question WHERE que_id IN (SELECT DISTINCT que_id FROM mst_question where test_id=$tid )",$cn) 2. I tried soring the results into an array and use the shuffle function but that didnt work either. I am guessing the query is correct and the reason it doesnt work properly has something to do with the way the results are being displayed but as i said i am a newbie to php/mySQL. This is the modified content of the php file. Any ideas/suggestions are welcome <?php session_start(); include("database.php"); extract($_POST); extract($_GET); extract($_SESSION); if(isset($subid) && isset($testid)) { $_SESSION[sid]=$subid; $_SESSION[tid]=$testid; header("location:quiz.php"); } if(!isset($_SESSION[sid]) || !isset($_SESSION[tid])) //An den exei tethei subject kai testid tous paei sto index.php { header("location: index.php"); } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Online Quiz</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link href="quiz.css" rel="stylesheet" type="text/css"> </head> <body> <?php include("header.php"); $rs=mysql_query("SELECT DISTINCT * FROM mst_question WHERE que_id IN (SELECT DISTINCT que_id FROM mst_question where test_id=$tid ) ORDER BY rand()",$cn) or die(mysql_error()); if(!isset($_SESSION[qn])) { $_SESSION[qn]=0; mysql_query("delete from mst_useranswer where sess_id='" . session_id() ."'") or die(mysql_error()); $_SESSION[trueans]=0; } else { if($submit=='Next Question' && isset($ans)) { mysql_data_seek($rs,$_SESSION[qn]); $row= mysql_fetch_row($rs); mysql_query("insert into mst_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error()); if($ans==$row[7]) { $_SESSION[trueans]=$_SESSION[trueans]+1; } $_SESSION[qn]=$_SESSION[qn]+1; } else if($submit=='Get Result' && isset($ans)) { mysql_data_seek($rs,$_SESSION[qn]); $row= mysql_fetch_row($rs); mysql_query("insert into mst_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error()); if($ans==$row[7]) { $_SESSION[trueans]=$_SESSION[trueans]+1; } echo "<h1 class=head1> Result</h1>"; $_SESSION[qn]=$_SESSION[qn]+1; echo "<Table align=center><tr class=tot><td>Total Question<td> $_SESSION[qn]"; echo "<tr class=tans><td>True Answer<td>".$_SESSION[trueans]; $w=$_SESSION[qn]-$_SESSION[trueans]; echo "<tr class=fans><td>Wrong Answer<td> ". $w; echo "</table>"; mysql_query("insert into mst_result(login,test_id,test_date,score) values('$login',$tid,'".date("d/m/Y")."',$_SESSION[trueans])") or die(mysql_error()); echo "<h1 align=center><a href=review.php> Review Question</a> </h1>"; unset($_SESSION[qn]); unset($_SESSION[sid]); unset($_SESSION[tid]); unset($_SESSION[trueans]); exit; } } $rs=mysql_query("SELECT DISTINCT * FROM mst_question WHERE que_id IN (SELECT DISTINCT que_id FROM mst_question where test_id=$tid )",$cn) or die(mysql_error()); if($_SESSION[qn]>mysql_num_rows($rs)-1) { unset($_SESSION[qn]); echo "<h1 class=head1>Some Error Occured</h1>"; session_destroy(); echo "Please <a href=index.php> Start Again</a>"; exit; } mysql_data_seek($rs,$_SESSION[qn]); $row= mysql_fetch_row($rs); echo "<form name=myfm method=post action=quiz.php>"; echo "<table width=100%> <tr> <td width=30> <td> <table border=0>"; $n=$_SESSION[qn]+1; echo "<tR><td><span class=style2>Que ". $n .": $row[2]</style>"; echo "<tr><td class=style8><input type=radio name=ans value=1>$row[3]"; echo "<tr><td class=style8> <input type=radio name=ans value=2>$row[4]"; echo "<tr><td class=style8><input type=radio name=ans value=3>$row[5]"; echo "<tr><td class=style8><input type=radio name=ans value=4>$row[6]"; if($_SESSION[qn]<mysql_num_rows($rs)-1) echo "<tr><td><input type=submit name=submit value='Next Question'></form>"; else echo "<tr><td><input type=submit name=submit value='Get Result'></form>"; echo "</table></table>"; ?> </body> </html> Edited July 21, 2013 by GFOnyX Quote Link to comment https://forums.phpfreaks.com/topic/280367-randomizing-questions-order-in-quiz/ Share on other sites More sharing options...
Riparian Posted July 23, 2013 Share Posted July 23, 2013 Just a suggestion and may be way off the mark but what if you were to load all the questions into a session array once per game while(.....){ $_SESSION[QUESTIONS][$id]=$question; } randomly select one question from the array $Quiz=$_SESSION[QUESTIONS][rand(0,sizeof($_SESSION[QUESTIONS])-1)]; remove it from the array unset($_SESSION[QUESTIONS][array_search($questionIDNumber , $_SESSION[QUESTIONS])]); The syntax might be wrong... just a thought... cheers Quote Link to comment https://forums.phpfreaks.com/topic/280367-randomizing-questions-order-in-quiz/#findComment-1441750 Share on other sites More sharing options...
GFOnyX Posted July 24, 2013 Author Share Posted July 24, 2013 (edited) Hey Riparian thx for the suggestion but since i am not experienced with php i'll have to study a bit before i can apply it beacuse what you propose is smth new to me. Meanwhile i managed to make questions appear in random order following a method i found here and if i pass the value of the correct answer to the input field manually it works (for example 1 when the correct answer is the first answer). I now have 2 problems. 1. I currently have more than one questions on the page. I want it to be only one.So far for my tests i am using "limit 1" but that doesnt solve the problem because it wont move to the next question. 2.How do i pass the other values to the input fileds using foreach? Right now i have 4 input fields as i was supposed to,4 questions as i was supposed to but i can only pass one value to one of the questions.How do i pass the rest of the values for the remaining three questions? Here is the modified code. The part which i refer to is where the "foreach" begins <?php session_start(); include("database.php"); extract($_POST); extract($_GET); extract($_SESSION); if(isset($subid) && isset($testid)) { $_SESSION[sid]=$subid; $_SESSION[tid]=$testid; header("location:quiz.php"); } if(!isset($_SESSION[sid]) || !isset($_SESSION[tid])) { header("location: index.php"); } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Online Quiz</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link href="quiz.css" rel="stylesheet" type="text/css"> </head> <body> <?php include("header.php"); $rs=mysql_query("SELECT * FROM mst_question WHERE test_id=$tid LIMIT 1",$cn) or die(mysql_error()); if(!isset($_SESSION[qn])) { $_SESSION[qn]=0; mysql_query("delete from mst_useranswer where sess_id='" . session_id() ."'") or die(mysql_error()); $_SESSION[trueans]=0; } else { if($submit=='Next Question' && isset($answ)) { mysql_data_seek($rs,$_SESSION[qn]); $row= mysql_fetch_row($rs); mysql_query("insert into mst_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$answ')") or die(mysql_error()); if($answ==$row[7]) { $_SESSION[trueans]=$_SESSION[trueans]+1; } $_SESSION[qn]=$_SESSION[qn]+1; } else if($submit=='Get Result' && isset($answ)) { mysql_data_seek($rs,$_SESSION[qn]); $row= mysql_fetch_row($rs); mysql_query("insert into mst_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$answ')") or die(mysql_error()); if($answ==$row[7]) { $_SESSION[trueans]=$_SESSION[trueans]+1; } echo "<h1 class=head1> Result</h1>"; $_SESSION[qn]=$_SESSION[qn]+1; echo "<Table align=center><tr class=tot><td>Total Question<td> $_SESSION[qn]"; echo "<tr class=tans><td>True Answer<td>".$_SESSION[trueans]; $w=$_SESSION[qn]-$_SESSION[trueans]; echo "<tr class=fans><td>Wrong Answer<td> ". $w; echo "</table>"; mysql_query("insert into mst_result(login,test_id,test_date,score) values('$login',$tid,'".date("d/m/Y")."',$_SESSION[trueans])") or die(mysql_error()); echo "<h1 align=center><a href=review.php> Review Question</a> </h1>"; unset($_SESSION[qn]); unset($_SESSION[sid]); unset($_SESSION[tid]); unset($_SESSION[trueans]); exit; } } $questions = array(); while($row = mysql_fetch_assoc($rs)) { $answers = array($row['ans1'], $row['ans2'], $row['ans3'], $row['ans4']); shuffle($answers); $questions[] = array( 'que_id' => $row['ques_id'], 'q' => $row['que_desc'], 'answers' => $answers ); shuffle($questions); $output = "<ol>\n"; foreach($questions as $ques) { $output .= "<li>\n"; $output .= "{$ques['q']}<br>\n"; foreach($ques['answers'] as $ans => $answ) { $output .= "<input type='radio' name='answ' value='1'> $answ<br>\n"; } $output .= "</li><br>\n"; } $output .= "</ol>\n"; echo "<form name=myfm method=post action=quiz.php>"; echo "<table width=100%> <tr> <td width=30> <td> <table border=0>"; echo $output; } if($_SESSION[qn]<mysql_num_rows($rs)-1) echo "<tr><td><input type=submit name=submit value='Next Question'></form>"; else echo "<tr><td><input type=submit name=submit value='Get Result'></form>"; echo "</table></table>"; ?> </body> </html> Edited July 24, 2013 by GFOnyX Quote Link to comment https://forums.phpfreaks.com/topic/280367-randomizing-questions-order-in-quiz/#findComment-1441968 Share on other sites More sharing options...
QuickOldCar Posted July 24, 2013 Share Posted July 24, 2013 Is always difficult to get true random results. You can try shuffle() more than once, or try array_rand() and use the count of results in array I have also used making a numbers range() with a combination of mysql's max(), and then selecting some results from x many results using key values Quote Link to comment https://forums.phpfreaks.com/topic/280367-randomizing-questions-order-in-quiz/#findComment-1442000 Share on other sites More sharing options...
QuickOldCar Posted July 24, 2013 Share Posted July 24, 2013 here is a function that may be of use to you, modify it to your needs. <?php function random_row($table, $column) { $max_sql = "SELECT max(" . $column . ") AS max_id FROM " . $table; $max_row = mysql_fetch_array(mysql_query($max_sql)); $random_number = mt_rand(1, $max_row['max_id']); $random_sql = "SELECT * FROM " . $table . " WHERE " . $column . " >= " . $random_number . " ORDER BY " . $column . " ASC LIMIT 1"; $random_row = mysql_fetch_row(mysql_query($random_sql)); if (!is_array($random_row)) { $random_sql = "SELECT * FROM " . $table . " WHERE " . $column . " < " . $random_number . " ORDER BY " . $column . " DESC LIMIT 1"; $random_row = mysql_fetch_row(mysql_query($random_sql)); } return $random_row; } //USAGE echo '<pre>'; print_r(random_row('YOUR_TABLE', 'YOUR_COLUMN')); echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/280367-randomizing-questions-order-in-quiz/#findComment-1442002 Share on other sites More sharing options...
Joshua F Posted July 25, 2013 Share Posted July 25, 2013 (edited) - read op wrong Edited July 25, 2013 by Joshua F Quote Link to comment https://forums.phpfreaks.com/topic/280367-randomizing-questions-order-in-quiz/#findComment-1442050 Share on other sites More sharing options...
GFOnyX Posted July 25, 2013 Author Share Posted July 25, 2013 (edited) QuickOldCar thx for your solution.It is way more sophisticated than the one i used. However i have managed for now to make questions appear in random using the shuffle(array) as you also mention.My problem is now using the foreach loop how to pass all four values (1,2,3,4) for the answers. After i solve that i will try apllying your more advanced method. The part of the code i reder to is this one: while($row = mysql_fetch_assoc($rs)) { $answers = array($row['ans1'], $row['ans2'], $row['ans3'], $row['ans4']); shuffle($answers); $questions[] = array( 'que_id' => $row['ques_id'], 'q' => $row['que_desc'], 'answers' => $answers ); shuffle($questions); $output = "<ol>\n"; foreach($questions as $ques) { $output .= "<li>\n"; $output .= "{$ques['q']}<br>\n"; foreach($ques['answers'] as $ans => $answ) { $output .= "<input type='radio' name='answ' value='1'> $answ<br>\n"; } $output .= "</li><br>\n"; } As you can see i pass for example the value 1 for the first answer (ans1).How can i pass all 4 values in there? Thanks again everyone for all the help. Edited July 25, 2013 by GFOnyX Quote Link to comment https://forums.phpfreaks.com/topic/280367-randomizing-questions-order-in-quiz/#findComment-1442081 Share on other sites More sharing options...
QuickOldCar Posted July 26, 2013 Share Posted July 26, 2013 $number_value = 0; while($row = mysql_fetch_assoc($rs)) { $answers = array($row['ans1'], $row['ans2'], $row['ans3'], $row['ans4']); shuffle($answers); $questions[] = array( 'que_id' => $row['ques_id'], 'q' => $row['que_desc'], 'answers' => $answers ); shuffle($questions); $output = "<ol>\n"; foreach($questions as $ques) { $output .= "<li>\n"; $output .= "{$ques['q']}<br>\n"; foreach($ques['answers'] as $ans => $answ) { $number_value = $number_value + 1; $output .= "<input type='radio' name='answ' value='$number_value'> $answ<br>\n"; } $output .= "</li><br>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/280367-randomizing-questions-order-in-quiz/#findComment-1442213 Share on other sites More sharing options...
GFOnyX Posted July 26, 2013 Author Share Posted July 26, 2013 $number_value = 0; while($row = mysql_fetch_assoc($rs)) { $answers = array($row['ans1'], $row['ans2'], $row['ans3'], $row['ans4']); shuffle($answers); $questions[] = array( 'que_id' => $row['ques_id'], 'q' => $row['que_desc'], 'answers' => $answers ); shuffle($questions); $output = "<ol>\n"; foreach($questions as $ques) { $output .= "<li>\n"; $output .= "{$ques['q']}<br>\n"; foreach($ques['answers'] as $ans => $answ) { $number_value = $number_value + 1; $output .= "<input type='radio' name='answ' value='$number_value'> $answ<br>\n"; } $output .= "</li><br>\n"; } Thank you so much QuickOldCar! The values 1,2,3,4 are actually passing to the DB.I was trying to do something similar to your solution but my syntax was way off. The problem now is it does not seem to recognize the correct id after the questions have been shuffled. Here is an example: What does SQL stand for? Structured Query Language Structured Question Language String Query Language Strong Question Language The correct answer (Structured Query Language) here is the answer with id 3.However when i select the correct answer (Structured Query Language) it will pass to the database the id 1 because the correct answer showed up first. Quote Link to comment https://forums.phpfreaks.com/topic/280367-randomizing-questions-order-in-quiz/#findComment-1442263 Share on other sites More sharing options...
QuickOldCar Posted July 26, 2013 Share Posted July 26, 2013 would something like this work? $number_value = 0; while($row = mysql_fetch_assoc($rs)) { $answers = array($row['ans1'], $row['ans2'], $row['ans3'], $row['ans4']); shuffle($answers); $questions[] = array( 'que_id' => $row['ques_id'], 'q' => $row['que_desc'], 'answers' => $answers ); shuffle($questions); $output = "<ol>\n"; foreach($questions as $ques) { $output .= "<li>\n"; $output .= "{$ques['q']}<br>\n"; foreach($ques['answers'] as $answ) { $number_value = $number_value + 1; $output .= "<input type='radio' name='".$answ[$number_value]."' value='$number_value'>'".$answ[$number_value]."<br>\n"; } $output .= "</li><br>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/280367-randomizing-questions-order-in-quiz/#findComment-1442334 Share on other sites More sharing options...
GFOnyX Posted July 27, 2013 Author Share Posted July 27, 2013 would something like this work? $number_value = 0; while($row = mysql_fetch_assoc($rs)) { $answers = array($row['ans1'], $row['ans2'], $row['ans3'], $row['ans4']); shuffle($answers); $questions[] = array( 'que_id' => $row['ques_id'], 'q' => $row['que_desc'], 'answers' => $answers ); shuffle($questions); $output = "<ol>\n"; foreach($questions as $ques) { $output .= "<li>\n"; $output .= "{$ques['q']}<br>\n"; foreach($ques['answers'] as $answ) { $number_value = $number_value + 1; $output .= "<input type='radio' name='".$answ[$number_value]."' value='$number_value'>'".$answ[$number_value]."<br>\n"; } $output .= "</li><br>\n"; } Thanx for the help but just tried it and it doesnt work. The new code seems to present 2 problems: 1. The answers do not appear next to the input buttons. Instead of the phrases, a single letter appears next to each input but i am guessing it is just a letter from the answer phrase because they change when i hit refresh like the answers do. However if i change the value='$number_value'>'".$answ[$number_value]."<br>\n"; with value='$number_value'> $answ<br>\n"; the answers appear again but it doesnt really make a difference beacuse the following problem persists 2. The submit button (Get Result) does not work now, meaning it doesn't pass anything to the database and instead it just refreshes the same page. Quote Link to comment https://forums.phpfreaks.com/topic/280367-randomizing-questions-order-in-quiz/#findComment-1442362 Share on other sites More sharing options...
GFOnyX Posted July 29, 2013 Author Share Posted July 29, 2013 (edited) I modified the code to execute something that shoud be more simple but still doesnt work the way it was supposed to. I am currently trying to display in random order ONLY the questions and NOT the answers. The questions do display randomly but the results even though they are now fixed they are not passing correctly. By looking at another table at my DB which temporarily stores the results i found out that this is happening because it passes a different question to the DB than the one the user sees while taking the quiz. I have no idea why this is happening now. How is it possible to show me a specific question and at the same time passing a different question to the DB? It is like they pass to the DB in a specific order and if that order matches the order the user sees than the score is ok. This is very weird. Here is my modified code: <?php session_start(); include("database.php"); extract($_POST); extract($_GET); extract($_SESSION); if(isset($subid) && isset($testid)) { $_SESSION[sid]=$subid; $_SESSION[tid]=$testid; header("location:quiz.php"); } if(!isset($_SESSION[sid]) || !isset($_SESSION[tid])) { header("location: index.php"); } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Online Quiz</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link href="quiz.css" rel="stylesheet" type="text/css"> </head> <body> <?php include("header.php"); $rs=mysql_query("SELECT * FROM mst_question WHERE test_id=$tid",$cn) or die(mysql_error()); if(!isset($_SESSION[qn])) { $_SESSION[qn]=0; mysql_query("delete from mst_useranswer where sess_id='" . session_id() ."'") or die(mysql_error()); $_SESSION[trueans]=0; } else { if($submit=='Next Question' && isset($answ)) { mysql_data_seek($rs,$_SESSION[qn]); $row= mysql_fetch_row($rs); mysql_query("insert into mst_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$answ')") or die(mysql_error()); if($answ==$row[7]) { $_SESSION[trueans]=$_SESSION[trueans]+1; } $_SESSION[qn]=$_SESSION[qn]+1; } else if($submit=='Get Result' && isset($answ)) { mysql_data_seek($rs,$_SESSION[qn]); $row= mysql_fetch_row($rs); mysql_query("insert into mst_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$answ')") or die(mysql_error()); if($answ==$row[7]) { $_SESSION[trueans]=$_SESSION[trueans]+1; } echo "<h1 class=head1> Result</h1>"; $_SESSION[qn]=$_SESSION[qn]+1; echo "<Table align=center><tr class=tot><td>Total Question<td> $_SESSION[qn]"; echo "<tr class=tans><td>True Answer<td>".$_SESSION[trueans]; $w=$_SESSION[qn]-$_SESSION[trueans]; echo "<tr class=fans><td>Wrong Answer<td> ". $w; echo "</table>"; mysql_query("insert into mst_result(login,test_id,test_date,score) values('$login',$tid,'".date("d/m/Y")."',$_SESSION[trueans])") or die(mysql_error()); echo "<h1 align=center><a href=review.php> Review Question</a> </h1>"; unset($_SESSION[qn]); unset($_SESSION[sid]); unset($_SESSION[tid]); unset($_SESSION[trueans]); exit; } } $questions = array(); $number_value = 0; while($row = mysql_fetch_assoc($rs)) { $answers = array($row['ans1'], $row['ans2'], $row['ans3'], $row['ans4']); //shuffle($answers); $questions[] = array( 'que_id' => $row['ques_id'], 'q' => $row['que_desc'], 'answers' => $answers ); } shuffle($questions); $output = "<ol>\n"; foreach($questions as $ques) {} //$output .= "<li>\n"; $output .= "{$ques['q']}<br>\n"; foreach($ques['answers'] as $ans => $answ) { $number_value = $number_value + 1; $output .= "<input type='radio' name='answ' value='$number_value'> $answ<br>\n"; } //$output .= "</li><br>\n"; echo "<form name=myfm method=post action=quiz.php>"; echo "<table width=100%> <tr> <td width=30> <td> <table border=0>"; echo $output; if($_SESSION[qn]<mysql_num_rows($rs)-1) echo "<tr><td><input type=submit name=submit value='Next Question'></form>"; else echo "<tr><td><input type=submit name=submit value='Get Result'></form>"; echo "</table></table>"; ?> </body> </html> Edited July 29, 2013 by GFOnyX Quote Link to comment https://forums.phpfreaks.com/topic/280367-randomizing-questions-order-in-quiz/#findComment-1442545 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.