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
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:
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...
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.
Thanks for your kind help greatlogix & derek34. Will try it out and see. +rep added to both for your kind help. Thanks