-1

我正在尝试进行测验。它正在工作,但没有给出正确的结果。在正确答案上,例如答案 1 变量 rans 应该增加 1,但在提交第二个问题后它会增加,这就是为什么第 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>";
    }

    }
    ?>
4

1 回答 1

0

好吧,你的代码有点乱。

你有随机的重言式(比如$rans=$rans;绝对什么都不做。如果回答者点击“下一步”,你就分配$totalquestion了两次。一定要好好看看,然后重构这个页面。

但是你的问题的答案可能是因为你正在检查他们是否在代码底部输入了正确的答案——在你展示了结果或下一个问题之后。

您已经使用了 PHP 的脚本功能,而没有触及任何函数,因此它将从上到下进行评估。

我会移动所有内容:将“下一个”的处理程序移动到顶部,在您的默认变量分配下方,然后将正确答案的检查放在其下方,然后进行下一个问题的演示,然后是“提交”处理程序。

为了可读性和可重用性,我也会将不同的单元分解为函数。例如,制作一个函数来打印指定的问题,制作另一个函数来验证用户输入的正确答案。

于 2013-10-22T15:49:35.043 回答