PHP MySQL Pagination with Random Ordering
When implementing search functionality with pagination on a website, it is crucial to address issues related to the random ordering of results. This article provides solutions to the following challenges:
Example:
<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>
The above is the detailed content of How to Implement Pagination with Random Ordering in PHP and MySQL?. For more information, please follow other related articles on the PHP Chinese website!