PHP页面:实现多项选择和True / False(测验)

时间:2015-09-08 22:14:28

标签: php html css

此网页提取存储在两个不同表格中的问题和答案,称为问题和答案。我能够成功从数据库中获取数据并代表它。问题是评估部分。由于问题的数量未知,所以我应该如何动态更改标签的值?同时在为单个问题实现它时(即:数据库中只有一个问题),我无法获得正确的结果。有人可以帮帮我吗?

我的代码: -

<?php

include("session.php");
include("exam_session.php");
echo "Hello ".$username;
echo " Click Here To Log Out... <a href='logout.php'> LogOut!</a>";
echo "<br /><br /> Quiz Code : ".$code;
?>
<?php
//$variable_name=array('zero','one','two','three','four','five','six','seven','eight');
$qu=mysql_query("SELECT COUNT(*) FROM questions where code='".$code."'");
//$la=mysql_fetch_array($qu);
//echo "No of Questions : ".$la;
//$limit=$_SESSION['maid'];

if(isset($_POST["sub"])) {
    $last=0;
    $marks=0;
    $te=$_POST['one'].$_POST['one'];
    echo "<br> Value : ".$_POST['one'];
    if( isset($_POST['one']) ) {
        echo "You have selected :".$_POST['one'];
        //echo "<br />ONE : ".$PayPlatform;
        //if($PayPlatfrom=='A')
        $marks+=1;
    }
    $last+=1;
    $_SESSION["marks"] = $marks;
    echo $_SESSION['marks'];
    echo "<br />Marks : ".$marks;
    $_SESSION["tot_ques"] = $qu;
    header("location: last_ans.php");
    exit();
}
?>

<html>
<head>
</head>
<body>
<?php
$variable_name=array('zero','one','two','three','four','five','six','seven','eight');
//print_r($variable_name);
$macount=0;
//$temp=$variable_name[$macount];
//echo "<br /> Temp : ".$temp;
$maid=0;
$matf=0;
$zids=0;
$query=mysql_query("SELECT COUNT(*) FROM questions");
$query5=mysql_query("SELECT COUNT(*) FROM answers");
$row=mysql_fetch_array($query);
$rows=$row[0];  
$row5=mysql_fetch_array($query5);
$rows5=$row5[0];    
$query1=mysql_query("select question_id,question,type,code from questions");
$query6=mysql_query("select id,question_id,answer,correct from answers");

for($i=0; $i<$rows ; $i++) {
    $ans=mysql_fetch_array($query1);
    $name=$ans['code'];
    if($name == $code) {
        $msg=$ans['question'];
        echo '<div id="msg_pst"><br />
        Question : <br /><br /><p class="msps"> '.$msg.' </p><br /><br />
        </div>';
        $types=$ans['type'];
        $quest1=$ans['question_id'];
        for($k=0;$k<$rows5;$k++) {
            //echo "inside k ";
            $ans5=mysql_fetch_array($query6);
            $quest2=$ans5['question_id'];
            if($quest1==$quest2) {
                //echo "inside j";
                if($types=="mc") {
                    $temp=$variable_name[$maid];
                    echo "Temp : ".$temp;
                    $msg5=$ans5['answer'];
                    $correct=$ans5['correct'];
                    if($correct==1) {
                        //$divid=$variable_name($zids);
                        echo '<div id="msg_pst">
                        <input type="radio" name="one" value= A /> '.$msg5.'
                        </div>';
                        $macount+=1;
                        if($macount%4==0) {
                            $maid+=1;
                        }
                    }
                    else if($correct!=1) {
                        echo ' <div id="msg_pst">
                        <input type=radio name="one" value=False /> '.$msg5.'
                        </div>';
                        $macount+=1;
                        if($macount%4==0) {
                            $maid+=1;
                        }
                    }
                }
                else if($types=="tf") {
                    $msg5=$ans5['question_id'];
                    $correct=$ans5['correct'];
                    if($correct==1) {
                        echo '<div id="msg_pst">
                        <input type=radio name='.$maid.' id='.$maid.' value=A /> <p class="msps"> '.$msg5.' </p>
                        </div>';                                    
                        $matf+=1;
                        if($matf%2==0) {
                            $maid+=1;
                            $matf=0;
                        }
                    }
                    else if($correct!=1) {
                        echo '<div id="msg_pst">
                        <input type=radio name='.$maid.' id='.$maid.' value=False /> <p class="msps"> '.$msg5.' </p>
                        </div>';
                        $matf+=1;
                        if($matf%2==0) {
                            $maid+=1;
                            $matf=0;
                        }
                    }
                }
            }
        }   
    }
}
//echo "<br / Maid : >".$maid;
//session_start();
$_SESSION['maid']=$maid;
?>
<form action="exam.php" method="POST">
<input type="submit" name="sub" value="Submit Quiz" />
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

我认为使代码工作所需的关键是while循环。在你的情况下,while循环是有用的,因为它为数据库中的每一行循环遍历相同的代码,即使你不知道有多少行。

您的代码:

$row=mysql_fetch_array($query);

应该是:

while($row=mysql_fetch_array($query)){
    //This is one row in your database
    //It will run code inside here to every row in your table
    //You could do something like echo "<label>{$row['question_name']}</label>"; to dynamically create labels. 
    var_dump($row);
}

希望这是你要找的东西。