Forum Moderators: coopster

Message Too Old, No Replies

loop/function help

         

kristie380

7:35 pm on Mar 11, 2008 (gmt 0)

10+ Year Member



I am trying to calculate the results from a quiz to tell the user how many answers they got right and what their percentage is. I have tried a couple of different methods but I am fairly new to this type of scripting and I can't seem to get something to work. Does anyone have any suggestions? Here is what I have so far...

<?php

$num = 1

if ($_POST[1] == "d") { $num }

if ($_POST[2] == "c") { $num }

if ($_POST[3] == "a") { $num }

if ($_POST[4] == "c") { $num }

if ($_POST[5] == "a") { $num }

if ($_POST[6] == "b") { $num }

if ($_POST[7] == "d") { $num }

if ($_POST[8] == "b") { $num }

if ($_POST[9] == "c") { $num }

if ($_POST[10] == "a") { $num }

echo "You got $num++ out of 10 correct (xx%)!";

?>

Demaestro

7:42 pm on Mar 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Using your example the following should work.

<?php

$num = 0

if ($_POST[1] == "d") { $num = $num + 1 }

if ($_POST[2] == "c") { $num = $num + 1 }

if ($_POST[3] == "a") { $num = $num + 1}

if ($_POST[4] == "c") { $num = $num + 1}

if ($_POST[5] == "a") { $num = $num + 1}

if ($_POST[6] == "b") { $num = $num + 1}

if ($_POST[7] == "d") { $num = $num + 1}

if ($_POST[8] == "b") { $num = $num + 1}

if ($_POST[9] == "c") { $num = $num + 1}

if ($_POST[10] == "a") { $num = $num + 1}

echo "You got $num out of 10 correct (xx%)!";

?>