Hi
I am trying to make a multi choice quiz. I aim to randomly select 5 questions from a database of 100 questions. Two are true false, one will have more than one answer, the other two will have one answer.
// select two questions from database that are true / false
$query = "select row_id from questions where multi_answer = 'b' order by rand() limit 2";
$result = mysql_query($query);
while($boolean = mysql_fetch_array($result,MYSQL_ASSOC)){
$quiz[] = $boolean['row_id'];
};
// select two question from database that will have more than one answer
$query = "select row_id from questions where multi_answer = 'y' order by rand() limit 1";
$result = mysql_query($query);
while($multi = mysql_fetch_array($result,MYSQL_ASSOC)){
$quiz[] = $multi['row_id'];
};
// select two questions from database that can have only one answer
$query = "select row_id from questions where multi_answer = 'n' order by rand() limit 2";
$result = mysql_query($query);
while($normal = mysql_fetch_array($result,MYSQL_ASSOC)){
$quiz[] = $normal['row_id'];
};
and I need to work this somehow into the below code. so that it uses the random set of numbers that i know will be made up of the right type of questions. Maybe I have attacked this from totally the wrong angle. Please let me know.
$display = 1;
if(isset($_GET['np'])) {
$num_pages = $_GET['np'];
} else {
$num_pages = 5;
}
// determine where in the database to start returning results
if(isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
$query = "select row_id, question from questions limit $start, $display";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "No.".$row['row_id']." ".$row['question']."<br />";
$query2 = 'select answer from x_answers where question_id ='.$row['row_id'];
$result2 = mysql_query($query2);
while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
echo "Answer ".$row2['answer']."<br />";
}
}
################################################################################################################################
// make the links to the other pages
if($num_pages > 1) {
echo '<br /><p>';
// determine what page the script is on
$current_page = ($start/$display) + 1;
// if its not the first page, make a previous button
if($current_page != 1) {
echo '<a href="quiz.php?s='.($start - $display).'&np='.$num_pages.'">Previous</a> ';
}
// make all the numbered pages
for($i=1; $i <= $num_pages; $i++) {
if($i != $current_page) {
echo '<a href="quiz.php?s='.(($display * ($i -1))).'&np='.$num_pages.'">'.$i.'</a> ';
} else {
echo $i.' ';
}
}
// if its not the last page make a next button
if($current_page != $num_pages) {
echo '<a href="quiz.php?s='.($start + $display).'&np='.$num_pages.'">Next</a>';
}
}
I hope this makes sense. All I need is create a multi choice quiz. It must pull 5 random questions from the database. (made up of two true/false, one multi answer,and two single answer)
each of the possible answers is also put in a random order.
Any help would be much appreciated.
Thank you
I am trying to make a multi choice quiz. I aim to randomly select 5 questions from a database of 100 questions. Two are true false, one will have more than one answer, the other two will have one answer.
// select two questions from database that are true / false
$query = "select row_id from questions where multi_answer = 'b' order by rand() limit 2";
$result = mysql_query($query);
while($boolean = mysql_fetch_array($result,MYSQL_ASSOC)){
$quiz[] = $boolean['row_id'];
};
// select two question from database that will have more than one answer
$query = "select row_id from questions where multi_answer = 'y' order by rand() limit 1";
$result = mysql_query($query);
while($multi = mysql_fetch_array($result,MYSQL_ASSOC)){
$quiz[] = $multi['row_id'];
};
// select two questions from database that can have only one answer
$query = "select row_id from questions where multi_answer = 'n' order by rand() limit 2";
$result = mysql_query($query);
while($normal = mysql_fetch_array($result,MYSQL_ASSOC)){
$quiz[] = $normal['row_id'];
};
and I need to work this somehow into the below code. so that it uses the random set of numbers that i know will be made up of the right type of questions. Maybe I have attacked this from totally the wrong angle. Please let me know.
$display = 1;
if(isset($_GET['np'])) {
$num_pages = $_GET['np'];
} else {
$num_pages = 5;
}
// determine where in the database to start returning results
if(isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
$query = "select row_id, question from questions limit $start, $display";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "No.".$row['row_id']." ".$row['question']."<br />";
$query2 = 'select answer from x_answers where question_id ='.$row['row_id'];
$result2 = mysql_query($query2);
while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
echo "Answer ".$row2['answer']."<br />";
}
}
################################################################################################################################
// make the links to the other pages
if($num_pages > 1) {
echo '<br /><p>';
// determine what page the script is on
$current_page = ($start/$display) + 1;
// if its not the first page, make a previous button
if($current_page != 1) {
echo '<a href="quiz.php?s='.($start - $display).'&np='.$num_pages.'">Previous</a> ';
}
// make all the numbered pages
for($i=1; $i <= $num_pages; $i++) {
if($i != $current_page) {
echo '<a href="quiz.php?s='.(($display * ($i -1))).'&np='.$num_pages.'">'.$i.'</a> ';
} else {
echo $i.' ';
}
}
// if its not the last page make a next button
if($current_page != $num_pages) {
echo '<a href="quiz.php?s='.($start + $display).'&np='.$num_pages.'">Next</a>';
}
}
I hope this makes sense. All I need is create a multi choice quiz. It must pull 5 random questions from the database. (made up of two true/false, one multi answer,and two single answer)
each of the possible answers is also put in a random order.
Any help would be much appreciated.
Thank you