如何从数据库中获取下一行?

时间:2015-09-18 06:07:15

标签: php mysql

以下是测验的脚本。我想用它的选项来获取问题。当我点击提交按钮时,应该显示带有它的选项的下一个问题,我的数据库(测验)有两个表如下:

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

以下是我的脚本

$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>

    

2 个答案:

答案 0 :(得分:2)

首先从数据库中获取第一个问题再见

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

下:

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

前面的:

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

答案 1 :(得分:0)

您可以将JavaScript用于此目的。
您必须在php多维数组中保留问题和答案,然后json_encode该数组并传递给javascript。然后您就可以获得一个Object。单击下一步按钮会传递索引并获得下一个问题。