Ahmed Yasen
Ahmed Yasen

Reputation: 145

how can I get the next row from the database?

Following is the script for a quiz. I want to fetch the questions with it's options. When I click on submit button, then the next question with it's options should be displayed , my database (quiz) have two tables as follows :

questions 
________________________________________________________________
|qid[primary key] |questions[varchar(255)]|
|1                |Quest1                 |
|2                |Quest2                 |
|3                |Quest3                 |
|4                |Quest4                 |

answers
|id[primary key]  |answer                 |qid[foreign key] 
|1                |answer1                |1                 
|2                |answer2                |1
|3                |answer3                |1
|4                |answer4                |1
|5                |answer1                |2                 
|6                |answer2                |2
|7                |answer3                |2
|8                |answer4                |2
|9                |answer1                |3                 
|2                |answer2                |3
|3                |answer3                |3
|4                |answer4                |3

Following is my script

$connection = mysqli_connect('localhost' , 'root' , '' , 'quiz');
<?php
$qquery = "SELECT * FROM questions" ;
$ques = mysqli_query($connection , $qquery);
$qrow = mysqli_fetch_assoc($ques);

$aquery = "SELECT * FROM answers INNER JOIN questions WHERE answers.qid = questions.qid " ;
$ans = mysqli_query($connection , $aquery);
?>
<html>
<body>
<form method = "post" id = 'qform' action = 'process.php'>
<h3> <?= $qrow['question']; ?></h3>

<?php while($arow = mysqli_fetch_assoc($ans) ):
if($qrow['qid'] == $arow['qid']):
?>
<input type = "radio" name = "<?= $arow['qid'] ?> " value = "<?=  $arow['degree'] ?>" ><?= $arow['answer'] ?><br />
<?php endif ; endwhile; ?>
<br />
<input type = 'submit' name = 'submit' value = 'NEXT' >
<form>

Upvotes: 0

Views: 86

Answers (2)

Ninju
Ninju

Reputation: 2530

First get the first Questions from data base bye

 $qquery = "SELECT * FROM questions WHERE id = (select min(id) from questions)" ;

next:

select * FROM questions where id = (select min(id) from questions where id > $_post['qid']);

previous:

select * FROM questions where id = (select max(id) from questions where id < $_post['qid']);

Upvotes: 2

Ninju
Ninju

Reputation: 2530

You can use JavaScript for this purpose.
You have to keep the question and answers in a php multidimensional array, then json_encode that array and pass to javascript.Then you can get a Object. On clicking next button pass the index and get the next question.

Upvotes: 0

Related Questions