2

Working on a quiz website where when users submits their answers $perQuestionScore equals true when the answer is correct but false when it is wrong. I'm trying to find the instances where $perQuestionScore equals true in other to total the score but it doesn't seem to work.

My code looks like below

   <?php    

$perQuestionScore = 0;

if (isset($_POST['grader'])) {

if(isset($_POST[$chosen]))

{
        $choice= $_POST[$chosen];

    if (strpos($choice, $correctOne) !== false) {

        $perQuestionScore++;

    echo $_POST[$chosen] . "" . " is the correct answer";

} elseif (strpos($choice, $correctOne) == false) { echo $_POST[$chosen] . "" . " is the Wrong answer";

} else  {

echo "You did not choose an answer"; { 



}
}
}               
}
     }
     echo "<input id=grader' type='submit' name='grader' value='Grade Quiz'>" . "</form>";
                    echo $perQuestionScore * 10;

} 





$conn->close();
?> 
Sleek Geek
  • 4,638
  • 3
  • 27
  • 42
  • your mixing assignment with comparison. –  Feb 21 '16 at 23:25
  • ok, reopened and answered. –  Feb 21 '16 at 23:29
  • `echo count($perQuestionScore).true * 10;` your clearly mixing in js syntax here –  Feb 21 '16 at 23:34
  • count expects an array\object, your feeding it a string now. I don't see why you keep editing the question. its confusing –  Feb 21 '16 at 23:42
  • where does `$chosen` come from? should `$_POST[$chosen]` be `$_POST[chosen]` ? –  Feb 21 '16 at 23:57
  • Values of user selections. It shows wrong and right answers perfectly! – Sleek Geek Feb 21 '16 at 23:59
  • if its from a form it going to be `$_POST[chosen]` not `$_POST[$chosen]` –  Feb 22 '16 at 00:00
  • like that because I have $chosen= $row["id"]; Using the id as the element "name" – Sleek Geek Feb 22 '16 at 00:02
  • here is a 'working' demo based on your code http://ideone.com/FumGih. nothing has been changed other than the fake values, so i suggest that is where you start debugging. `var_dump($_POST);` etc –  Feb 22 '16 at 00:10
  • @SleekGeek if Dagon's example doesn't help, could you fix the curly braces? Seems like there are too many or not enough. (I'm looking at `echo "You did not choose an answer"; { }` but more besides) –  Feb 22 '16 at 00:22

1 Answers1

0

I suggest simplifying it to this:

<?php 


$perQuestionScore = 0; //initialize var

if ($choice == $correct) {
    $perQuestionScore++; //add 1 if correct
} 


echo $perQuestionScore * 10; //i guess you want it times 10