使用所选选项显示图像

时间:2016-06-25 12:58:44

标签: javascript php html

我正在建立一个问题和答案的网站,其中将显示几个多项选择题,用户必须选择单选按钮来回答问题。提交后,将显示正确和错误的答案,并且用户可以选择查看正确的答案。 在此页面中,为了查看正确答案,我想要做的是显示正确选项的刻度线图像和所有其他错误选项的交叉图像。但我在编写逻辑方面遇到了问题。 这就是我到目前为止所做的。

此页面用于选择要显示的图像

<?

        session_start() ; 
        $wrong =  array() ;
        if( !empty($_SESSION['wrongAnswer'] ) )  {
            $wrong = $_SESSION['wrongAnswer'] ;//wrongAnswer array has  
                                               //ids of wrongly answered 
                                               //questions         

            session_destroy() ;
        }

        if( !empty($_POST['wrongAnswer'] ) )  {
            $wrong = $_POST['wrongAnswer'] ; 
        }

        $i = 0 ;
        $quesID = ""; 
        $question = "" ;
        require_once "connection.php";  
        foreach ($wrong as $n) {     

       <?  $query = "SELECT * FROM answers WHERE question_id = '$n' " ;
            if($result = $mysqli->query( $query ) )  {
               if( $row = $result->fetch_object() ){
                    $answer = $row->answer ;

                 // $count++;
                }
                else 
                    echo "Couldn't tally answer " . $mysqli->error ;

            }

            else
                echo "Error executing query" . $mysqli->error ;


                                  //<----- THIS IS WHERE THE WRONGLY ANSWERED QUESTIONS WILL BE DISPLAYED WITH IMAGES AS MENTIONED BEFORE ------>//

                $query = "SELECT * FROM questions WHERE questionid='$n' " ;
                 $i ++ ;
                 if( $result = $mysqli->query( $query ) )  {  
                     $count= $result->num_rows ;
                      echo '<form name = "checkans.php" action = "checkans.php" method = "POST" >' ;

                     $row = $result->fetch_object()  ; 
                              //  $i++ ;
                               $quesID = $row->questionid ;
                               $question = $row->question ;

                                $quesID = $row->questionid ;
                                $query2 = "SELECT option1,option2,option3,option4 FROM options WHERE questionid='$quesID'" ;
                                if($result2 = $mysqli->query($query2) )  {
                                    if($row2 = $result2->fetch_object() )  {
                                        $option1 = $row2->option1 ;
                                        $option2 = $row2->option2 ;
                                        $option3 = $row2->option3 ;
                                        $option4 = $row2->option4 ;

                                       ?>
                      <table border="0" cellpadding="0" cellspacing="0">
                <?  echo "<div  id = $row->questionid name = boxs>" ; ?>
                <th> <h2> <? echo $i .")" ; ?>  </h2>  </th> 
                <th> <h2> <?echo $row->question ;?> </h2> </th> 

          <tr>  <input type="hidden" name="quesID[]" id="<? echo $quesID ; ?>" value="<? echo $quesID ; ?>">
            <div id = "options">
            <td>
            <span id = "1"> 
            <img id = "image" height="50px" width="50px">
            </td>
          <td>  
        <label>
          <? echo $option1 ;?>
        </label>
            </td>
        </span>

        <td>
        <span id = "2">
            <img id = "image" height="50px" width="50px">

          </td>
          <td>
        <label>
          <? echo $option2 ;?>
        </label>
        </td>
        </span>

        <td>
        <span id = "3">
            <img id = "image" height="50px" width="50px">

          </td>
          <td>
        <label>
          <? echo $option3 ;?>
        </label>
        </td>
          </span>

          <td>
          <span id = "4">
            <img id = "image" height="50px" width="50px">



          </td>

          <td>
        <label>
          <? echo $option4 ;?>
        </label>
        </td>
            </div>

        </span>
        </div>

              </tr>

          </table> 
            </section>
        <?          
                                    }
                                    else 
                                        echo "No options available for this question" ;
                                }


         }
         ?>
         <script type="text/javascript">
                var i = 0
                for(i=1; i <= 4; i++)  {
                    var y = document.getElementById(i);
                    //alert(y.childNodes)
                    var x = document.getElementById("image");
                    if( y.id ==  (<?echo $answer ; ?>) )  {
                        x.src = "correct.png" ;
                    }
                    else
                        x.src = "wrong.jpg" ;


                }


        }

    </script>
    <?
       }
    ?>

我的代码错误之处在于它只显示了一个问题的图像,而且错误选项也是如此。

0 个答案:

没有答案