Need small help in PHP

Discussion in 'PHP' started by life31, Nov 13, 2007.

  1. #1
    Wanted to create a small questioneer with 5 to 10 questions.
    The person selects one out of three option from each qustion.
    Something like this

    Q1 A B C
    Q2 A B C
    Q3 A B C
    Q4 A B C
    Q5 A B C

    Finally what i want as a result is how many SELECTED A , B & C got

    Like A=2
    B=1
    C=2


    And also the top two should be displayed.

    Like A&C

    HOW do i do it???
    Just need the PHP part there.... Do i use a for loop or an array to do that.

    I was trying for using for loop cos even incase if i increase the no of questions to 10 0r 20 i wont have to make much changes in the script.


    Can any one help.... Please...

    Thanks
     
    life31, Nov 13, 2007 IP
  2. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #2
    Try this. It may help you.
    <form method="post">
    <? for($i=1; $i<=5; $i++){ ?>
    <strong>Question <?= $i ?></strong><br>
    <input type="radio" name="q<?= $i ?>" value="1" />A
    <input type="radio" name="q<?= $i ?>" value="2" />B
    <input type="radio" name="q<?= $i ?>" value="3" />C
    <Br />
    <? } ?>
    <input type="submit">
    </form>
    <?
    foreach($_POST as $key => $value)
    echo $key. '='.$value. '<br>';
    ?>
    PHP:
     
    greatlogix, Nov 13, 2007 IP
    life31 likes this.
  3. derek34

    derek34 Guest

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I would use an IF statement and then count the number of times a, b, or c was selected.
    name your radio buttons a, b, c and then do something like this:
    
    <?php
    $aselection = $_GET['a'];
    //$bselection etc.
    if(isset($aselection)){
    $acounter = 0;
    $acounter++;
    echo "a was selected" . $acounter . "times."
    }
    
    elseif (){ //then do the same for $bselection and so on...
    
    }
    ?>
    
    PHP:
    ++ will increase the value of whatever variable you put in front of it.
    So every time the script finds a radio button that was selected it will count that as one.
    Hope that makes sense and I hope that helps...
     
    derek34, Nov 13, 2007 IP
    life31 likes this.
  4. derek34

    derek34 Guest

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    oops it should be like this instead:

    <?php
    $aselection = $_GET['a'];
    //$bselection etc.
    if($aselection !== "")){
    $acounter = 0;
    $acounter++;
    echo "a was selected" . $acounter . "times."
    }
    
    elseif (){ //then do the same for $bselection and so on...
    
    }
    ?>
    PHP:
    otherwise it would have detected that the radio button was set every time. This was it will only count it if there is something entered in $_GET.
     
    derek34, Nov 13, 2007 IP
  5. life31

    life31 Active Member

    Messages:
    1,024
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    78
    #5
    Thanks for your kind help greatlogix & derek34.
    Will try it out and see.

    +rep added to both for your kind help. Thanks
     
    life31, Nov 14, 2007 IP