如何在数组中创建一个新字段?

发布于 2024-12-10 13:04:39 字数 1696 浏览 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']) 来显示答案的 wieght% 百分比如果学生网络的答案是错误的,那么学生的答案权重应该 = 0%,因为我希望你现在明白我想要的是什么。 我们不能

创建一个新的 $row['...']($row['StudentAnswerWeight' 是新的 $row['...'] 将被调用的内容)并使用 if 语句。 $row['AnswerContent'] = $row['StudentAnswer'] 然后 $row['...'] = $row['Weight%'] else $row['...'] = '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['StudentAnswer'] = $row['AnswerContent']}</td> 
  <td>{$row['StudentId']}</td>
  </tr>";
    }
  ?>

谢谢

Sorry I have had to submit this question again because I have had hardly any views as it is an old post. I am desperate though for this to be answered. Please help me.

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

For 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. Lets 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 which I want to create (lets call it ($row['StudentAnswerWeight']) to display the wieght% percentage of the answer which is 5%. If the studnet got the answer wrong by lets say the Student put in his/her answer 'Manchester'. The studentanswerweight should = 0% as that the answer is wrong. I hope you now understand what I want to acheive.

Can't we make a new $row['...']($row['StudentAnswerWeight' is what the new $row['...'] will be called) and use an if statement. If $row['AnswerContent'] = $row['StudentAnswer'] then $row['...'] = $row['Weight%'] else $row['...'] = '0'. I was thinking along those lines but I could not get it to work, I hope you guys can figure it out :)

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['StudentAnswer'] = $row['AnswerContent']}</td> 
  <td>{$row['StudentId']}</td>
  </tr>";
    }
  ?>

Thank You

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

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

发布评论

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

评论(2

淡莣 2024-12-17 13:04:39

echo 之前添加此内容:

if ( $row['StudentAnswer'] == $row['AnswerContent'] ) {
  $row['StudentAnswerWeight'] = $row['Weight%'];
} else {
  $row['StudentAnswerWeight'] = '0';
}

并删除此 {$row['StudentAnswer'] = $row['AnswerContent']}

Add this before echo:

if ( $row['StudentAnswer'] == $row['AnswerContent'] ) {
  $row['StudentAnswerWeight'] = $row['Weight%'];
} else {
  $row['StudentAnswerWeight'] = '0';
}

And get rid of this {$row['StudentAnswer'] = $row['AnswerContent']}.

枫以 2024-12-17 13:04:39

试试这个: echo ($row['StudentAnswer']==$row['AnswerContent']) ? $row['Weight%'] : '0'

在您的示例中:

<?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>";
echo ($row['StudentAnswer']==$row['AnswerContent']) ? $row['Weight%'] : '0';
echo "</td><td>{$row['StudentId']}</td>
  </tr>";
    }
  ?>

Try this: echo ($row['StudentAnswer']==$row['AnswerContent']) ? $row['Weight%'] : '0'

in your example:

<?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>";
echo ($row['StudentAnswer']==$row['AnswerContent']) ? $row['Weight%'] : '0';
echo "</td><td>{$row['StudentId']}</td>
  </tr>";
    }
  ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文