2012-02-19 5 views
0

몇 가지 HTML 질문이있는 PHP 페이지가 있습니다. 각 질문에는 "유형"이 있으며 사용자가 1의 척도로 해당 질문에 답변합니다 -5, 아래의 예에서는 A, B, &의 3 가지 질문 유형이 있습니다. 각 질문 "유형"에 대해 TOTAL 점수를 어떻게 추가 할 수 있습니까? (점수는 각 아래 1-5 눈금의 라디오 버튼에서 만들어집니다 질문) 그런 다음 총 점수를 PHP 변수로 저장합니까?끝 부분에 점수를 추가하는 다양한 범주의 질문에 대한 라디오 버튼 등급 시스템

여기에 HTML 코드가 있습니다. 어디에서 시작할 지 알 수 없지만 곧 학교 행사에 참석해야합니다. 하차하고 싶지 않습니다. 하하! 모든 도움을 주셔서 감사합니다, 미안 HTML 양식에 대해 거의 알지 못합니다! 어쨌든, 여기에 HTML 코드는 PHP가 어디든 갈 수의 I/아직 작성하지 않은,이다 : 하하 :

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"> 
+0

질문이 닫힌 이유를 이해하려면 [faq] 및 [ask] –

답변

2

는 각 질문에 대한 다음, 그것의 모든 질문에 큰 폼을 만들고, 이름을 설정 각 질문마다 다른 키워드로 입력하십시오. 당신이 당신의 양식을 취급 할 때, 당신은 $ _POST로 얻을 것이다 [ '이름'] 선택한 값, 당신이

<?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 />'; 
} 
?> 

내가 cat_a 및 cat_b의 이름을 명시 적으로 한 wan't wathever을 추가하고 배급을하거나 하지만 배열에 모든 고양이를 넣을 수 있습니다.이 루프에서 루프가 반복되는 동안 질문이 반복되고 5 개의 대답이 반복됩니다.

+0

내가 시도 할 때마다 값 0에 대해 0을 반환합니다.이 코드는 점수를 평균화하지 않습니까? A 타입의 모든 질문의 합계와 B 타입의 모든 질문의 합계가 필요합니다. A 타입의 여러 질문 등이 있습니다. –

+0

Nevermind 내가 잘못 입력 했으므로 코드가 작동하지 않습니다. 그러나 그것은 내가 찾고있는 것이 아닙니다. 수동으로 각 질문의 이름을 지정하고 모든 값을 추가 할 수 있지만 문제는 모든 하하에 약 95 개의 질문이 있다는 것입니다. 최악의 시나리오라면 30 분 정도 걸리 겠지만 더 빠른 방법이 있는지 궁금해합니다. –

+0

난 그냥 코드를 편집하지만 문제가되지 않는 데이터베이스에서 질문을 취할 수 있습니다. 쿼리의 수에 따라 루프를 만듭니다. – lpostula