2011-10-19 63 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>"; 
    } 
    ?> 
相關問題