Can anyone tell me how to retrieve records from database one at a time..
I am attaching a sample code here:

include('connection.php');
$sql1="SELECT * FROM question_master";
$result=mysql_query($sql1);
while($row = mysql_fetch_array($result))
{
echo $row['Question'];
}

Let me explain the code..

There is a table named "question_master" (containing some questions) in database with a field named "Question".
This code display all the questions stored in the table into the screen.

My problem is here

I want to display the questions one by one. Like when we click the next button it will display the next question and so until all the records are displayed.

Help me out with the main logic I should use to implement it. Or help me with the script.

This should work:
First Question:

include('connection.php');
$sql1="SELECT * FROM question_master limit 1";
$result=mysql_query($sql1);
while($row = mysql_fetch_array($result))
{
echo $row['Question'];
}

2nd Question:

include('connection.php');
$sql1="SELECT * FROM question_master LIMIT 1 OFFSET 1";
$result=mysql_query($sql1);
while($row = mysql_fetch_array($result))
{
echo $row['Question'];
}

Third Question:

include('connection.php');
$sql1="SELECT * FROM question_master LIMIT 1 OFFSET 2";
$result=mysql_query($sql1);
while($row = mysql_fetch_array($result))
{
echo $row['Question'];
}

etc.

A better way to do this is to use loop
take a variable (let it be 'a') and initialize it equal to 1.
Then inside loop use this statement:

$sql1="SELECT * FROM question_master LIMIT 1 OFFSET '$a' ";
'instead of above line you can also like query as
'$sql1="SELECT * FROM question_master LIMIT '$a',1 ";
$result=mysql_query($sql1);
while($row = mysql_fetch_array($result))
{
echo $row['Question'];
}

after that make a submit button and on it's click event run a javascript to increment the value of the variable by 1.
Repeat this loop until it is equal to the max number of questions...

OR

Other way is to use POST method of form....

<?php
echo "<form method='POST' action=''>";
if(!isset($a))
{
$_POST['a']=1
}
else
{

}
include('connection.php');
$sql1="SELECT * FROM question_master LIMIT '$_POST['a'],1";
$result=mysql_query($sql1);
while($row = mysql_fetch_array($result))
{
echo $row['Question'];
}
$b=$_POST['a']+1;
echo "<input type='hidden' value='$b' name='a'>";
echo "<input type='submit' value='submit' name='submit'>";
?>

For the second alternative it may contain certain error as i havenot checked that command for now....it was just for your knowledge purpose...

commented: Quite good altternatives to work on... +0
Member Avatar for diafol

You could use ajax to help you here:

1. hidden form field with q no.
2. next button click: js function reads form fields (inc. hidden field).
3. js passes data to php script
4. php script takes data, stores answers, retrieves next question (based on value from hidden field)
5. output passed back to js script.
6. js updates the question area in the page.

ALthough this looks complciated, it's a trivial operation - especially if you use something like jQuery.

Just a thought.

@all Thanks to all of you for the suggestions.
If I use LIMIT 1 and OFFSET as variable, it will display the 1st row first and then 2nd row (after incrementing the OFFSET using javascript) and so on from the table. What if I want the questions to be displayed randomly using rand().

Member Avatar for diafol
$q = shuffle(range(0,19));

Will give you randomized offsets between 0 and 19 for the first 20 questions. You need to keep this alive between questions, so few ways:

easiest may be to shove $q as a session variable.

@ardav But how will I use your code in the following code?

<?php
	if(isset($_POST['next']))
	{
		$a=$_POST['a'];
	}
	if(!isset($a))
	{
		$a=0;
	}
	include('connection.php');
	$sql1="SELECT * FROM question_master WHERE Sub_Id='9' LIMIT 1 OFFSET $a";
	$result=mysql_query($sql1);
	echo "<form method='post' action=''>";
	while ($row = mysql_fetch_array($result))
	{
		echo $row['Question']. "<br/>";
		echo "<input type='radio' value='answer1' name='answer'>" .$row['Answer1'];
		echo "<input type='radio' value='answer1' name='answer'>" .$row['Answer2'];
		echo "<input type='radio' value='answer1' name='answer'>" .$row['Answer3'];
		echo "<input type='radio' value='answer1' name='answer'>" .$row['Answer4']. "<br/>";
	}
	$b=$a+1;
	echo "<input type='hidden' value='$b' name='a'>";
	echo "<input type='submit' name='next' value='next'> ";
	echo "<input type='reset' name='reset' value='Reset'>";
	echo "</form>";
?>

This code is working properly without randomization.

Member Avatar for diafol

quiz intialize:

session_start();
$q = shuffle(range(0,19));
$_SESSION['q'] = $q;

THen in your display page:

<?php
        session_start(); //MUST BE AT TOP OF PAGE
	if(isset($_POST['next'])){
		$a=$_POST['a'];
	}
	if(!isset($a)){
		$a=0;
	}
        $q = $_SESSION['q']['a'];
	include('connection.php');
	$sql1="SELECT * FROM question_master WHERE Sub_Id='9' LIMIT 1 OFFSET $q";
	$result=mysql_query($sql1);
	echo "<form method='post' action=''>";
	while ($row = mysql_fetch_array($result))
	{
		echo $row['Question']. "<br/>";
		echo "<input type='radio' value='answer1' name='answer'>" .$row['Answer1'];
		echo "<input type='radio' value='answer1' name='answer'>" .$row['Answer2'];
		echo "<input type='radio' value='answer1' name='answer'>" .$row['Answer3'];
		echo "<input type='radio' value='answer1' name='answer'>" .$row['Answer4']. "<br/>";
	}
	$a=$a+1;
	echo "<input type='hidden' value='$a' name='a'>";
	echo "<input type='submit' name='next' value='next'> ";
	echo "<input type='reset' name='reset' value='Reset'>";
	echo "</form>";
?>
commented: nice work man +0

@ardav Thanks a lot for the help.. I'll try it...

Another little help...

In the display page I am using radio button for the answers. How will I store the results that the users checked so that I can evaluate them after clicking the final submit button (the submit button after the last question)?

Member Avatar for diafol

you can use session again.

echo "<input type='radio' value='1' name='answer'>" .$row['Answer1'];
		echo "<input type='radio' value='2' name='answer'>" .$row['Answer2'];
		echo "<input type='radio' value='3' name='answer'>" .$row['Answer3'];
		echo "<input type='radio' value='4' name='answer'>" .$row['Answer4'].

Then

$ans = $_POST['answer'];
array_push($_SESSION['ans'],$ans);

then art the end use the two session arrays to build a SQL statement.

how to code of previous button.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.