php - mysql 下一个和上一个

标签 php mysql next

我目前正在使用 mysql 和 php 从我的大约 1100 条记录的表中显示 9 个随机结果。

是否可以有一个下一个和上一个按钮,即使它是随机的?我查看了此处已发布的几个示例,但它们似乎是特定于应用程序/项目的。这是我的代码的样子..

    function executeQuery($searchKey)
    {

        if ($searchKey == null)
        {
    $query = "SELECT DISTINCT flightNumber, flightCity FROM allFlights LIMIT 0,9";
        //DEBUG -echo "<p>$searchKey</p>";echo "<p>$query</p>";
        }
        else
        {
        //DEBUG -echo "<p>$searchKey</p>";echo "<p>var not null</p>";
           $query = "Select distinct * from allFlights where flightCity LIKE '%$searchKey%' LIMIT 0,9";
        //DEBUG -echo "<p>$searchKey</p>";echo "<p>$query</p>";
        }
   $result=mysql_query($query);
   $numrow=mysql_numrows($result);
   if ($numrow === 0)
   {
    $query = "Select distinct * from allFlights where flightNumber LIKE '%$searchKey%' LIMIT 0,9";
    $result=mysql_query($query);
    $numrow=mysql_numrows($result);
   }
return $result;
     }

       function populate ()
       {
         $searchKey = mysql_real_escape_string($_POST["search"]); //assigns user input to searchKey variable
         //DEBUG -echo "<p>$searchKey</p>";
         $result=executeQuery($searchKey);
         $numrow=mysql_numrows($result);

     if ($numrow == 0)
     {
        echo "<center><p><b> No results found, please try another keyword.</p></center></b>";
     }
     else
     { display results. -- this part i have working.
             }

我更喜欢在加载页面时发生这种情况: -列出了可用航类数量的当前位置。 (现在显示 1100 个航类中的 9 个) 显示 -9 个随机航类。 - 下一个按钮将显示接下来的 9 个航类(随机会很好。) - 上一个按钮将显示前一个(原始)9 个航类(随机会很好。)

说完一切后,我希望能够加载页面以识别 9 个随机航类,按下一步以识别新的 9 个随机航类,然后按上一个并识别原始的 9 个随机航类。

非常感谢任何帮助。

最佳答案

您可以使用 ORDER BY RAND(seed) 来给出 pseudorandom可重复的顺序:

SELECT * 
FROM ....
ORDER BY RAND(9325)
LIMIT 99, 9

调整偏移量以在结果中前后移动。种子可以是任何整数,如果您想重新随机化顺序,您可以更改它,但您必须在按后退或前进时使用相同的种子。

关于php - mysql 下一个和上一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7882594/

相关文章:

php - 将结果添加到 WordPress 搜索结果中

php - Laravel 5.6.28 : Auth Middleware redirects to Login (handle not called)

php - 分页 - 如何不允许用户进一步浏览现有页面?

mysql - 从旧表创建一个表,选择特定行

java - 为什么这种排序在 Solr 中不起作用?

python - 为什么 __next__() 内部的 yield 会返回生成器对象?

php - 在 Laravel 5.1 的 AppServiceProvider 中获取当前路由名称

PHP 转换跟踪

Python:下一个()函数

node.js - nodejs中的next(err)和next(new Error(err))有什么区别?