Home >Backend Development >PHP Problem >How to use PHP to achieve random non-duplicate data in paging
PHP is a scripting language widely used in web development. It is powerful, easy to use and efficient, and is widely used to build various web applications. When developing these applications, it is often necessary to display data in pages, and to ensure that the data is random and not repeated, which requires special attention. This article will introduce how to use PHP to achieve paging random non-duplicate data.
1. Implementation of paging
Paging is a common way of displaying data. Usually we divide a large data set into several small data blocks. Each small data block is called One page. This can not only facilitate display, but also improve page loading speed. In PHP, we can use the LIMIT keyword to implement paging.
The following is a simple paging code example:
<?php $page_size = 10; $page_num = isset($_GET['page_num']) ? $_GET['page_num'] : 1; $start = ($page_num - 1) * $page_size; $sql = "SELECT * FROM my_table LIMIT $start, $page_size"; //执行sql语句并获取数据 ?>
In the above code, $page_size represents the amount of data on each page, $page_num represents the current page number, and $start represents the current page data starting point. Paging can be easily implemented using the LIMIT keyword.
2. Obtain random non-duplicate data
When we need to randomly obtain several pieces of data in a large data set, we can use the RAND() function in MySQL. For example:
SELECT * FROM my_table ORDER BY RAND() LIMIT 10;
The above code indicates that 10 pieces of data are randomly obtained from the my_table table. Since the RAND() function is used, each execution is random and the data is not repeated. However, it is more difficult when this method requires paging, because the results of each execution are different and need to be processed by the program to achieve paging.
The following provides an implementation idea:
1. First obtain the total number of rows of the entire data set and save it to a variable. You can use the following code:
$sql_count = "SELECT COUNT(*) AS count FROM my_table"; //执行sql语句并获取总行数 $count = $result['count'];
2. Calculate the number of data rows to be obtained:
$random_count = $page_size * 3;//获取3页数据的总行数
3. Execute the following code in a loop until enough rows are obtained:
$start = rand(0, $count - 1);//生成随机起始点 $sql = "SELECT * FROM my_table LIMIT $start, $random_count"; //执行sql语句并获取数据
4. Deduplicate and randomly sort the obtained data:
$data = array_unique($data);//去重 shuffle($data);//随机排序
5. Just take out the required number of rows from the obtained data.
Finally, display the data on the page and implement paging. The complete code is as follows:
<?php $page_size = 10;//每页数据量 $page_num = isset($_GET['page_num']) ? $_GET['page_num'] : 1;//当前页数 $random_count = $page_size * 3;//获取3页的数据量 $start = ($page_num - 1) * $page_size;//分页起始行数 $sql_count = "SELECT COUNT(*) AS count FROM my_table";//获取总行数的sql语句 $sql = "SELECT * FROM my_table LIMIT $start, $random_count";//获取随机数据的sql语句 //执行sql语句并获取数据 //计算总行数 $count = $result_count['count']; $data = array(); while (count($data) < $random_count) { $start = rand(0, $count - 1);//生成随机起始点 $sql = "SELECT * FROM my_table LIMIT $start, $random_count"; //执行sql语句并获取数据 $data = array_merge($data, $result_data); } $data = array_unique($data);//去重 shuffle($data);//随机排序 $data_page = array_slice($data, ($page_num - 1) * $page_size, $page_size);//分页 //展示数据 ?>
In this way, paging, random, and non-duplicate data acquisition can be achieved. Of course, there are certain performance issues with this method, because the query statement needs to be executed multiple times, so when the amount of data is large, it may affect the performance of the program.
3. Summary
Paging, random, and non-duplicate data acquisition are very common requirements in the process of web application development. This can be easily achieved through the method described in this article. Of course, in the actual development process, various caching technologies, database optimization and other methods can also be combined to further improve the performance of the program.
The above is the detailed content of How to use PHP to achieve random non-duplicate data in paging. For more information, please follow other related articles on the PHP Chinese website!