Help!! Online Exam Checking!

Discussion in 'PHP' started by razeljane, May 26, 2008.

  1. #1
    hello...can somebody help me? i've been working this out for how many weeks but still cant get it.. I have a paging online exam, there are 10 questions in every page and there are three pages. When i click submit, it only checks the last page, not included the 1st and 2nd page..

    here's my code...
    SUBJECT.PHP --- list of all my subjects..


    <?php
    session_start();
    import_request_variables("pgc","");

    include("../dbconnect.php");
    include("../header.php");
    ?>

    <tr>
    <td align="center" align="top">
    <br />
    <br />
    <table>
    <td width=25>
    <img src="../IMAGES/ball.gif" width=18 height=18></td><Td>
    <a href="subject.php?type=math">Math</a>
    </td>

    <td width=25>
    <img src="../IMAGES/ball.gif" width=18 height=18></td><Td>
    <a href="subject.php?type=physics">Physics</a>
    </td>

    <td width=25>
    <img src="../IMAGES/ball.gif" width=18 height=18></td><Td>
    <a href="subject.php?type=english">English</a>
    </td>

    <td width=25>
    <img src="../IMAGES/ball.gif" width=18 height=18></td><Td>
    <a href="subject.php?type=history">History</a>
    </td>

    <td width=25>
    <img src="../IMAGES/ball.gif" width=18 height=18></td><Td>
    <a href="subject.php?type=science">Science</a>
    </td>

    <td width=25>
    <img src="../IMAGES/ball.gif" width=18 height=18></td><Td>
    <a href="subject.php?type=world">World</a>
    </td>

    </table>
    </td>
    </tr>

    <tr>
    <td align="center">

    <?php
    $type = $_GET["type"];

    include ("../quiz1.php");
    include("../dbconnect.php");

    switch($type){
    case "math" :
    $quiz = question($type);
    $list = $quiz['question'];
    $page = $quiz['paging'];

    echo "<SCRIPT language=JavaScript type=''>loadSelections(document.forms[0]);</SCRIPT>";
    echo "<body onunload='saveSelections(document.forms[0])'>";
    echo "<form action='../check/math.php' method='post' id='appointment' name='appointment'>";
    echo "<table>
    $list
    <tr><td>$page</td></tr></table>";
    echo "</form>";
    echo "</body>";
    break;

    case "english" :
    $quiz = question($type);
    $list = $quiz['question'];
    $page = $quiz['paging'];

    echo "<SCRIPT language=JavaScript type=''>loadSelections(document.forms[0]);</SCRIPT>";
    echo "<body onunload='saveSelections(document.forms[0])'>";
    echo "<form action='../check/english.php' method='post' id='appointment' name='appointment'>";
    echo "<table>
    $list
    <tr><td>$page</td></tr></table>";
    echo "</form>";
    echo "</body>";
    break;

    case "physics" :
    $quiz = question($type);
    $list = $quiz['question'];
    $page = $quiz['paging'];

    echo "<SCRIPT language=JavaScript type=''>loadSelections(document.forms[0]);</SCRIPT>";
    echo "<body onunload='saveSelections(document.forms[0])'>";
    echo "<form action='../check/physics.php' method='post' id='appointment' name='appointment'>";
    echo "<table>
    $list
    <tr><td>$page</td></tr></table>";
    echo "</form>";
    echo "</body>";
    break;

    case "history" :
    $quiz = question($type);
    $list = $quiz['question'];
    $page = $quiz['paging'];

    echo "<SCRIPT language=JavaScript type=''>loadSelections(document.forms[0]);</SCRIPT>";
    echo "<body onunload='saveSelections(document.forms[0])'>";
    echo "<form action='../check/history.php' method='post' id='appointment' name='appointment'>";
    echo "<table>
    $list
    <tr><td>$page</td></tr></table>";
    echo "</form>";
    echo "</body>";
    break;

    case "science" :
    $quiz = question($type);
    $list = $quiz['question'];
    $page = $quiz['paging'];

    echo "<SCRIPT language=JavaScript type=''>loadSelections(document.forms[0]);</SCRIPT>";
    echo "<body onunload='saveSelections(document.forms[0])'>";
    echo "<form action='../check/science.php' method='post' id='appointment' name='appointment'>";
    echo "<table>
    $list
    <tr><td>$page</td></tr></table>";
    echo "</form>";
    echo "</body>";
    break;

    case "world" :
    $quiz = question($type);
    $list = $quiz['question'];
    $page = $quiz['paging'];

    echo "<form method=post action='../check/world.php'>";
    echo "<table>
    $list
    <tr><td>$page</td></tr></table>";
    break;

    default :
    echo "Please choose a category";
    break;

    }
    ?>

    <tr>
    <td colspan="2"><a href=""><img src="../images/m01.jpg" border="0"></a><a href=""><img src="../images/m02.jpg" border="0"></a><a href=""><img src="../images/m03.jpg" border="0"></a><a href=""><img src="../images/m04.jpg" border="0"></a><a href=""><img src="../images/m05.jpg" border="0"></a></td>
    </tr>


    ------------------------------------------------------------
    quiz1.php----> includes my paging and exams

    <?php
    session_start();
    include ('../contentdb.php');

    function dbconnect() {
    $host = 'localhost';
    $user = 'root';
    $password = 'a';
    return mysql_connect($host, $user, $password);
    }

    function queryDB($query) {
    $link = dbconnect();
    $db = 'onlineexam';

    mysql_select_db($db, $link);
    return mysql_query($query);
    }

    function question($type){

    $type = $type;
    $sql = "SELECT * FROM quiz where category = '$type' ORDER BY qnumber ASC";
    $queryRows = "SELECT COUNT(quiz.id) AS NUMROWS FROM quiz WHERE category = '$type' ORDER BY qnumber ASC";
    $mypage = paging("?type=$type", $sql, $queryRows, $_REQUEST["p"] ,10);

    $result = queryDB($mypage[1]) or die(mysql_error());
    $db = "SELECT * from quiz";
    $dbresult = mysql_query($db);
    $num = mysql_numrows( $dbresult );


    while ($row = mysql_fetch_assoc($result))
    {
    $qnumber = $row["qnumber"];
    $id = $row["id"];
    $question = $row["question"];
    $opt1=$row["opt1"];
    $opt2=$row["opt2"];
    $opt3=$row["opt3"];
    $answer = $row["answer"];
    $category = $row["category"];
    $q = $row["q"];

    if($bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
    else{$bgcolor='#f1f1f1';}

    $list["question"] .= "<tr><td align=left colspan=3><br><b>$qnumber. $question</b></td></tr>
    <tr><td align=left><input type=radio name=\"q$id\" value=\"$opt1\" onclick=submit()>$opt1</a></td><td>
    <tr><td align=left><input type=radio name=\"q$id\" value=\"$opt2\" onclick=submit()>$opt2</a></td><td>
    <tr><td align=left><input type=radio name=\"q$id\" value=\"$opt3\" onclick=submit()>$opt3</a></td></tr>";

    }
    }

    $list["paging"] = $mypage[0];
    return $list;
    }

    function paging($page, $query, $queryRows, $p, $default_rowsPerPage = 2) {

    $rand = $_SESSION['rand'];

    if (empty($rand)) {
    srand((float)microtime()*1000000);
    $rand = "0.".rand();
    $_SESSION['rand'] = $rand;}


    $pageNum = 1;
    $p = $_REQUEST["p"];

    if(isset($p))
    {
    $pageNum = $p;
    }

    if (isset($_REQUEST["rowsPerPage"])) {
    $_SESSION['set_rowsPerPage'] = $_REQUEST["rowsPerPage"];
    $rowsPerPage = $_SESSION['set_rowsPerPage'];

    }
    else {
    if (isset($_SESSION['set_rowsPerPage']))
    $rowsPerPage = $_SESSION['set_rowsPerPage'];
    else
    $rowsPerPage = $default_rowsPerPage;
    }

    $offset = ($pageNum - 1) * $rowsPerPage;

    $sql = $query." LIMIT $offset, $rowsPerPage";
    $query = queryDB($sql) or die(mysql_error());

    $self = $_SERVER['PHP_SELF'].$page;
    $row_count = 0;
    $row_count++;

    $result = queryDB($queryRows) or die('Error, query failed');
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $numrows = $row['NUMROWS'];

    $maxPage = ceil($numrows/$rowsPerPage);

    $nav = '';

    for($page = 1; $page <= 3; $page++)
    {

    }

    if ($pageNum > 2){
    $page = $pageNum - 1;
    echo "<font color=red><b><u>HINT:</u></b></font>&nbsp;&nbsp;<font color=black><b>Hit</b></font>&nbsp;<font color=red><b><u>PREVIOUS</u></font>&nbsp;<font color=black><b>button to go back to previous page.</b></font>";
    }

    if ($pageNum !=1)
    {
    $prev = '<a href=javascript:history.back() style=text-decoration:none><input type=submit value=Previous></a>';
    echo "$score";
    }

    if ($pageNum < 3) {
    $page = $pageNum + 1;
    $next = "<a onclick=saveSelections(document.getElementById('appointment'));validateAppointment(document.forms['appointment']) href='#' style='text-decoration:none'><input type=submit value='Save Answer'><a href=\"$self&p=$page\" style='text-decoration:none'><input type='submit' name='submit 'value=Next></a>";
    echo "<br/>";
    echo "<font color=red><b>Take your time in answering before going into <u>NEXT</u> page.</b></font>";
    }

    else
    {
    $prev = '<a href=javascript:history.back() style=text-decoration:none><input type=submit value=Previous></a>';
    }

    if ($pageNum = 3 && $pageNum >= 3)
    {
    $sub = "<input type=submit name='submit' value='Submit and Finish'>";
    }

    $fromPage[0]=$first . $prev . $nav . $save . $next . $last . $sub;
    $fromPage[1]=$sql;

    return $fromPage;
    }

    ?>


    ----------------------------------------------------------------------------
    math.php ----> for checking!

    <?php
    session_start();
    include("../contentdb.php");
    include('../header.php');

    echo "<tr>";
    echo "<td>";

    $type=$_GET['type'];
    $display = mysql_query("SELECT * FROM quiz where category='math'",$db);

    if ($submit)
    {
    $score=0;

    while ($result = mysql_fetch_array($display))
    {
    $answer = $result["answer"];
    $q = $result["q"];

    if ($$q == $answer)
    {
    $score++;
    }
    }

    $total = 30;

    echo "<tr>";
    echo "<td>";
    echo "<p align=left><b>You scored $score out of $total</b></p>";
    echo "<p>";

    if ($score == $total) {
    echo "<p align=left>Congratulations! You got every question right!</p>";
    }
    elseif ($score/$total < 0.34) {
    echo "<p align=left>Not the best score.</p>";
    }
    elseif ($score/$total > 0.67) {
    echo "<p align=left>Well done! You certainly know your stuff.</p>";
    }
    else {
    echo "<p align=left>Not bad - but there were a few that caught you out!</p>";
    }

    echo "<b><p align=left>Here are the answers:</p></b>";

    echo "<table border=0 align=left >";
    $display = mysql_query("SELECT * FROM $table where category='math' ORDER BY qnumber ASC",$db);
    while ($row = mysql_fetch_array($display)) {

    $qnumber = $row ["qnumber"];
    $question = $row["question"];
    $answer = $row["answer"];
    $q = $row["q"];

    echo "<tr align=left><td><br><b>$qnumber. $question</b></td></tr>";

    if ($$q == $answer)
    {
    echo "<tr align=left><td><b>&raquo;You answered <font color=red><u>${$q}</u></font>, CORRECT!</b></td></tr>";
    echo "${$q}";
    }

    elseif ($$q == "") {
    echo "<tr align=left><td>&raquo;You didn't select an answer. The answer is $answer</td></tr>";
    }

    else {
    echo "<tr><td>&raquo;you answered <b><u>\"${$q}\"</u></b>. The answer is <font color=red><b><u>$answer</u></b></font></td></tr>";
    echo "${$q}";
    }

    }
    echo "</table></p>";
    echo "<br />";


    echo "<br />";
    echo "<br />";
    echo "<br />";
    echo "<br />";
    echo "<tr><td><a href='../student/subject.php'>Take New Test?</a></td></tr>";
    echo "<tr><td>&nbsp;</td></tr>";
    }


    ?>
     
    razeljane, May 26, 2008 IP