jushiro Posted September 15, 2011 Share Posted September 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247174-help-anyone/ Share on other sites More sharing options...
Pandemikk Posted September 15, 2011 Share Posted September 15, 2011 That means either $score or count($questions) is evaluating to 0 (perhaps even both). To debug which one, please var_dump both of those and exit(); right before line 13. Quote Link to comment https://forums.phpfreaks.com/topic/247174-help-anyone/#findComment-1269489 Share on other sites More sharing options...
jushiro Posted September 15, 2011 Author Share Posted September 15, 2011 they return to NULL.. as i thought. what should i do? Quote Link to comment https://forums.phpfreaks.com/topic/247174-help-anyone/#findComment-1269490 Share on other sites More sharing options...
Pandemikk Posted September 15, 2011 Share Posted September 15, 2011 Both of them? Well $score is 0 because your for loop isn't getting looped because count($questions) is 0. And I think I see one problem, your code shows "if(result)". It should be if ($result). Quote Link to comment https://forums.phpfreaks.com/topic/247174-help-anyone/#findComment-1269505 Share on other sites More sharing options...
jushiro Posted September 15, 2011 Author Share Posted September 15, 2011 I changed my code for that.. but still has the same error. but why is $question being 0? but the questions are outputed.. xD is my code for loop wrong? help pls. Quote Link to comment https://forums.phpfreaks.com/topic/247174-help-anyone/#findComment-1269524 Share on other sites More sharing options...
Pandemikk Posted September 15, 2011 Share Posted September 15, 2011 Well there could be many reasons. You have lots of things wrong with your code. Does $_POST['p'] actually have any data in it? Can you var_dump($questions) right before you do the for loop and paste what you get here? Quote Link to comment https://forums.phpfreaks.com/topic/247174-help-anyone/#findComment-1269541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.