简体   繁体   English

下一个和上一个mysql数据库记录随机

[英]next and previous mysql database records randomly

i am working on a computer based test, and am unable to get question from the database randomly with the next button displaying a question that has not been previously displayed earlier and the previous button displaying the previously displayed question. 我正在进行基于计算机的测试,并且无法从数据库中随机获取问题,而下一个按钮显示的是先前未显示的问题,而前一个按钮显示的是先前显示的问题。 I have this code please can anybody help me out. 我有此代码,任何人都可以帮助我。

$specqtn = mysql_query("SELECT * FROM question_reg 
WHERE courseid = '$id'") or die (mysql_error());    
while ($specqtnrow = mysql_fetch_array($specqtn)){
    $qtnid = $specqtnrow['qtnid']."<br>";
} 
echo "<tr><td>Question</td><td>$qtncontent</td></tr>";
while ($i < $count){
    $sql = mysql_query("SELECT * FROM mdlopt 
    WHERE qtnid = $qtnid ORDER BY RAND()") or die (mysql_error()); 
    while($sqlrow = mysql_fetch_array($sql)){
        $count = mysql_num_rows($sql);
        $optid = $sqlrow['optid'];
        $optval = $sqlrow['optval']."<br>";
        echo "<tr><td></td><td><input name='radiobutton' type='radio'    value='$qtnid'/>$optval</td></tr>";
        $i++;
    }
}
echo "<tr><td></td><td><input name='Previous' type='submit' id='Previous'  value='Previous' />";
echo "<input type='submit' name='Next' value='next'>";
echo "<input type='submit' name='Submit' value='Submit' /></td></tr>";

Hi in your code you are retrieving all the rows from the table. 嗨,在您的代码中,您正在检索表中的所有行。 just put limit in your sql query you will get what you are looking for. 只要在您的sql查询中放入limit,您就会得到您想要的东西。

Depends on your definition of "next" and "previous". 取决于您对“下一个”和“上一个”的定义。 I assume that it is the order of courseid . 我认为这是courseid的顺序。 To get the next row you can write a query like this: 要获得下一行,您可以编写如下查询:

SELECT * FROM question_reg
WHERE courseid > '$id'
ORDER BY courseid ASC
LIMIT 1

And this query to get the previous one: 而这个查询得到上一个:

SELECT * FROM question_reg
WHERE courseid < '$id'
ORDER BY courseid DESC
LIMIT 1

I think there are a few things you need to consider. 我认为您需要考虑一些事项。

  1. You need to mix the DB with some application logic. 您需要将数据库与某些应用程序逻辑混合在一起。 Why? 为什么? Because you need to keep track of the history of displayed questions (Maybe a list containing the question and its selected answer). 因为您需要跟踪显示的问题的历史记录(可能是包含问题及其选定答案的列表)。

  2. Now that you have the already displayed questions you need to use them in order to DISJOINT the Questions on DB and Questions Solved. 现在,你有已经显示的问题,你需要以不相交解决在DB和问题中的问题来使用它们。 (Just like a set intersection). (就像设置交集一样)。 Why in your App logic. 为什么在您的应用逻辑中。 Well, because you will be working with random select on db so you don't have any record of what was returned. 好吧,因为您将在db上使用随机选择,所以您没有任何记录。

  3. Depending on the number of questions to solve I'ld suggest you to get at first the set of questions and cache them. 根据要解决的问题数量,我建议您首先获取一组问题并进行缓存。 Then, if you need to persist state (if there's some error or the user get disconnected) its easy. 然后,如果您需要保持状态(如果有错误或用户断开连接),那么这很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM