Jump to content

Help.MySql Read and Compare .


greenfilled

Recommended Posts

ufff... Hello People !. SO !

 

I need a small QUIZ script for my site . I could not find the right script for me, so I decided to do it myself, BUT I am not a a big php programmer .

 

So . The quiz is supposed to have 25 questions, with 3 answers each . BUT ! the thing is that some questions can have one, two or three right anwers correct, and all the right answers have to be corect for the hole question to be corect .

 

So that is my main problem .

 

The script reads from the database the question number ID, the question, the answers (A1, A2, A3) and the value for each answer. (R1, R2, R3 ) . like for A1 R1=0 , that means R1 is wrong . and so on .

each question has 3 answers .

 

I CAN NOT get right the code that makes variables for each $_POST and the code that should compare it with the value from the database .

 

THE CODE

the main page, the quiz page :

 

                        <form method="POST" action="quiz.php">
                        <p align="left"> Hello what is your name? <br/>
									Enter name:
					<input type="text" name="name" value="Mr./Mrs." size="15" maxlength="15">


<?php

$username="root";
$password="password";
$database="quiz";

mysql_connect(localhost,$username,$password);

mysql_close();

@mysql_select_db($database) or die( "Can not select database!");


function dbQuery($sql, $silence = true)
{
$query = mysql_query($sql);

if(!$query && !$silence)
{
	WriteError('Query Error', mysql_error().'<br />Query String: '.$sql);

	return false;
}

return $query;
}

$query1 = "SELECT Id, ImgFile, Question, AnswerA, AnswerB, AnswerC, R1, R2, R3
FROM `quiz` ORDER BY `Id` ASC 
LIMIT 0 , 26";
$result1 = dbQuery($query1) or die(mysql_error());
$count = 0;
$intrebarecount =0;
$questions_per_row=1;
while (list ($Id, $ImgFile, $Question, $AnswerA, $AnswerB, $AnswerC, $R1, $R2, $R3) = mysql_fetch_row($result1)) 
{ 
	if ($count == $questions_per_row) 
	{ 
		echo '</tr><tr>';
		$count = 0;
	} 

$intrebarecount ++;
echo '
					<td><!-- QUESTION AND ANSWERS CHOICES-->
			<tr>
					<td> _______________________________________ <br/>
							<b>Intrebarea nr.'.$intrebarecount.'</b> <br/>
							<font color=green> '.$Question.' </font> <br/>
							<input type="checkbox" value="'.$R1.'" name="'.$Id.'R1">
							<font color=red>
							'.$AnswerA.'
							<br>
							<input type="checkbox" value="'.$R2.'" name="'.$Id.'R2">
							'.$AnswerB.'
							<br>
							<input type="checkbox" value="'.$R3.'" name="'.$Id.'R3">
							'.$AnswerC.'
							</font> <br/>
					</td>
									<tr>				
					';


	$count++;
}
?>

			<tr>
					<td><br/>
							<p align="center">
									<input type="submit" value="Mark Questions" name="questionsanswer">
									<input type="reset" value="Reset Questions" name="reset">

							</p>
					</td>
			</tr>

 

THE CODE

the php compare script page, the result page :

 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Quiz Result</title>
</head>
		<body>
					<table align="center" vspace="200" border="3" bgcolor=pink>
						<tr>
						  <td>
<?		
			echo 'The time & date is: ';
			echo date('j F H:i');
			echo	"<br/>Hello $_POST[name]";

// ================================================
$username="root";
$password="insane";
$database="quiz";
$counter = 0;

mysql_connect(localhost,$username,$password);
mysql_close();
@mysql_select_db($database) or die( "Can not select database!");

function dbQuery($sql, $silence = true)
{
$query = mysql_query($sql);
if(!$query && !$silence)
{
	WriteError('Query Error', mysql_error().'<br />Query String: '.$sql);
	return false;
}
return $query;
}

$query22 = "SELECT Id, ImgFile, Question, AnswerA, AnswerB, AnswerC, R1, R2, R3
FROM `quiz` ORDER BY `Id` ASC 
LIMIT 0 , 26";
$result11 = dbQuery($query22) or die(mysql_error());
$count = 0;
$intrebarecount1 =0;
$questions_per_row=1;
while (list ($Id, $ImgFile, $Question, $AnswerA, $AnswerB, $AnswerC, $R1, $R2, $R3) = mysql_fetch_row($result11)) 
{ 
	if ($count == $questions_per_row) 
	{ 
		echo '</tr><tr>';
		$count = 0;
	} 


$intrebarecount1 ++;

$RR1 = $Id . 'R1';
$RR2 = $Id . 'R2';
$RR3 = $Id . 'R3';

    print $R1;
    print ':::'.$_POST[$RR1];
if (($_POST[$RR1] = $R1) && ($_POST[$RR2] = $R2) && ($_POST[$RR3] = $R3))
{
			$RaspunsFinal = "CORECT";
			$counter++;	
exit;
}
else
{
			$RaspunsFinal = "WRONG";
}


echo '
					<td><!-- ANSWERS-->
			<tr>
					<td> _______________________________________ <br/>
							<b>Intrebarea nr.'.$intrebarecount1.'</b> <br/>
							<font color=green> '.$Question.' </font> <br/>
							<input disabled="disabled" type="checkbox" value="0" name="'.$Id.'R1">
							<font color=red>
							'.$AnswerA.'
							<br>
							<input disabled="disabled"  type="checkbox" value="0" name="'.$Id.'R2">
							'.$AnswerB.'
							<br>
							<input disabled="disabled" type="checkbox" value="1" name="'.$Id.'R3">
							'.$AnswerC.'
							</font> ';
       
   					print $RaspunsFinal;

        
    echo '				<br/>
					</td>
									<tr>				
					';

	$count++;
}



			//Generate and out put the result
			$result = $counter / 26 * 100;							

			echo "$counter Questions out of 26 was answered correctly<br/>";
			echo "You got $result % right!<br/>";

			if ($result >= 21)
							{
								echo '<b><big>Congratulations !<br/>';
								echo 'Why not see if your can beat your last score <a href="quiz.php">click here</a>.';
							}	
							else 
									{
										echo '<b>Not good...</b><br/>';											
										echo 'To do the quiz again, please <a href="quiz.php">click here</a>.';		
									}	

?>
								</td>
						</tr>
				</table>

		</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.