PHP SCRIPT TO RETRIEVE QUESTIONS RANDOMLY AND DISPLAY IT ON A HTML FORM

4 replies
please i need help, i am trying to tretrieve data randomly from a database table named question and displayed the retrieve data on html form.

i am making use of a session variable to store the retrieve data, and i create a class with a function, so that any time the function is called it will retrieve data form the question table and assign the retrieve values to the session variables but i am having this error saying.

! ) Parse error: syntax error, unexpected ''startExam.php'' (T_CONSTANT_ENCAPSED_STRING) in C:\wamp\www\examSystem\student.php on line 6

**************************************************
this is my code
<?php session_start(); ?>
<?php


class startExam
{

$_SESSION['answers1'];
$_SESSION['answers2'];
$_SESSION['answers3'];
$_SESSION['answers4'];
$_SESSION['Q_ID'];
$_SESSION['question'];

function loop()
{
$con=mysqli_connect("localhost","root","","online_ exam");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}



if(isset($_SESSION['$Q_ID']))
$_SESSION['Q_ID'] = $_SESSION['Q_ID']+ 1;
else
$_SESSION['Q_ID'] = 1;

$id = $_SESSION['Q_ID'];


$result = mysqli_query($con,"SELECT * FROM question WHERE Question_ID = '$id'");



$Quest;

while($row = mysqli_fetch_array($result))
{
$_SESSION['question'] = $row['Quest'];

}
$AnsResult = mysqli_query($con,"SELECT * FROM answers WHERE answer_id = '$id'");

while($Ans = mysqli_fetch_array($AnsResult))
{
$_SESSION['answers1'] = $Ans['answer1'];
$_SESSION['answers2'] = $Ans['answer2'];
$_SESSION['answers3'] = $Ans['answer3'];
$_SESSION['answers4'] = $Ans['answer4'];

}

mysqli_close($con);
// header('Location: http:student.php');
}
}



?>


and this code is to trigger the function and display the data
********************************

<?php session_start();
?>


<?php
required_once 'startExam.php';

$myChange = new startExam();
if(isset($_POST['submit']))
{
$myChange->loop();
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Student page</title>

<link rel="stylesheet" type="text/css" href="Admin.css">
<link rel="stylesheet" type="text/css" href="Login.css">
<link rel="stylesheet" type="text/css" href="Student.css">
</head>

<body style = "background-color:#F5F5F5">
<p id ="bgr">
<table>
<tr>
<th></th>
<th></th>
</tr>
<tr>
<td>
<img src = "image/logo5.jpg"height="100"width="600" border="0"/>
</td>
</tr>
</table>
</p>
<p>
<hr id = "line1">
<img src = "image/stu.jpg"height="40"width="120" border="0"/> <br/>
<a href="login.html">Logout</a>
</p>
<!--menu-->
<nav>
<p>

<table>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<td id="menu">

</td>
</tr>




<tr>
<td id="menu">
<a href="/E/">End Exam</a>
</td>
</tr>

<br/>
<br/>

<tr>
<td id="menu">

</td>


</table>

</p>
</nav>
<center>



<br/>

<form action = "<?=$_SERVER['PHP_SELF'];?>" method = "post">
<table>
<caption><?php echo $_SESSION['question'];?></caption>
<tr><td><input type = "radio" name = "value1"/></td><td> </td>
</tr>
<tr><td><input type = "radio" name = "value1"/></td><td> <?php //echo $_SESSION['answers2'];?> </td>
</tr>
<tr><td><input type = "radio" name = "value1"/></td><td> <?php //echo $_SESSION['answers3'];?> </td>
</tr>
<tr><td><input type = "radio" name = "value1"/></td><td> <?php //echo $_SESSION['answers4'];?> </td>
</tr>
</table>
<input type="button" value = "CONTINUES">
</form>


</center>
</body>
</html>
#display #form #html #php #questions #randomly #retrieve #script
  • Profile picture of the author Brandon Tanner
    Simple syntax error...

    "required_once" should be "require_once"
    Signature

    {{ DiscussionBoard.errors[8648564].message }}
  • Profile picture of the author lloydblinks
    i correct the mistake to the right syntax "require_once" nut still having error, so now i change my script to this.

    <?php //session_start(); ?>
    <?php

    $con=mysqli_connect("localhost","root","","online_ exam");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM question");
    //$AnsResult = mysqli_query($con,"SELECT * FROM answers");
    $AnsResult = mysqli_query($con,"SELECT * FROM answers WHERE answer_id = '1'");

    $_SESSION['answers1'];
    while($row = mysqli_fetch_array($AnsResult) )
    {

    $_SESSION['answers1'] = $row[answer1];

    }


    while($row = mysqli_fetch_array($result) )
    {

    $question = $row['Quest'];


    echo "$question";
    echo "<br/>";
    echo '<input type = "radio"/>'.$_SESSION['answers1'];

    echo "<br/>";
    echo '<input type = "radio">';
    echo "<br/>";
    echo '<input type = "radio">';
    echo "<br/>";
    echo '<input type = "radio">';
    echo "<br/>";

    }



    mysqli_close($con);

    // header('Location: http:student.php');



    ?>
    it is working but it displayed this error
    Notice: Use of undefined constant answer1 - assumed 'answer1' in C:\wamp\www\examSystem\startExam.php on line 19
    which is the session variable $_SESSION['answers1'];
    {{ DiscussionBoard.errors[8651604].message }}
  • Profile picture of the author Andrew H
    Well thats likely because you need to uncomment session_start() at the top of the page (ie: remove the two slashes before it).

    On another note, this script looks like complete garbage - it appears the logic doesn't even make sense. If you're just 'copy paste coding', I suggest looking for another script.
    Signature
    "You shouldn't come here and set yourself up as the resident wizard of oz."
    {{ DiscussionBoard.errors[8651856].message }}
  • Profile picture of the author lloydblinks
    i did this and it is working but it displays the html checkbox before displaying the question.
    it is a logical error that i just dont understand why.
    this is the code

    <?php

    $con=mysqli_connect("localhost","root","","online_ exam");
    // Check connection
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM question");
    //$AnsResult = mysqli_query($con,"SELECT * FROM answers");

    while($row = mysqli_fetch_array($result) )
    {

    $question = $row['Quest'];
    $Q1 = $row['answer1'];
    $Q2 = $row['answer2'];
    $Q3 = $row['answer3'];
    $Q4 = $row['answer4'];



    print("$question");
    echo '<br/>';
    echo '<input type = "checkbox" name = "answers"/>'.$Q1;

    echo '<br/>';
    echo '<input type = "checkbox">'.$Q2;
    echo '<br/>';
    echo '<input type = "checkbox">'.$Q3;
    echo '<br/>';
    echo '<input type = "checkbox">'.$Q4;
    echo '<br/>';

    }



    mysqli_close($con);
    ?>
    {{ DiscussionBoard.errors[8656004].message }}

Trending Topics