Jump to content

Help ! anyone.. :(


jushiro

Recommended Posts

i have a code for making a quiz ing php.. im having problem when it need to compute the correct answer.. i think it cant read the values. :(

 

Here's the code.

 

<?php
$host="localhost"; 
$username="root"; 
$password="";
$db_name="dbquiz";
$value = $_POST['p'];

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$value = stripslashes($value);
$value = mysql_real_escape_string($value);

$sql="SELECT * FROM questions WHERE assname = '$value' ORDER BY RAND() LIMIT 100";
$result=mysql_query($sql) or die(mysql_error());

if(result){
while($row = mysql_fetch_assoc($result)) 
    { 
    $q = $row['question']; 
$c1= "" .$row['choice1']; 
$c2 ="" .$row['choice2']; 
$c3 ="" .$row['choice3']; 
$c4 ="" .$row['choice4']; 
$a ="".$row['answer'];
$questions[] = array($q,$c1,$c2,$c3,$c4,$a);
    } 
}
include_once("quiz_maker.php");

?>

and for the quiz maker.

<?php

if (isset($_POST['sent'])) {
for ($i=0;$i<count($questions);$i++) {
	echo($questions[$i][0]." - ");
	if ($_POST['q'.$i]=="c") {
		echo("<b>Correct!</b><br>\n");
		$score++;
	} else {
		echo("<b>Wrong!</b><br>\n");
	}
}
$percent = number_format(($score/count($questions))*100,2,".",",");
echo("<br>".$score." out of ".count($questions)." (".$percent."% right)<br>\n");
} else {
echo("<form action=\"#\" method=\"post\">\n");
echo("<input type=\"hidden\" name=\"sent\">\n");
for ($i=0;$i<count($questions);$i++) {
	echo("<b>".$questions[$i][0]."</b><br><br>\n");
	if ($questions[$i][5]==1) {
		echo("<input type=\"radio\" name=\"q".$i."\" value=\"c\"> ".$questions[$i][1]."<br>\n");
	} else {
		echo("<input type=\"radio\" name=\"q".$i."\" value=\"w\"> ".$questions[$i][1]."<br>\n");
	}
	if ($questions[$i][5]==2) {
		echo("<input type=\"radio\" name=\"q".$i."\" value=\"c\"> ".$questions[$i][2]."<br>\n");
	} else {
		echo("<input type=\"radio\" name=\"q".$i."\" value=\"w\"> ".$questions[$i][2]."<br>\n");
	}
	if ($questions[$i][5]==3) {
		echo("<input type=\"radio\" name=\"q".$i."\" value=\"c\"> ".$questions[$i][3]."<br>\n");
	} else {
		echo("<input type=\"radio\" name=\"q".$i."\" value=\"w\"> ".$questions[$i][3]."<br>\n");
	}
	if ($questions[$i][5]==4) {
		echo("<input type=\"radio\" name=\"q".$i."\" value=\"c\"> ".$questions[$i][4]."<br><br>\n");
	} else {
		echo("<input type=\"radio\" name=\"q".$i."\" value=\"w\"> ".$questions[$i][4]."<br><br>\n");
	}
}
echo("<input type=\"submit\" value=\"Am I Right?!\">");
}
?>

...

 

Here what the error says.. "Warning: Division by zero in C:\Documents and Settings\Ken\Desktop\xampplite\htdocs\Pages\quiz_maker.php on line 13"

Here the line 13.

"$percent = number_format(($score/count($questions))*100,2,".",",");"

 

 

 

 

.. i dont get it..

the question are shown but when the quizmaker runs it's computation for the correct answer im getting an error.

Anyone help pls. :(

 

Link to comment
https://forums.phpfreaks.com/topic/247174-help-anyone/
Share on other sites

Archived

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

×
×
  • 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.