How to count variables in php?

Discussion in 'PHP' started by eritrea1, Jun 26, 2012.

  1. #1
    Hi Everyone.
    Ok, basically i am trying to create a PHP script in which, a visitor gets asked a couple of questions and all he/she has to do is, to click on a set of check-boxes/drop-downs to answer them, and finally submit to check the result of the quiz.

    Now, here is the trick... without using SQL, how can you count the number of answers which the user has answered correctly, and display something like:
    You have got 3/4/5... answers right.

    I can use something like this:

    STORING THE ANSWERS;

    
    <?php 
    
    if(isset($_POST['submit'])) 
    {
      $quiz1 =$_POST['three']; 
    }
     
    ?>
    
    Code (markup):

    THE TEST FORM..

    
    How many colors does the US flag has?<br/>
    <form method="post" action="index.php">
    <select name="flag">
      <option value="">Choose Catagory</option>
      <option value="one">one</option>
      <option value="two">Politics</option>
      <option value="three">two</option>
      <option value="four">four</option>
      <option value="five">five</option>
      <option value="six">six</option>
      <option value="seven">seven</option>
    <input type="submit" name="submit" value="Check Test"> 
    </select>
    
    Code (markup):
    The IF / ELSE STATEMENT
    
    <?php  
    
    if(isset($_POST['flag'] == 'three'))
    
    {
    echo ' You have got 1 answer right ';
    }
    
    else 
    {
    echo ' You have got 0 answer right ';
    }
    
    
    ?> 
    
    
    Code (markup):


    Now, the only problem resting is, how do you count how many questions the person has answered right IF there were to be more that 5 questions.
     
    eritrea1, Jun 26, 2012 IP
  2. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #2
    First, you wouldn't use SQL for validation. I'm not sure why you assumed that may of been an option.

    Second, if you just want to verify answers from with in your script you should store the answers in an array. Then check the submitted values to the array.

    For instance if there is a question "What is Lady Gagas favorite fruits?" and the checkbox choices are: Apples, Oranges, Bananas, and Strawberries. If the only acceptable answers are Bananas and Apples then try something like this:


    
    $answersWrong = 0; // Nothing is wrong until we start validation
    
    $fruitAnswers = array("Bananas", "Apples"); // Correct answers for lady gagas favorite fruits
    
    $fruitsChosen = $_POST['fruits']; // I assume your checkbox name is fruits
    
    if (!isset($fruitsChosen))
    { // Visitor didn't bother to answer question
       $answersWrong = 1; // So the answer is wrong or you can instruct the visitor to answer it
    } else
    {
         for ($i=0; $i < count($fruitsChosen); $i++)
         { // Loop through all fruits chosen
             if (!in_array($fruitsChosen[$i], $fruitAnswers))
             { // Uh oh.. this item is NOT in the fruit answers
               $answersWrong = 1; // 1 question wrong
             }
         }
    }
    
    print "There were " . $answersWrong . " answers wrong\n";
    
    PHP:
    This is to just give you an idea. I didn't test this example. I wrote it in this tiny textarea. There may even be a typo in there...but you will get the drift on how to loop through submitted values from checkboxes.
     
    NetStar, Jun 26, 2012 IP
  3. eritrea1

    eritrea1 Active Member

    Messages:
    182
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    70
    #3
    Thanks NetStar

    But I tried your code then, and then today but it is not working.
    Could you probably give me a link, and I will try to learn it myself, that is much better.
    I just don't know where to get a link, that would help me solve this problem.
     
    eritrea1, Aug 4, 2012 IP
  4. webshore88

    webshore88 Well-Known Member

    Messages:
    131
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #4
    If you submitting all questions together then use this trick:
    store all answers in an array as
    $right_ans = 0;$answer[1] = "ans1";$answer[2] = "ans2";$answer[3] = "ans3";
    PHP:
    then check if answer is matching. If yes then increase answer value or just leave to be as:
    
    $answer[1] == $_POST['ans1']){
        $right_ans++;
    }
    if($answer[2] == $_POST['ans2']){
        $right_ans++;
    }
    
    PHP:
    at the end you will get total right answers in $right_ans;


    else if your questions are displayed one by one then increase your $right_ans and store it in session as:
    $answer[1] = "ans1";$answer[2] = "ans2";$answer[3] = "ans3";
    PHP:
    then check if answer is matching. If yes then increase answer value or just leave to be as:
    
    session_start();
    if(!isset($_SESSION['right_ans'])){
        $_SESSION['right_ans'] = 0;
    }
    $right_ans = $_SESSION['right_ans'];
    
    
    if($answer[1] == $_POST['ans1']){
          $right_ans++;
          $_SESSION['right_ans'] = $right_ans;
    }
    if($answer[2] == $_POST){
        $right_ans++;
        $_SESSION['right_ans'] = $right_ans;
    }
    
    PHP:
    at the end you will get total right answers in $right_ans;


    and good luck :cool:
     
    webshore88, Aug 4, 2012 IP