0

hope everyone is fine over there :)

i just wanted to ask how i can display only one question at a time from mysql table??

here is my code which is doing following things
1) selecting all questions and displaying them
what i want to do is
1) display only one question at a time on a webpage and if user press the next button it shows another question
2) i want to check which option he selected so that i could be able to check that option against the correct answer.. and put a (x) or (tick) in front of that question and then move to next....

i hope i explained my answer clearly enough. i just want a way to do these things as i am new to php and my sql. Thankyou :)

questions table has following fields : id|question|type (t/f or MCQs)|possible_opt_1|possible_opt_2|possible_opt_3|possible_opt_4|answer|evaluationid (used as a foreign key)

<?php
   $con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con)
    {
   die('Could not connect: ' . mysql_error());
    }
// selecting the questions that are from the current evaluation.
mysql_select_db($dbname, $con);
$result = mysql_query("SELECT * FROM questions WHERE (evaluationid=".$evaluationid.")");

echo "<form  action='start-course-evaluation-action.php' method='post'>";
echo "<table border='1' style='width:500px;'> <br />";

while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    echo " <strong>" . $row['question'] . "</strong><br /><br /><br />";
    if ($row['type'] === 'MCQs')
    {
    echo "<input type='radio' value='' />"; 
    echo  $row['possible_opt_1']; echo "<br />";
    echo "<input type='radio' value='' />"; 
    echo  $row['possible_opt_2'];echo "<br />";
    echo "<input type='radio' value='' />";
    echo  $row['possible_opt_3'];echo "<br />";
    echo "<input type='radio' value='' />"; 
    echo  $row['possible_opt_4'];echo "<br /><br /><br />";
    }
    else {
         echo "<input type='radio' value='' />"; 
         echo  $row['possible_opt_1']; echo "<br />";
         echo "<input type='radio' value='' />"; 
         echo  $row['possible_opt_2'];echo "<br />";


    }
    echo "</tr>";
    }


echo "</table>";
echo "</form>";
mysql_close($con);
?>
3
Contributors
31
Replies
38
Views
5 Years
Discussion Span
Last Post by broj1
Featured Replies
  • 3

    Assuming that id is an integer and autoincremented by the DB then the id fields probably increase with every new question and you can use this approach: Whenever you read and display the question save the id in the session. If there is no id in the session you are … Read More

  • 2

    Yes, you are right. The questions are getting displayed on the same page. This is set in the action attribute of the form: <form action='start-course-evaluation-action.php' method='post'> and could as well be: <form action='#' method='post'> and the above code should be part of this script. You have to add logic to … Read More

  • where will come first, and order by later SELECT * FROM questions WHERE (evaluationid=".$evaluationid.") ORDER BY id ASC LIMIT 1" Read More

  • 2

    I was away until just now. OK, so you will try it other way. My suggestion is that you first outline the logic with comments like: // check if the user is authorised, if not, redirect // if first question read the first question, correct answer and possible answers // … Read More

3

Assuming that id is an integer and autoincremented by the DB then the id fields probably increase with every new question and you can use this approach:

Whenever you read and display the question save the id in the session. If there is no id in the session you are displaying the first question otherwise you just go for the next question. Also save the id of the last question so you know when to stop. The rest is documended in the code below (it should be a part of start-course-evaluation-action.php script):

// start the session
session_start();

// if this is the first run of your script
// the id has not been stored in the session yet
if(!isset($_SESSION['current_id'])) {

    // this query will select the question with
    // the lowest id, presumably the first one
    $query = 'SELECT * FROM questions ORDER BY id ASC LIMIT 1';

    // read the row, display the question as in the form as above
    // the form can have action set to self (#)
    ...

    // store the current id in the session
    $_SESSION['current_id'] = $row['id']

    // read the last id so you know when to stop
    $query = 'SELECT id FROM questions ORDER BY id DESC LIMIT 1';
    ...

    // store the last id in the session
    $_SESSION['last_id'] = $row['id'];

// if current id exists in the session you can go and display the next qestion
} else {

    // this query will read the question just after the current id
    // (the next question)
    $query = 'SELECT * FROM questions WHERE id > {$_SESSION['current_id']} ORDER BY id ASC LIMIT 1';
    }
    // read the row and display the question as in the form as above
    ...

    // store the current id in the session
    $_SESSION['current_id'] = $row['id'];

    // check if you are at last question
    if($_SESSION['current_id'] == $_SESSION['last_id']) {

       // do whatever appropriate if it is the last question
       // like set a flag
    }
}

This is the concept you can adapt to your needs. Hope it helps.

Edited by broj1

0

@broj1
thankyou very much for such a helpful response :)
the code i posted above is part or start-course-evaluation.php
so shouldn't i use this code in the same page??? as i guess it is diplaying questions 1 by one. plus i would check the answers in the action script.isn't it??

2

Yes, you are right. The questions are getting displayed on the same page. This is set in the action attribute of the form:

<form  action='start-course-evaluation-action.php' method='post'>

and could as well be:

<form  action='#' method='post'>

and the above code should be part of this script. You have to add logic to evaluate and/or save the answers to the current question (probbably save them permamnently in a database or temporary in a session upon each submit). The logic should be somewhere on the beginning of the script.

0

@broj1 thanx again

should i just fetch the $row[id]; for storing last id??
and at the end instead of submit button i should place a next button? m i reight?

// read the last id so you know when to stop
$query = 'SELECT id FROM questions ORDER BY id DESC LIMIT 1';
...
// store the last id in the session
$_SESSION['last_id'] = $row['id'];
0

this is what i have done so far.. its giving error in the query..

<?php
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con)
    {
   die('Could not connect: ' . mysql_error());
    }
mysql_select_db($dbname, $con);
if(!isset($_SESSION['current_id'])) {
$result = mysql_query("SELECT * FROM questions ORDER BY id ASC LIMIT 1 WHERE (evaluationid=".$evaluationid.")");
echo $result;
echo "<form  action='#' method='post'>";
echo "<table border='1' style='width:500px;'> <br />";

while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    $_SESSION['current_id'] = $row['id'];
    echo " <strong>" . $row['question'] . "</strong><br /><br /><br />";
    if ($row['type'] === 'MCQs')
    {

    echo "<input type='radio' value='' />"; 
    echo  $row['possible_opt_1']; echo "<br />";
    echo "<input type='radio' value='' />"; 
    echo  $row['possible_opt_2'];echo "<br />";
    echo "<input type='radio' value='' />";
    echo  $row['possible_opt_3'];echo "<br />";
    echo "<input type='radio' value='' />"; 
    echo  $row['possible_opt_4'];echo "<br /><br /><br />";
    }
    else {
         echo "<input type='radio' value='' />"; 
         echo  $row['possible_opt_1']; echo "<br />";
         echo "<input type='radio' value='' />"; 
         echo  $row['possible_opt_2'];echo "<br />";

            }
    echo "</tr>";
    }   
echo "<input type='submit' name='next' value='next' />";
echo "</table>";


echo "</form>";

$result1 = mysql_query("SELECT id FROM questions ORDER BY id ASC LIMIT 1 WHERE (evaluationid=".$evaluationid.")");
while($row = mysql_fetch_array($result1))
    {

         $_SESSION['last_id'] = $row['id'];

     }




}
mysql_close($con);
?>
1

where will come first, and order by later
SELECT * FROM questions WHERE (evaluationid=".$evaluationid.") ORDER BY id ASC LIMIT 1"

0

@urtrivedi thanx for correcting :)

but its showing nothing now.. no question and no error as well

0

it was a typing mistake.. i corrected it.. :) thanx

total number of questions in this quiz are 2 but it shows only one..what could be the reason?? is thre any mistake in my code or am i have placed the next button some whereelse?

0

you place limit 1 in your query so it is giving only 1 record in output

u check that query, does in mysql (may be phpmyadmin) runs and gives 2 record or 1

0

yeah i limit 1 is beacuse i want to put 1 question at a time and when i press next i want to see the other question and if that question is last the while loop would stop and save that id in lastid...but in this code when i press next it doesnot show me the next question

0

no i just placed the relevant code here.. complete code is this

<?php include("../includes/config.php"); ?>
<?php
 if ($_SESSION["isstudent"])
   {
$cid= $_GET['id'];
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con) { die('Could not connect: ' . mysql_error()); }

mysql_select_db($dbname, $con);
$result1 = mysql_query("SELECT learningmaterial.id,learningmaterial.title,evaluation.id AS eid, evaluation.learningmaterialid FROM learningmaterial JOIN evaluation  ON learningmaterial.id=evaluation.learningmaterialid");

        while($row = mysql_fetch_array($result1))
        {
            $name=$row['title'];
            $evaluationid=$row['eid'];
        }
?>
<?php
$result = mysql_query("SELECT * FROM courses WHERE (id=".$cid.")");

        while($row = mysql_fetch_array($result))
        {
            $title=$row['title'];
        }
?>
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/swfobject.js"></script>
<!--[if lt IE 9]>
  <script type="text/javascript" src="js/excanvas/excanvas.js"></script>
<![endif]-->
<script type="text/javascript" src="js/spinners/spinners.min.js"></script>
<script type="text/javascript" src="js/lightview/lightview.js"></script>
<link rel="stylesheet" type="text/css" href="css/lightview/lightview.css"/>

<?php include("../includes/pre-header.php");?>
<title>Evaluation</title>
</head>
<body>
<div class="container">
<?php include("../includes/header.php"); ?>
<?php include("includes/nav.php"); ?>
<div id="maincontent">
<div class="span-24 last">
    <div id="breadcrumbs" class="mycss">
        <a href="index.php">Home</a> >
        <a href="my-courses.php">My Courses</a> >
        <a href="start-course.php"><?php echo $title; ?></a> >
        <a href="start-course-evaluation.php"><?php echo $name; ?></a> >
        Start Course

    </div>
    </div>
<?php include("includes/offered-courses-aside.php"); ?>
<br /> <br />
 <div class="span-18 last">
<h2 class="alt"><?php echo $title ?></h2>


<?php
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con)
    {
   die('Could not connect: ' . mysql_error());
    }
mysql_select_db($dbname, $con);
if(!isset($_SESSION['current_id'])) {
$result = mysql_query("SELECT * FROM questions WHERE (evaluationid=".$evaluationid.") ORDER BY id ASC LIMIT 1 ");
echo $result;
echo "<form  action='#' method='post'>";
echo "<table border='1' style='width:500px;'> <br />";

while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    $_SESSION['current_id'] = $row['id'];
    echo " <strong>" . $row['question'] . "</strong><br /><br /><br />";
    if ($row['type'] === 'MCQs')
    {

    echo "<input type='radio' value='' />"; 
    echo  $row['possible_opt_1']; echo "<br />";
    echo "<input type='radio' value='' />"; 
    echo  $row['possible_opt_2'];echo "<br />";
    echo "<input type='radio' value='' />";
    echo  $row['possible_opt_3'];echo "<br />";
    echo "<input type='radio' value='' />"; 
    echo  $row['possible_opt_4'];echo "<br /><br /><br />";
    }
    else {
         echo "<input type='radio' value='' />"; 
         echo  $row['possible_opt_1']; echo "<br />";
         echo "<input type='radio' value='' />"; 
         echo  $row['possible_opt_2'];echo "<br />";

            }
            echo "<input type='submit' name='next' value='next' />";
    echo "</tr>";
    }   

echo "</table>";


echo "</form>";

$result1 = mysql_query("SELECT id FROM questions WHERE (evaluationid=".$evaluationid.") ORDER BY id ASC LIMIT 1 ");
while($row = mysql_fetch_array($result1))
    {

         $_SESSION['last_id'] = $row['id'];

     }




}
mysql_close($con);
?>

</div>
</div>
<?php include("../includes/footer.php"); ?>
</div>
</body>

</html>
<?php
    }
    else
    {
        header("Location: ".$fullpath."login/unauthorized.php");

    }
?>
0

is it on live server or on your computer?
and if u click refresh its stays to same qustion or it moves to next question?

0

I do not have time to test your code right away (hopefully I can do it tonight at home). First thing I can't find is session_start() which should be on the beginning of the script. You must add it in order to usesessions.

0

@broj1 thankyou for helping :)

yup actually the file which is included as a config file has already the session_start()

0

it shows that i moved on the next question but next question is not displayed...
im doing the work on xampp

0

One thing you can do is quickly debug the query. Put this code on line 71 (and remuve the echo $result code):

echo $query;

This will display your query on each new page and you can check in phpmyadmin whether the values are correct. You can do this for all queries and other values a s well.

Edited by broj1

0

And also I noticed that the query for last id on line 108 is wron. It shoult be ordered in descending order (DESC) to get the last id.

$result1 = mysql_query("SELECT id FROM questions WHERE (evaluationid=".$evaluationid.") ORDER BY id DESC LIMIT 1 ");
0

i used echo $result to debug the query... but there was no error...
because my query is saved in $result
and te problem is still the same... i've used DESC as well...

on debug it shows this: Resource id #11
and one thing more.. it never shows the question again. i mean it shows the first question once and when i click next it doesnt shows another question and then if i go back to previous page from browser it shows nothing...is that because of sessions?

0

The $result does not show your query, echoing it is pretty much useless. It holds the resource - a special PHP type. So please do as I suggested in my last post. Echo the queries and test them in phpmyadmin.

and then if i go back to previous page from browser it shows nothing...is that because of sessions?

It could be true, I am not sure I should do a test. There are other mechanisms you could use instead of sessions to transfer the current id: a hidden field, a querystring or even a cookie. But session is the most secure way since the visitor can not change the values and skip to other questions.

0

yeah i also want to do it with sessions
please suggest something i have to finsh this work today. im hanging on this question since weeks.. as iam just a beginner

when i write echo $query it says undefined variable

0

when i write echo $query it says undefined variable

Ups, I just realized that you do not use $query to store the query (as I suggested) but you pass it as a string to mysql_query. Well, I have to live right now but I'll come back tonight. In meanwhile you can assign a query to a variable and echo it:

instead of:

$result = mysql_query("SELECT * FROM courses WHERE (id=".$cid.")");

do:

$query = "SELECT * FROM courses WHERE (id=".$cid.")";
echo $query;
$result = mysql_query($query);
0

thankyou so much.. :)
that what i was saying thanx again for the quick response
please check the code if u get time

0
<?php
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con)
    {
   die('Could not connect: ' . mysql_error());
    }
mysql_select_db($dbname, $con);
if(!isset($_SESSION['current_id'])) {
    $_SESSION['current_id']="0";
    }
    $query = "SELECT answer FROM questions WHERE (id=".$_SESSION['current_id'].")";
    $result = mysql_query($query);

    {
        $answer= $row['answer'];
    }

$result = mysql_query("SELECT * FROM questions WHERE (evaluationid=".$evaluationid." AND id>". $_SESSION['current_id'].") ORDER BY id ASC LIMIT 1 ");
echo "<form  action='#' method='post'>";
echo "<table border='1' style='width:500px;'> <br />";

while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    $_SESSION['current_id'] = $row['id'];
    echo " <strong>" . $row['question'] . "</strong><br /><br /><br />";
    if ($row['type'] === 'MCQs')
    {

    echo "<input type='radio' value='' name='answer' checked />"; 
    echo  $row['possible_opt_1']; echo "<br />";
    echo "<input type='radio' value='' name='answer' />"; 
    echo  $row['possible_opt_2'];echo "<br />";
    echo "<input type='radio' value='' name='answer' />";
    echo  $row['possible_opt_3'];echo "<br />";
    echo "<input type='radio' value='' name='answer' />"; 
    echo  $row['possible_opt_4'];echo "<br /><br /><br />";
    }
    else {
         echo "<input type='radio' value='' name='answer' />"; 
         echo  $row['possible_opt_1']; echo "<br />";
         echo "<input type='radio' value='' name='answer' />"; 
         echo  $row['possible_opt_2'];echo "<br />";

            }
            echo "<input type='submit' name='next' value='next' />";
    echo "</tr>";
    }   

echo "</table>";


echo "</form>";

$result1 = mysql_query("SELECT id FROM questions WHERE (evaluationid=".$evaluationid.") ORDER BY id DESC LIMIT 1 ");
while($row = mysql_fetch_array($result1))
    {

         $_SESSION['last_id'] = $row['id'];

     }
mysql_close($con);
?>
<?php
$correct = 0;
$selected_opt=$_POST['answer'];
if ($selected_opt== $row['answer'])
{
    $correct+= 10;
    echo "your answer is right";
}
else 
{
echo "your answer is wrong";    
}

?>

@broj1 this is what i have done so far.. it is now showing 1 question and on selecting next it is showing the next question

now i want to check which question is right and which is wrong.. if question is right it should echo something and add 10 to score and if its wrong it should just print the question is wrong

2) to keep $score updating till last question and then tell student at end of quiz how much score he gained..

i know there will be mistakes in my code as i am a beginner..so pleasse dont get angry..

its echoing "your answer is right" on each page and then giving an error for undefined index answer

iam having a hard time..

Thanx in advance :)

Edited by Riu 2009

0

OK, you posted while I was checking your previous code.

now i want to check which question is right and which is wrong

Well, everything depends on what you want to do. If the question is wrong, should it stay on the same question or move to the next? Where do you want to keep the answers, in a database or in session (they will get lost once the user logs out)? It is good to have some sort of a blueprint before you start coding.

0

if it moves to next question it should go next but tell the user that question is wrong before going to next page.

i want to keep the answers in sessions but i want to keep track of the progress by storing some score points (in some progress table in database) which i could show on each login that the user has earned some points on that course before

0

@broj1 i left this logic.. im doing it the other way..thanx for your help anyways :)

2

I was away until just now. OK, so you will try it other way. My suggestion is that you first outline the logic with comments like:

// check if the user is authorised, if not, redirect

// if first question read the first question, correct answer and possible answers
// otherwise read the next question, correct answer and possible answers

// if user submitted the answer to the previous question store the answer

// compare the answers    
// if the answer was wrong notify the user

// display the page

// ...

Once you have logic done start coding. It will be easier that way and to upgrade logic later. And separate the logic and the html output as much as possible.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.