Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在从数组检索数据库字段的表中创建新行?_Php_Arrays_If Statement_Html Table - Fatal编程技术网

Php 如何在从数组检索数据库字段的表中创建新行?

Php 如何在从数组检索数据库字段的表中创建新行?,php,arrays,if-statement,html-table,Php,Arrays,If Statement,Html Table,我有一个数组,它将从数据库中获取行。但是我想做的是在表中创建一个新的空行,如果$row['StudentAnswer']等于$row['AnswerContent'],那么就创建一个新字段(比如它被称为$row['StudentAnswerWeight'])=$row['Weight%]否则$row['StudentAnswerWeight']='0' 确定该表是查询的输出。数组包含查询中的字段。以此为例:$row['Answercontent']=Leeds(这是问题的正确答案。)$row['

我有一个数组,它将从数据库中获取行。但是我想做的是在表中创建一个新的空行,如果
$row['StudentAnswer']
等于
$row['AnswerContent']
,那么就创建一个新字段(比如它被称为
$row['StudentAnswerWeight'])
=
$row['Weight%]
否则
$row['StudentAnswerWeight']
='0'

确定该表是查询的输出。数组包含查询中的字段。以此为例:
$row['Answercontent']=Leeds
(这是问题的正确答案。)
$row['StudentAnswer']=Leeds
(这是学生为答案所做的。)
$row['weight%]
是正确答案的分数百分比。假设上面例子的答案=总分的5%。现在,正如上面的示例所示,学生的答案与正确答案匹配,这意味着在新字段中我想要
($row['StudentAnswerWeight']
显示答案的权重百分比,即5%。如果学生的答案是错误的,让我们假设学生在答案中输入“Manchester”。学生回答权重应为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>";
        }
      ?>

会话ID
问题编号
问题
正确答案
学生回答
正确答案权重
学生答案权重
学生证
这个怎么样:

...
<td>{$row['Weight%']}</td>
<td>{" . $row['StudentAnswer'] == $row['AnswerContent'] ? $row['Weight%'] : "0" . "}</td>
<td>{$row['StudentId']}</td>
...
。。。
{$row['Weight%']}
{“$row['StudentAnswer']==$row['AnswerContent']?$row['Weight%]:“0”。”
{$row['StudentId']}
...
我想这就是你想要做的,尽管我还是有点困惑。这样行吗

或者,如果你尝试一种更持久的方法,可能会更容易。它需要更多的语法,但使逻辑更容易判断

...
<td>{$row['Weight%']}</td>
<td>{"; 
if ($row['StudentAnswer'] == $row['AnswerContent'])
  echo "$row['Weight%']";
else
  echo "0";
echo "}</td>
<td>{$row['StudentId']}</td>
...
。。。
{$row['Weight%']}
{"; 
如果($row['StudentAnswer']=$row['AnswerContent']))
回显“$row['Weight%'”;
其他的
回声“0”;
回声“}
{$row['StudentId']}
...

不,该字段实际上被称为权重%。没有拼写错误。表格和字段工作正常并输出数据。但我需要添加一个新字段,我将仅在数组中调用StudentAnswerWieght,如果studentanswer和answercontent不匹配,则该字段将输入0,或者输入“权重%”中的数字来自StudentAnswerWeight字段下同一行的字段。希望这有意义。我想我现在已经修复了它。我已经有一段时间没有使用PHP了,但是“.”字符是用来连接字符串和代码的,如果我没有弄错的话,对吧?是的,“.”是用来连接字符串的。尽管我仍然不知道你最初的问题是什么答案不起作用。对不起。我们不能创建一个新的$row['…']并使用if语句吗。如果$row['AnswerContent']=$row['StudentAnswer'],则$row[''.']=$row['Weight%],否则$row['.']='0'。我一直在想这些问题,但我无法让它工作,我希望你们能弄明白:)你们想把它插回你们的mysql表吗?我以为你只是想把它打印在一个表格单元格的页面上?学会。更好的办法是雇佣一名程序员。