如何在表中从数组中检索数据库字段中创建新行? [英] How do I make a new row in a table which retrieves database fields from an array?
问题描述
我有一个数组,它将从数据库中获取行。但我想要做的是在表中创建一个新的空行,其中if $ row ['StudentAnswer']
等于 $ row ['AnswerContent ']
然后是新字段(比如说它叫 $ row ['StudentAnswerWeight'])
= $ row ['Weight %']
else $ row ['StudentAnswerWeight']
='0'。
I have an array which will fetch rows from the database. But what I want to do is to create a new empty row in the table where if $row['StudentAnswer']
equals $row['AnswerContent']
then new field (lets say its called $row['StudentAnswerWeight'])
= $row['Weight%']
else $row['StudentAnswerWeight']
= '0'.
确定表是查询的输出。该数组包含查询中的字段。看一下这个例子: $ row ['Answercontent'] = Leeds
(这是问题的正确答案。) $ row [' StudentAnswer'] =利兹
(这是学生提出的答案。) $ row ['weight%']
是百分比正确答案的标记。让我们说上面例子的答案=总分数的5%。现在,如上例所示,学生的答案与正确答案相符,这意味着在新字段中我想要($ row ['StudentAnswerWeight']
来显示权重%答案的百分比为5%。如果学生得到答案错误,请让学生说出他们的答案'曼彻斯特'。学生的体重应该= 0%,因为答案是错误的。我希望你现在明白我想要的实现。
OK the table is an output from a query. The array contains fields from the query. Look at this for an example: $row['Answercontent'] = Leeds
(This is the correct answer for the question.) $row['StudentAnswer'] = Leeds
(This is what the student has put for the answer.) The $row['weight%']
is the percentage of the mark for the correct answer. Let's say that the answer for the above example = 5% of the total marks. Now as the above example shows that the student's answer has matched the correct answer it means that in the new field I want ($row['StudentAnswerWeight']
to display the weight% percentage of the answer which is 5%. If the student got the answer wrong by lets say the Student put in their answer 'Manchester'. The studentanswerweight should = 0% as that the answer is wrong. I hope you now understand what I want to achieve.
我怎么能这样做因为我似乎无法做到这一点?
How can I do this because I can't seem to get it right?
以下是PHP代码中的数组和表中的字段:
Below is the array and the fields in the table in php code:
<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>
...
认为这是你想要做的,虽然我是还是有点困惑。这有用吗?
Think that's what you're trying to do, although I'm still a bit confused. Does that work?
或者如果你尝试更多的方法可能会更容易。这需要更多的语法,但使逻辑更容易解决...
Or perhaps it would be easier if you tried a more drawn out approach. It takes more syntax but makes the logic a little bit easier to decifer...
...
<td>{$row['Weight%']}</td>
<td>{";
if ($row['StudentAnswer'] == $row['AnswerContent'])
echo "$row['Weight%']";
else
echo "0";
echo "}</td>
<td>{$row['StudentId']}</td>
...
这篇关于如何在表中从数组中检索数据库字段中创建新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!