我有一个数组,它将从数据库中提取行。但我想做的是在表中创建一个新的空行,如果$row['StudentAnswer']
等于$row['AnswerContent']
,那么新字段(比如说它被称为$row['StudentAnswerWeight'])
=$row['Weight%']
,否则$row['StudentAnswerWeight']
='0'(
确定,该表是查询的输出。数组包含查询中的字段。举个例子:$row['Answercontent'] = Leeds
(这是问题的正确答案。($row['StudentAnswer'] = Leeds
(这是学生对答案的回答。($row['weight%']
是正确答案的分数百分比。假设上面例子的答案=总分的5%。现在,由于上例显示学生的答案与正确答案匹配,这意味着在新字段中,我希望($row['StudentAnswerWeight']
显示答案的权重百分比,即5%。如果学生的答案是错的,比如说学生在他们的答案中输入"曼彻斯特"。学生的权重应该为0%,因为答案是错误的。我希望你现在明白我想要实现什么。
我怎么能这样做,因为我似乎做不好?
下面是php代码中的数组和表中的字段:
<table border='1'>
<tr>
<th>Session ID</th>
<th>Question Number</th>
<th>Question</th>
<th>Correct Answer</th>
<th>StudentAnswer</th>
<th>Correct Answer Weight</th>
<th>Student Answer Weight</th>
<th>Student ID</th>
</tr>
<?php
while ($row = mysql_fetch_array($result)) {
echo "
<tr>
<td>{$row['SessionId']}</td>
<td>{$row['QuestionNo']}</td>
<td>{$row['QuestionContent']}</td>
<td>{$row['AnswerContent']}</td>
<td>{$row['StudentAnswer']} </td>
<td>{$row['Weight%']}</td>
<td>{$row['StudentAnswerWeight']}</td>
<td>{$row['StudentId']}</td>
</tr>";
}
?>
这个怎么样:
...
<td>{$row['Weight%']}</td>
<td>{" . $row['StudentAnswer'] == $row['AnswerContent'] ? $row['Weight%'] : "0" . "}</td>
<td>{$row['StudentId']}</td>
...
尽管我还是有点困惑,但你想这就是你想要做的。这样行吗?
或者,如果你尝试一种更持久的方法,也许会更容易。它需要更多的语法,但使逻辑更容易判断。。。
...
<td>{$row['Weight%']}</td>
<td>{";
if ($row['StudentAnswer'] == $row['AnswerContent'])
echo "$row['Weight%']";
else
echo "0";
echo "}</td>
<td>{$row['StudentId']}</td>
...