2012-11-28 95 views
0

我正在嘗試工作我的頭圍繞此,我使用以下代碼來檢查測驗的答案並根據結果輸出正確或錯誤並且如果答案字段是黑色的(僅來自反饋表單),則顯示不同的消息。 我無法在這種情況下應用php count函數,儘管如此,我試圖讓它做到這一點,它計算正確和不正確答案的數量,並將兩者加在一起,然後我可以計算出%得分從那。使用php的count()命令來計算if語句的結果

 <?php 
// Make a MySQL Connection 
// Construct our join query 
$query = "SELECT * FROM itsnb_chronoforms_data_answerquiz a, itsnb_chronoforms_data_createquestions 
q WHERE a.quizID='$quizID' AND a.userID='$userID' and q.quizID=a.quizID and 
a.questionID = q.questionID ORDER BY a.cf_id ASC" or die("MySQL ERROR: ".mysql_error()); 
$result = mysql_query($query) or die(mysql_error()); 

// Print out the contents of each row into a table 
while($row = mysql_fetch_array($result)){ 
if ($row['correctanswer'] == ''){echo '<tr><td style="color:blue;">Thankyou for your feedback</td></tr>';} 
elseif ($row['correctanswer'] == $row['quizselectanswer']){ 
echo '<tr><td style="font-weight:bold; color:green;">CORRECT</td></tr>';} 
else {echo '<tr><td style="font-weight:bold; color:red;">INCORRECT</td></tr>'; 
}} 
?> 

四中這個例子,並一直在努力解決如何在工作,但想不出如何與它進行計算的,如果語句的結果?

<?php 
$people = array("Peter", "Joe", "Glenn", "Cleveland"); 
$result = count($people); 

echo $result; 
?> 
+0

刪除多餘的whitspaces在你的代碼 –

+0

@RhoHappy如果有任何的代碼需要更多的空白。 – lynks

+0

爲什麼你需要使用count(),有很多解決方案可能 – dmytrivv

回答

1

簡單地增加一些反在你的,如果/ ELSEIF/else結構...

$counters = array('blank'=>0, 'correct'=>0, 'incorrect'=>0); 
// Print out the contents of each row into a table 
while($row = mysql_fetch_array($result)){ 
    if (''==$row['correctanswer']) { 
     echo '<tr><td style="color:blue;">Thankyou for your feedback</td></tr>'; 
     $counters['blank'] += 1; 
    } 
    elseif ($row['correctanswer']==$row['quizselectanswer']) { 
     echo '<tr><td style="font-weight:bold; color:green;">CORRECT</td></tr>'; 
     $counters['correct'] += 1; 
    } 
    else { 
     echo '<tr><td style="font-weight:bold; color:red;">INCORRECT</td></tr>'; 
     $counters['incorrect'] += 1; 
    } 
} 

echo '#correct answers: ', $counters['correct']; 
+0

感謝您的幫助,現在完美地工作! –

2

只是增加兩個計數器

$correct_answers = 0; 
$incorrect_answers = 0; 
while ($row = mysql_fetch_array($result)) { 
    if ($row['correctanswer'] == '') {... 
    } elseif ($row['correctanswer'] == $row['quizselectanswer']) { 
     echo '<tr><td style="font-weight:bold; color:green;">CORRECT</td></tr>'; 
     $correct_answers++; 
    } else { 
     echo '<tr><td style="font-weight:bold; color:red;">INCORRECT</td></tr>'; 
     $incorrect_answers++; 
    } 
} 
1

每個分支如何與創建變量正確答案的數量?對於您循環的每個問題,將值遞增1。

<?php 
    $correct_answers = 0; 
    while($questions) { 
     if($answer_is_correct) { 
      // Increment the number of correct answers. 
      $correct_answers++; 
      // Display the message you need to and continue to next question. 
     } 
     else { 
      // Don't increment the number of correct answers, display the message 
      // you need to and continue to the next question. 
     } 
    } 
    echo 'You got ' . $correct_answers . ' question(s) right!'; 
1
<?php 
// Make a MySQL Connection 
// Construct our join query 
$query = "SELECT ... "; 

$result = mysql_query($query) or die(mysql_error()); 

$countCorrect = 0; 
$countIncorrect = 0; 

// Print out the contents of each row into a table 
while($row = mysql_fetch_array($result)){ 
    if ($row['correctanswer'] == ''){echo '<tr><td style="color:blue;">Thankyou for your feedback</td></tr>';} 
    elseif ($row['correctanswer'] == $row['quizselectanswer']){ 
     $countCorrect++; 


     echo '<tr><td style="font-weight:bold; color:green;">CORRECT</td></tr>';} 
     else { 
     $countIncorrect++; 
     echo '<tr><td style="font-weight:bold; color:red;">INCORRECT</td></tr>'; 
     } 



} 

echo ((int)($countCorrect/($countIncorrect + $countCorrect) * 100)) . "% answers were correct!" ; 

?>