2015-07-02 24 views
0

我有一個表question_attempts。該表具有兩列rightanswersyouranswer從兩列使用條件相同的記錄計數

我想獲得記錄數rightanswer = youranswer。這實際上是我的整個代碼。

$quizsections = mysql_query("SELECT * FROM quiz_sections"); 
while($quizsectionsrslt = mysql_fetch_array($quizsections)){ 
$quizsectionsid  = $quizsectionsrslt['id']; 
$quizsectionsheading = $quizsectionsrslt['heading']; 
$quizsectionsquizid = $quizsectionsrslt['quizid']; 
$quizsectionsfirstslot = $quizsectionsrslt['firstslot']; 
echo $quizsectionsheading."<br />"; 
$quizslots = mysql_query("SELECT * FROM quiz_slots WHERE `quizid`=$quizsectionsquizid LIMIT ".($quizsectionsfirstslot-1).", 10"); 
while($quizslotsrslt = mysql_fetch_array($quizslots)){ 
    $quizslotids = $quizslotsrslt['questionid']; 
    $questions = mysql_query("SELECT * FROM question_attempts WHERE `questionid`=$quizslotids"); 
    $num_rows = mysql_num_rows($quizslots); 
    while($answercount = mysql_fetch_array($questions)){ 
     $answercountrslt = $answercount['questionid']; 
     echo $rightanswer = $answercount['rightanswer']; 
     echo $youranser = $answercount['responsesummary']."<br />"; 
//Here want to display the count of rightanswer and count of wrong answer. 
    } 
    } 
    echo "Total Questions : ".$num_rows."<br /><br />"; 
} 
+0

得到了我的要求的結果。 – Raja

回答

3

使用下面:

$rightanswercount=0; 
$badanswercount=0; 

    $questions = mysql_query("SELECT * FROM question_attempts"); 
     while($answercount = mysql_fetch_array($questions)){ 
    $rightanswer = $answercount['rightanswer']; 
    $youranser = $answercount['youranswer']; 
    if($youranser == $rightanswer) 
      { 
       $rightanswercount=$rightanswercount+1; 
      } 
      else 
      { 
       $badanswercount=$badanswercount+1; 
      } 
     } 

echo "Right Count=".$rightanswercount; 
echo "Bad Count=".$badanswercount; 
+0

Amrut!它的工作很棒。但是,放置此代碼後重復多次的結果。我剛剛更新了我的問題。看看它的PLZ。 – Raja

+0

胡莉!得到了我想要的結果。 – Raja

1

如果你想只算然後u可以使用mysql_num_rows

$questions = mysql_query("SELECT count(*) FROM question_attempts where rightanswers=youranswer"); 
$count=mysql_num_rows($questions); 
+0

我也試過這個。查看一下我的代碼。我用$ num_rows而不是$ count – Raja

1

您可以通過SQL查詢做你的任務,並在正確執行的東西答案

$questions = mysql_query("SELECT * FROM question_attempts where rightanswers=youranswer"); 

$count = mysql_num_rows($questions); 

// $count is number of right answers 
while($row = mysql_fetch_array($questions)) 
{ 
    // Stuff on right answers 
} 
+0

Vipul,我已經試過這個,你可能會明白,如果你看我的代碼一次。 – Raja

相關問題