Jump to content

Quizz


ulisses

Recommended Posts

Hi all!I working on a quizz and I have 2 tables. The first one has the following fields:id- the question idquestion- the questionoptions- the optionsanswer- the correct answer for the questionThe second one has:subscriber- the subscriber that's on the quizzquestion- the questioncounter- the points the subscriber hasquestion_sequence- the sequence of the questions have been postedThe idea is:The question posted to the subscriber randomly. Each right answer the points increment by one. If a subscriber answers correctly 10 consecutive questions he wins a gift, but if it fails before reaching 10 consecutive correct answers his points go back to zero.My problem at this moment is the counter and the question sequence. Don't know why the counter is not incrementing. The question sequence I know how i have to do, but don't really know how to implement. If u answer question by the sequence: question 10, question 4, question 18, ... it shall insert on the table like this 10,4,18; like this it wouldn't repeat one question to a subscriber.I'd appreciate any idea

Link to comment
Share on other sites

What code do you have so far, what do you need help with?
these are the pages i have:1- the start page
<html><body><form action="quizz.php" method="post" ><b>Your number :</b><input type="text" name="phone" /><input type="submit" name="submit" value="start QUIZZ" /></form></body></html>

2-

<?phpsession_start();$con = mysql_connect("servername","username","password");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("db", $con);$_SESSION['number']=$_POST['phone'];mysql_query("INSERT INTO answers_log (subscriber) VALUE ('$_SESSION[number]')");$max_id=mysql_query("SELECT MAX(id) AS max FROM questions_log");$max_row=mysql_fetch_array($max_id);$sup=$max_row['max'];$min_id=mysql_query("SELECT MIN(id) AS min FROM questions_log");$min_row=mysql_fetch_array($min_id);$inf=$min_row['min'];$question_id=(mt_rand($inf,$sup));$select_q=mysql_query("SELECT * FROM questions_log WHERE id=$question_id");$select_row=mysql_fetch_array($select_q);$answer=mysql_query("SELECT answer FROM questions_log WHERE id=$question_id");$answer_row=mysql_fetch_array($answer);$_SESSION['answer']=$answer_row['answer'];$_SESSION['question']=$select_row['question'];$_SESSION['id']=$select_row['id'];echo "<b>" . $select_row['question'] . "</b></br>";echo $select_row['options'];mysql_close($con);?><html><body><form action="quizz1.php" method="post" ><b>a:</b><input type="radio" name="option" value="a" /></br><b>b:</b><input type="radio" name="option" value="b" /></br><b>c:</b><input type="radio" name="option" value="c" /></br><input type="submit" name="submit" value="submit answer" /></form></body></html>

3-

<html><body><?phpsession_start();$con = mysql_connect("servername","username","password");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("bigexp", $con);$x=$_POST['option'];$counter=mysql_query("SELECT counter_r FROM answers_log");$counter_row=mysql_fetch_array($counter);$r=(int)$counter_row['counter_r'];if($x==$_SESSION['answer'])  {  $r++;  echo $r++;	echo "GREAT! CORRECT ANSWER!</br>";	mysql_query("UPDATE answers_log SET question='$_SESSION[question]', counter_r=$r, question_sequence='$_SESSION[id]' WHERE subscriber='$_SESSION[number]'");$max_id=mysql_query("SELECT MAX(id) AS max FROM questions_log");$max_row=mysql_fetch_array($max_id);$sup=$max_row['max'];$min_id=mysql_query("SELECT MIN(id) AS min FROM questions_log");$min_row=mysql_fetch_array($min_id);$inf=$min_row['min'];$question_id=(mt_rand($inf,$sup));$select_q=mysql_query("SELECT * FROM questions_log WHERE id=$question_id");$select_row=mysql_fetch_array($select_q);$answer=mysql_query("SELECT answer FROM questions_log WHERE id=$question_id");$answer_row=mysql_fetch_array($answer);$_SESSION['answer1']=$answer_row['answer'];$_SESSION['question1']=$select_row['question'];$_SESSION['id1']=$select_row['id'];	echo "<b>" . $select_row['question'] . "</b></br>";  echo $select_row['options'];	echo '<form action="quizz2.php" method="post" ><b>a:</b><input type="radio" name="option" value="a" /></br><b>b:</b><input type="radio" name="option" value="b" /></br><b>c:</b><input type="radio" name="option" value="c" /></br><input type="submit" name="submit" value="submit answer" /></form>';	}	else	{	echo "UNFORTUNATELLY U FAILED! YOU WILL HAVE TO RESTART</br>";	mysql_query("DELETE FROM answers_log WHERE subscriber='$_SESSION[number]'");  echo '<form action="start_quizz.php" >		<input type="submit" name="submit" value="restart QUIZZ" />';  } mysql_close($con);?></body></html>

and so on. Thks

Link to comment
Share on other sites

It's going to be easiest if you do all of this in one file instead of having one file per question. That way at least you don't have to duplicate your code.Also, this: $r++; echo $r++;is going to increment $r twice. With this:

$max_id=mysql_query("SELECT MAX(id) AS max FROM questions_log");$max_row=mysql_fetch_array($max_id);$sup=$max_row['max'];$min_id=mysql_query("SELECT MIN(id) AS min FROM questions_log");$min_row=mysql_fetch_array($min_id);$inf=$min_row['min'];

You might as well just put it all in the same query:SELECT MAX(id) AS max, MIN(id) AS min FROM questions_logFor this:

$select_q=mysql_query("SELECT * FROM questions_log WHERE id=$question_id");$select_row=mysql_fetch_array($select_q);$answer=mysql_query("SELECT answer FROM questions_log WHERE id=$question_id");$answer_row=mysql_fetch_array($answer);

You're doing the same thing twice, first you're getting every field from the table, then you're getting a single field. You already got the answer field in the first result, you don't need to get it again.To store the list of IDs you've already gotten, you need to store them in a comma-separated list and then you use that list of the list of IDs to skip the next time you look for a question, e.g.:SELECT * FROM questions WHERE id NOT IN (1,2,3) ORDER BY RAND()That will select a random record that does not have one of those IDs.

Link to comment
Share on other sites

Ok, it's almost done! I've followed the advices and make it running fine. There is one problem with repeating questions.I've put the questions id in an array called $past_q. This is how $past_q is defined:

$sequence= "$select_row[question_sequence]" . "," . "$_SESSION[id]";$past_q=explode(",",$sequence);

When I select next question I do this:

$max_id=mysql_query("SELECT MAX(id) AS max, MIN(id) AS min FROM questions_log WHERE id NOT IN ('".implode(',', $past_q)."') ORDER BY RAND()");$max_row=mysql_fetch_array($max_id);$sup=$max_row['max'];$inf=$max_row['min'];$question_id=(mt_rand($inf,$sup));

but it still repeating questions.

Link to comment
Share on other sites

Make sure you're saving the array back to the database each time. You can do something like this to define it:

$past_q = explode(",", $select_row['question_sequence']);$past_q[] = $_SESSION['id'];

Then you would implode that and save it back to the table you got question_sequence from. In the select query, don't quote the list of IDs. You need a query that looks like this:

SELECT MAX(id) AS max, MIN(id) AS min FROM questions_log WHERE id NOT IN (1, 2, 3) ORDER BY RAND()

instead of this:

SELECT MAX(id) AS max, MIN(id) AS min FROM questions_log WHERE id NOT IN ('1, 2, 3') ORDER BY RAND()

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...