具有隨機排序的PHP MySQL 分頁
在網站上實現帶分頁的搜尋功能時,解決與隨機排序相關的問題至關重要結果的排序。本文提供了以下挑戰的解決方案:
範例:
<code class="php"><?php session_start(); // Generate a unique seed value for the first page if (!isset($_SESSION['seed'])) { $_SESSION['seed'] = rand(1, 1000); } // Exclude previously seen results $excludedIds = array(); if (isset($_SESSION['seenResults'])) { $excludedIds = $_SESSION['seenResults']; } // Query with random ordering and exclusion $query = "SELECT * FROM table WHERE id NOT IN ('" . implode("', '", $excludedIds) . "') ORDER BY RAND(" . $_SESSION['seed'] . ") LIMIT 10";</code>
以上是如何在 PHP 和 MySQL 中實作隨機排序分頁?的詳細內容。更多資訊請關注PHP中文網其他相關文章!