PHP分别识别每条记录

时间:2012-04-27 08:41:01

标签: php

我一直在研究一个调查系统,该系统从数据库中读取记录(按“章节”排序的问题,你会看到我后面的意思),并为每条记录添加一个分数。问题存储在一个数组中。所以一条记录可以容纳五个问题。让我们深入研究代码

//echo the question list from the database

$query_questions = mysql_query("SELECT * FROM `vraag`");
while($result = mysql_fetch_array($query_questions))
{
//displaying the questions
echo "<br /><br /><b>";
echo $result['header']. "</b><br />";
echo $result['beschrijving']. "<br /><br />";
echo $result['onderwerp']. "<br />";

$aantal = $result['aantal_vragen'];
$id = $result['id'];
echo "<input type='hidden' id='number_of_questions' value=".$aantal." mousedown='calculate_score(this.value,0)'/>";
$array = unserialize($result['vraag']);
//retrieve the content from the array
for($i = 0; $i < $array; $i++)
{
        foreach($array as $value)
        {
            $array = $value;
            echo "<form><table><td>". $array ."</td>";
            echo "
            <td>1<input type='radio' name='vraag[]' value=1 onclick='calculate_score(".$aantal.",this.value)'/></td>
            <td>2<input type='radio' name='vraag[]' value=2 onclick='calculate_score(".$aantal.",this.value)'/></td>
            <td>3<input type='radio' name='vraag[]' value=3 onclick='calculate_score(".$aantal.",this.value)'/></td>
            <td>4<input type='radio' name='vraag[]' value=4 onclick='calculate_score(".$aantal.",this.value)'/></td>
            <td>5<input type='radio' name='vraag[]' value=5 onclick='calculate_score(".$aantal.",this.value)'/></td></table></form>";
        }
    echo "Uw totaal score is: <span id='total_score[]' ></span>";
    break;
}
}

出了什么问题,它使用查询的最后一条记录作为要计算的值,它忽略了第一条记录,第二条记录只记录了我希望它为每条记录分配一个分数的最后一条(第三条)记录很快就抽出来了。无线电元素的要点是使点数系统从1到5排列,如下所示:问题1-2-3-4-5 这是我的意图 [记录1] 得分

[记录2] 得分

[记录3] 得分

但目前它的工作原理如下 [记录1]

[记录2]

[记录3] 得分

我不完全确定我的断言命令,但我正在测试它是否能解决我的问题。对于得分标签的事情,我知道每次填充时我应该将数组增加一个,但我不知道语法,或者是否甚至可以这样做。 我是新手堆叠溢出所以原谅我任何遗失的标签或任何其他不可读或不愉快的东西。

1 个答案:

答案 0 :(得分:0)

外部for循环毫无意义。在内循环中,你在迭代它时重新分配$array(不知道它做了什么,但它只是完全错误)。

//Drop this code:
//for($i = 0; $i < $array; $i++)
//{
    foreach($array as $value)
    {
        $array = $value;   /* this is WRONG */
        /* ... */
    }
echo "Uw totaal score is: <span id='total_score[]' ></span>";
//break;   <--- don't need this either
//}