2011-10-19 62 views
0

对不起,我不得不再次提交这个问题,因为我几乎没有任何意见,因为它是一个旧的职位。尽管如此,我仍然绝望。请帮帮我。如何在数组中创建一个新字段?

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

例如:$ row ['Answercontent'] = Leeds(这是问题的正确答案。)$ row ['StudentAnswer'] = Leeds(这是学生为答案所做的) $ row ['weight%']是正确答案的标记百分比。可以说,上面例子的答案=总分的5%。现在,如上例所示,学生的答案与正确答案匹配,这意味着在我想创建的新字段中(让我们称它为$ row ['StudentAnswerWeight'])来显示答案的百分比是5%,如果学生的答案错了,可以让学生把他/她的答案写成'曼彻斯特',学生的答案应该= 0%,因为答案是错误的,我希望你现在明白我想达到的目标。

我们不能创建一个新的$行['...']($ row ['StudentAnswerWeight'是新的$行['...']将被调用)并使用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>"; 
    } 
    ?> 

谢谢

+0

这难道不是工作? echo($ row ['StudentAnswer'== $ row ['AnswerContent'])? $ row ['Weight%']:'0' – bozdoz

回答

1

echo前补充一点:

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

,远离{$row['StudentAnswer'] = $row['AnswerContent']}的。

+0

你的意思是替换,而不是摆脱正确?这个答案应该工作。 – bozdoz

+0

这工作,非常感谢你:) – BruceyBandit

1

试试这个:在你的榜样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>"; 
    } 
    ?> 
相关问题