我有PHP查询来显示来自MYSQL数据库的结果。我使用Swiper jquery插件在每个幻灯片页面上显示9个结果(有3张幻灯片)。然后,当有人转到下一页时,将显示下一个9。这是我用来完成此操作的PHP查询。

$query_page_1 = mysqli_query( $connection, "SELECT * FROM tblclients  WHERE tblclients.package =  'standard' ORDER BY RAND() LIMIT 0, 9");
$query_page_2 = mysqli_query( $connection, "SELECT * FROM tblclients  WHERE tblclients.package =  'standard' ORDER BY RAND() LIMIT 9, 9");
$query_page_3 = mysqli_query( $connection, "SELECT * FROM tblclients  WHERE tblclients.package =  'standard' ORDER BY RAND() LIMIT 18, 9");


我的问题是,当我使用ORDER BY RAND()将每一页上的结果随机化时,它将在第1页到第2页上重复某些相同的结果。

最佳答案

为了解决这个问题,您需要在页面上提供一个公共种子。例如:

$query_page_1 = mysqli_query( $connection, "SELECT * FROM tblclients  WHERE tblclients.package =  'standard' ORDER BY RAND(42) LIMIT 0, 9");
$query_page_2 = mysqli_query( $connection, "SELECT * FROM tblclients  WHERE tblclients.package =  'standard' ORDER BY RAND(42) LIMIT 9, 9");
$query_page_3 = mysqli_query( $connection, "SELECT * FROM tblclients  WHERE tblclients.package =  'standard' ORDER BY RAND(42) LIMIT 18, 9");


其中42是常见的随机种子。

07-24 18:00