循环会话总分

时间:2014-12-08 06:04:44

标签: php

我正在使用会话将值传递到下一页,这是成功的问题是我有25个问题要这样做,所以我在想使用循环来传递所有问题,我确实让它工作我将如何这样做?

HTML

<html>

<head>
</head> 

<form method="post" action="data.php">
<table>
<tr>
                <td><label> I am a high achiever. </label></td>
                <td><input type="radio" name="D1" value="1"> 1 </td>
                <td><input type="radio" name="D1" value="2"> 2 </td>
                <td><input type="radio" name="D1" value="3"> 3 </td>
                <td><input type="radio" name="D1" value="4"> 4 </td>
                <td><input type="radio" name="D1" value="5"> 5 </td>
                <td><input type="radio" name="D1" value="6"> 6 </td>
                <td><input type="radio" name="D1" value="7"> 7 </td>
                <td><input type="radio" name="D1" value="8"> 8 </td>
                <td><input type="radio" name="D1" value="9"> 9 </td>
                <td><input type="radio" name="D1" value="10"> 10 </td>
            </tr><!-- 11 -->  
<tr>
                <td><label> I am strong willed. </label></td>
                <td><input type="radio" name="D2" value="1"> 1 </td>
                <td><input type="radio" name="D2" value="2"> 2 </td>
                <td><input type="radio" name="D2" value="3"> 3 </td>
                <td><input type="radio" name="D2" value="4"> 4 </td>
                <td><input type="radio" name="D2" value="5"> 5 </td>
                <td><input type="radio" name="D2" value="6"> 6 </td>
                <td><input type="radio" name="D2" value="7"> 7 </td>
                <td><input type="radio" name="D2" value="8"> 8 </td>
                <td><input type="radio" name="D2" value="9"> 9 </td>
                <td><input type="radio" name="D2" value="10"> 10 </td>
            </tr><!-- 15 -->
<tr>
<td colspan=2>
<div align="center"><input type="submit" name="submit" value="Score my test"></div>
</td>
</tr>
</table>
</form>
   </html>

PHP

<?php
 // i need to add a loop here i++ but i didt get it to work i got 25 question 
session_start();

$_SESSION['D1'] = $_POST['D1'];
$_SESSION['D2'] = $_POST['D2'];

$Dpoint = $_SESSION['D1'] + $_SESSION['D2'];

?>

<html>

<body>
    <p><?php echo("{$Dpoint}"."<br />");?></p>
</body>

</html>

问题是我得到D25问题添加总点我不想每个都做每个做任何一个知道如何在PHP上循环会话?喜欢的东西我&lt; 25; i ++,我确实让它继续工作。我不希望每个会话都加分。需要帮助。

2 个答案:

答案 0 :(得分:0)

您最好将输入名称设为数组:<input type="text" name="question[]">question[1]question[2] ...
与提交时相比,使用for / foreach循环来获取所有值:

$questions = !empty($_POST['question']) ? $_POST['question'] : false;
$total = 0;

if ($questions) {
    foreach ($questions as $i => $question) {
        $total += intval($question);
        //Additional if needed variables in session:
        $_SESSION['question'][$i] = intval($question);
    }
}

答案 1 :(得分:-1)

尝试以下代码

<tr>
                <td><label> I am a high achiever. </label></td>
                <td><input type="radio" name="points[D1]" value="1"> 1 </td>
                <td><input type="radio" name="points[D1]" value="2"> 2 </td>
                <td><input type="radio" name="points[D1]" value="3"> 3 </td>
            </tr><!-- 11 -->  
<tr>
                <td><label> I am strong willed. </label></td>
                <td><input type="radio" name="points[D2]" value="1"> 1 </td>
                <td><input type="radio" name="points[D2]" value="2"> 2 </td>
                <td><input type="radio" name="D2" value="3"> 3 </td>
            </tr><!-- 15 -->
<tr>
PHP中的

  $Dpoint = 0;
    for($i=1;$<=count($_POST['points']);$i++){
      $_SESSION["D$i"] = $_POST["D$i"];
       $Dpoint += $_SESSION["D$i"];
    }

或者你可以这样做

 <tr>
                    <td><label> I am a high achiever. </label></td>
                    <td><input type="radio" name="D1" value="1"> 1 </td>
                    <td><input type="radio" name="D1" value="2"> 2 </td>
                    <td><input type="radio" name="D1" value="3"> 3 </td>
                </tr><!-- 11 -->  
    <tr>
                    <td><label> I am strong willed. </label></td>
                    <td><input type="radio" name="D2" value="1"> 1 </td>
                    <td><input type="radio" name="D2" value="2"> 2 </td>
                    <td><input type="radio" name="D2" value="3"> 3 </td>
                </tr><!-- 15 -->
    <tr>

在PHP中

$_SESSION['points']['D1'] = $_POST['D1'];
$_SESSION['points']['D2'] = $_POST['D2'];
$Dpoint = 0;
for($i=1;$<=count($_SESSION['points']);$i++){
       $Dpoint += $_SESSION['points']["D$i"];
    }