2012-04-27 29 views
1

我一直在研究一个调查系统,该调查系统从数据库中读取记录(按“章节”排序的问题,您将会看到我的意思),并为每条记录添加一个分数。问题存储在一个数组中。所以一个记录可以容纳例如五个问题。让我们深入到代码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] 得分

我不完全确定break命令,但是我测试它是否能解决我的问题。对于得分标签,我知道我应该在每次填充数组时增加一个数组,但我不知道语法或者甚至可能根本就不这样做。 我是新来堆栈溢出,所以原谅我任何缺失的标签或任何其他不可读或不愉快的眼睛。

回答

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 
//} 
+0

谢谢你指出,我很感激。 – 2012-04-27 09:02:01