2013-10-21 36 views
-1

我試圖做一個測驗。它的工作,但沒有給出正確的結果。例如對於答案的正確答案,1個變量應該遞增1,但是在提交第2個問題後遞增,這就是爲什麼第10個當前答案的值不包括在總正確答案中的原因。如何顯示測驗的正確結果

  <?php 
     require_once("global.inc.php");?> 
    <form name="test" method="post" action="test.php"> 
    <?php 
    $qid=(!isset($_POST['q_id'])) ? 0 : $_POST['q_id']; 
    $rans=(!isset($_POST["rans"])) ? 0 : $_POST["rans"]; 
    $totalquestion=(!isset($_POST["totalquestion"])) ? 
      0 : $_POST["totalquestion"]; 
    echo $rans; 

    if(isset($_POST["submit"])) 
    { 
    echo "<table align='center' style='border:1px solid silver' width='80%' 
    bgcolor='green'>"; 
    echo "<tr><td>Total Question Attempt</td><td>",$totalquestion,"</td><tr>"; 
    echo "<tr><td>Correct Answer</td><td>",$rans,"</td></tr>"; 
    echo "<tr><td>Wrong Answer</td><td>",$totalquestion-$rans,"</td></tr>"; 
    echo "<tr><td>Correct Answer Percentage</td> <td>",$rans/$totalquestion*100,"%</td></tr>"; 
    echo "<tr><td>Wrong Answer Percenntage</td><td>",($totalquestion-$rans)/$totalquestion*100,"%</td></tr>"; 
    echo "</table><br><br>"; 
    $query="select * from questions,answers 
      where questions.q_id=answers.q_id"; 

    echo "<table cellpadding='5px' align='center' style='border:1px 
    solid silver'>"; 
    echo "<tr><th colspan='4' id='heading'>Online Quiz Test 
    Question</td></tr>"; 
    $result=mysql_query($query); 
    while ($row = mysql_fetch_array($result)) { 
    echo "<tr><td>",$row['q_id'],"</td><td colspan='2'>",$row['question'],"</td></tr><tr><td></td>"; 
    echo "<td colspan='2'>A. ",$row['opt1'],"</td>"; 
    echo "<td colspan='2'>B. ",$row['opt2'],"</td></tr>"; 
    echo "<tr><td></td><td colspan='2'>C. ",$row['opt3'],"</td>"; 
    echo "<td colspan='1'>D. ",$row['opt4'],"</td></tr>"; 
    echo "<tr><td colspan='4' align='right' 
    style='color:orange'>Correct option is ",strtoupper($row['correct_ans']),"</td></tr>"; 
    echo "<tr><td colspan='4' align='right' 
    style='color:orange'><hr></td></tr>"; 

    } 
    echo "</table>"; 
    echo "<p align='right'><a href='#' onclick='window.print()'>Print</a></p>"; 
    echo "<div style='visibility:hidden;display:none'>"; 

    } 

    ?> 
     <form name="test" method="post" action="test.php"> 
    <?php 
     if(!isset($a)) 
     { 
      $a=0; 

     //unset($_SESSION['score']); 
     } 


     if(isset($_POST['next'])) { 
      $a=$_POST['a']; 
      $totalquestion=$_POST['totalquestion']; 
      if(isset($_POST['rans'])) 
      $rans=$_POST['rans'];  

      } 

     $sql1="SELECT * FROM questions,answers 
        where questions.q_id=answers.q_id limit 1 offset $a"; 
     $result=mysql_query($sql1); 
     $num = mysql_num_rows($result); 
     echo "<form method='post' action=''>"; 


     if($result) { 
     while ($row = mysql_fetch_array($result)) 

     { 

     $qid = $row["q_id"]; 

     $questions = $row["question"]; 

     $opt1 = $row["opt1"]; 

     $opt2 = $row["opt2"]; 

     $opt3 = $row["opt3"]; 

     $opt4 = $row["opt4"]; 

     $correct = $row["correct_ans"]; 
     echo $rans; 
    ?> 


     <p >Q.<?php echo $qid ?> <?php echo $questions;?></p> 
<input type="radio" value="<?php echo $opt1;?>" name="choice"/><?php echo $opt1;?> <br/> 
<input type="radio" value="<?php echo $opt2;?>" name="choice"/><?php echo $opt2;?><br/> 
<input type="radio" value="<?php echo $opt3;?>" name="choice"/><?php echo $opt3;?><br/> 
<input type="radio" value="<?php echo $opt4;?>" name="choice"/><?php echo $opt4;?><br/> 
<input type="hidden" value="$answer" name="rightanswer[$qid]"/> 


    <?php 

     $b=$a+1; 


     $sql2="SELECT * FROM questions where q_id=$qid-1 "; 
     $result2=mysql_query($sql2); 
     while ($row2 = mysql_fetch_array($result2)) { 
     $ans=$row2['correct_ans']; 

     }  


     if(isset($_POST['choice'])) { 
     if($ans==$_POST['choice']){ 
     //echo "<input type='hidden' name='rans' value='".($rans+1). "'>"; 
     $rans=$rans+1; 
     } 
     else { 
     //echo "<input type='hidden' name='rans' value='" . $rans . "'>"; 
     $rans=$rans; 
     } 
     } 

     //$query="select correct_ans from questions where q_id='$qid'"; 
     //$result=mysql_query($query); 
     //while ($row = mysql_fetch_array($result)) { 
     //echo $row['correct_ans']; 

    echo "<input type='hidden' value='$b' name='a'>"; 
    echo "<input type='hidden' value='count' name='count'>"; 
    echo "<input type='hidden' name=qid value='$qid'>"; 

    echo "<input type='hidden' name='totalquestion' value='".$totalquestion+1)."'>"; 

     echo "<input type='hidden' name='rans' value='" . $rans . "'>"; 

     echo "<input type='submit' name='next' value='next'> "; 

     echo "<input type='submit' name='submit' value='submit'><br><br>"; 

    echo "</form>"; 
    } 

    } 
    ?> 
+1

$ Mysql_ *由於安全問題而不推薦使用。用戶mysqli或pdo在未來也可使用準備好的語句! – Snowburnt

+0

這應該做什麼? @ $ rans = $ rans;' – Snowburnt

+0

$ rans = $ rans是指定錯誤答案的前一個值(正確答案)。@ Snowburnt – user2745286

回答

0

好的,你的代碼有點亂。

你有隨機套套邏輯(如這絕對沒有的$rans=$rans;,如果回答者點擊「下一步」,你要指定$totalquestion兩次。絕對走好,認真審視和重構這個頁面。

但答案你的問題可能是因爲你正在檢查他們是否在代碼底部輸入了正確的答案 - 在你提交結果或下一個問題之後

你已經使用了腳本功能的PHP沒有觸及任何功能,所以它會評估自上而下。

我想把所有東西都移動:將「下一個」的處理程序移到頂部,在默認變量賦值下面,然後將正確答案的檢查放在下面,然後進行下一個問題的呈現,然後「提交」處理程序。

我會將不同的單位分解爲可讀性和可重用性的函數。例如,創建一個功能來打印出指定的問題,並使另一個用於驗證用戶輸入的正確答案。

+0

謝謝Snowburnt,我根據您的建議重新安排了我的代碼段,現在它正常工作@Snowburnt – user2745286