I want to make you all understand that I appreciate your collective efforts in attempting to solve the challenge that I put forth. However, I have solved the challenge. Below is the source:
<?php
// Establish Connection with Database// Select Database
include_once( '../../Connections/QUIZ.php' );
// Specify the query to execute
Every student has an app id in the session after login.
For every question the student answers, the app performs an UPDATE in another table u_answer.
We will first of all check whether the student has already answered any questions by performing a select query on the u_answer table.
AS_Id is the student's Id from the session variable.
$sql = "select * from u_answer,questions where u_answer.AS_Id = '" . $_SESSION[ 'appID' ] . "' and questions.qid = u_answer.qid";
$result = mysql_query( $sql, $con ) or die( mysql_error() );
$records = mysql_num_rows( $result );
if ( $records < 1 ) {
//create and increment a counter(x) by 1
$x = $records + 1;
Perform the original select query for the questions.
$sql2 = "select * from questions ORDER BY RAND() LIMIT 1";
else
{
Still increment the counter by 1 using the number of records found.
$x = $records + 1;
This select query below ensures that no students answers a particular question more than 1 time.
$sql2 = "SELECT questions.qid, questions.question,questions.ans1,
questions.ans2, questions.ans3, questons.ans4, questions.correctAns
FROM questions WHERE NOT EXISTS(SELECT * FROM u_answer WHERE u_answer.qid = questions.qid AND u_answer.AS_Id = '" . $_SESSION[ 'appID' ] . "') ORDER BY RAND() LIMIT 1";
$result2 = mysql_query( $sql2 );
perform a while loop here at this point.
while ( $row = mysql_fetch_array( $result2 ) ) {
$id = $row['id'];
$quest = $row['questions'];
$a1 = $row['ans1'];
$a2 = $row['ans2'];
$a3 = $row['ans3'];
$correct = $row['correctAns'];
Print out the extracted data.
echo $x.'<br />'; //the counter variable.
echo $quest.<br />;
echo $a1.'<br />';
echo $a2.'<br />';
echo $a3.'<br />';
echo $correct.'<br />';
}
Close connection to the database.
mysql_close($con);
?>
This works for me.