如何在表中创建一个新行以从数组中检索数据库字段?

发布于 2024-12-11 05:27:25 字数 1628 浏览 0 评论 0原文

我有一个将从数据库中获取行的数组。但我想要做的是在表中创建一个新的空行,其中如果 $row['StudentAnswer'] 等于 $row['AnswerContent'] 则新字段(假设它名为 $row['StudentAnswerWeight']) = $row['Weight%'] else $row['StudentAnswerWeight'] = '0'。

好的,该表是查询的输出。该数组包含查询中的字段。看一下这个例子:$row['Answercontent'] = Leeds(这是问题的正确答案。)$row['StudentAnswer'] = Leeds (这是学生输入的答案。)$row['weight%'] 是正确答案分数的百分比。假设上例的答案 = 总分的 5%。现在,如上面的示例所示,学生的答案与正确答案相匹配,这意味着在新字段中我希望 ($row['StudentAnswerWeight'] 显示答案的权重百分比,即 = 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>";
        }
      ?>

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'.

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?

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>";
        }
      ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北座城市 2024-12-18 05:27:25

怎么样:

...
<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>
...

How about this:

...
<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>
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文