Radio button rating system for various categories of questions that add a score at the end [closed]

StackOverflow https://stackoverflow.com/questions/9346441

Вопрос

I have a php page that has a few HTML questions on it, each question has a "type" and you answer that question on a scale of 1-5, In the example below there would be 3 question types: A, B, & C. How can I add the TOTAL score for each question "type"(score is made from the radio buttons for the 1-5 scale below each question) and then have those total scores stored as PHP variables?

Here is the HTML code, I have NO CLUE where to start but I need to make this for a school event we are doing soon and I don't wanna let them down :) Haha! Thanks for all the help guys, sorry I know so little about HTML forms! Anyways, here's the HTML code, the php can go anywhere I haven't written it yet /: haha:

Type A rating:
<br>
<form action="" method="post">
1
<input type="radio" name="select" value="1">
<input type="radio" name="select" value="2">
<input type="radio" name="select" value="3">
<input type="radio" name="select" value="4">
<input type="radio" name="select" value="5">
5
</form>
<br><br>
Type B rating:
<br>
<form action="" method="post">
1
<input type="radio" name="select" value="1">
<input type="radio" name="select" value="2">
<input type="radio" name="select" value="3">
<input type="radio" name="select" value="4">
<input type="radio" name="select" value="5">
5
</form>
<br><br>
Type A rating:
<br>
<form action="" method="post">
1
<input type="radio" name="select" value="1">
<input type="radio" name="select" value="2">
<input type="radio" name="select" value="3">
<input type="radio" name="select" value="4">
<input type="radio" name="select" value="5">
5
</form>
<br><br>
Type C rating:
<br>
<form action="" method="post">
1
<input type="radio" name="select" value="1">
<input type="radio" name="select" value="2">
<input type="radio" name="select" value="3">
<input type="radio" name="select" value="4">
<input type="radio" name="select" value="5">
5
</form>
<br><br>
Type A rating:
<br>
<form action="" method="post">
1
<input type="radio" name="select" value="1">
<input type="radio" name="select" value="2">
<input type="radio" name="select" value="3">
<input type="radio" name="select" value="4">
<input type="radio" name="select" value="5">
5
</form>
<br><br>
Type B rating:
<br>
<form action="" method="post">
1
<input type="radio" name="select" value="1">
<input type="radio" name="select" value="2">
<input type="radio" name="select" value="3">
<input type="radio" name="select" value="4">
<input type="radio" name="select" value="5">
5
</form>
<br><br>
Type B rating:
<br>
<form action="" method="post">
1
<input type="radio" name="select" value="1">
<input type="radio" name="select" value="2">
<input type="radio" name="select" value="3">
<input type="radio" name="select" value="4">
<input type="radio" name="select" value="5">
5
</form>
<br><br>

<input type="submit" name="Sum The Ratings" value="Vote">
Это было полезно?

Решение

Create a big form with all the questions in it, then for each question, set the name of the input with a keyword different for each questions. When you will treat your form, you will get with $_POST['name'] the value selected, just add them and make a ration or wathever you wan't

<?php
$name_cat_a = "A_";
$name_cat_b = "B_";
$cat_a_quest = array("Question A1", "Question A2");
$cat_b_quest = array("Question B1", "Question B2");
if(!isset($_POST[submit])){
echo '<form action="test.php" method=post>';
echo 'Type A rating:';
echo '<br />';
$ind = 0;
foreach($cat_a_quest as $question){
    echo $question;
    echo '<br>';
    $name = $name_cat_a . $ind;
    $ind ++;
    for($i=0;$i<5;$i++){
    echo '<input type="radio" name="'.$name.'" value="'.($i+1).'" />'.($i+1) ;
    }
    echo '<br />';
}
echo 'Type B rating:';
echo '<br />';
$ind = 0;
foreach($cat_b_quest as $question){
    echo $question;
    echo '<br>';
    $name = $name_cat_b . $ind;
    $ind ++;
    for($i=0;$i<5;$i++){
    echo '<input type="radio" name="'.$name.'" value="'.($i+1).'" />'.($i+1);
    }
    echo '<br />';
}
echo '<input type="hidden" name="submit" value="1" />';
echo '<input type="submit" name="Sum The Ratings" value="Vote">';
echo '</form>';
}
else{
$moyen_a = 0;
$moyen_b = 0;
$nmb_ques_a = count($cat_a_quest);
$nmb_ques_b = count($cat_b_quest);
for($i=0; $i<$nmb_ques_a; $i++){
     $moyen_a = $moyen_a + intval($_POST['A_'.$i]);
}
$moyen_a = $moyen_a / $nmb_ques_a;
for($i=0; $i<$nmb_ques_b; $i++){
     $moyen_b = $moyen_b + intval($_POST['B_'.$i]);
}
$moyen_b = $moyen_b / $nmb_ques_b;


echo 'A:'.$moyen_a.'<br />';
echo 'B:'.$moyen_b.'<br />';
}
?>

There i have explicitely name cat_a and cat_b but you can put all your cat in an array, then you loop into it, in this loop, you loop while ther is questions, and then you loop for the 5 answers

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top