What is the principle of paging?
Data paging requires the following conditions:
1. The total number of items participating in paging [$msg_count], this value can be obtained through database query;
2. The number of items displayed on each page [$pagesize], this value is defined by yourself;
3. The number of pages of the current page [$page], this value is transmitted and received through the address bar;
4. The total number of pages [$pagecount] can be calculated from the above information. Here you need to use ceil();
[$pagecount = ceil($msg_count/$pagesize);]
5. Database query uses [limit] in the sql statement to realize data changes:
For example:
select * from table name where condition limit $startnum, $pagesize;
And $startnum = ($page-1)*$pagesize;
Example:
/** * 取得上次的过滤条件 * @param string $param_str 参数字符串,由list函数的参数组成 * @return 如果有,返回array('filter' => $filter, 'sql' => $sql);否则返回false */ function get_filter($param_str = '') { $filterfile = basename(PHP_SELF, '.php');//string basename ( string $path [, string $suffix ] ) 返回路径中的文件名部分如果文件名是以 suffix 结束的,那这一部分也会被去掉。 if ($param_str) { $filterfile .= $param_str; } if (isset($_GET['uselastfilter']) && isset($_COOKIE['ECSCP']['lastfilterfile']) && $_COOKIE['ECSCP']['lastfilterfile'] == sprintf('%X', crc32($filterfile))) { return array( 'filter' => unserialize(urldecode($_COOKIE['ECSCP']['lastfilter'])), 'sql' => base64_decode($_COOKIE['ECSCP']['lastfiltersql']) ); } else { return false; } }
/** * 保存过滤条件 * @param array $filter 过滤条件 * @param string $sql 查询语句 * @param string $param_str 参数字符串,由list函数的参数组成 */ function set_filter($filter, $sql, $param_str = '') { $filterfile = basename(PHP_SELF, '.php'); if ($param_str) { $filterfile .= $param_str; } setcookie('ECSCP[lastfilterfile]', sprintf('%X', crc32($filterfile)), time() + 600); setcookie('ECSCP[lastfilter]', urlencode(serialize($filter)), time() + 600); setcookie('ECSCP[lastfiltersql]', base64_encode($sql), time() + 600); }
/** * 供货商资源管理 * @param bool $is_pagtion * @return array $arr */ function suppliers_resource_manage($is_pagtion=true) { global $db,$ecs; $result = get_filter(); if ($result === false) { $aiax = isset($_GET['is_ajax']) ? $_GET['is_ajax'] : 0; /* 过滤信息 */ $filter['sort_by'] = empty($_REQUEST['sort_by']) ? 'r.resource_id' : trim($_REQUEST['sort_by']); $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']); $filter['resource_id'] = empty($_REQUEST['resource_id']) ? '' : $_REQUEST['resource_id']; $filter['admin_name'] = empty($_REQUEST['admin_name']) ? '' : trim($_REQUEST['admin_name']); $filter['resource_name'] = empty($_REQUEST['resource_name']) ? '' : trim($_REQUEST['resource_name']); $filter['admin_id'] = isset($_REQUEST['admin_id'])?intval($_REQUEST['admin_id']):'-1'; $filter['resource_type'] = empty($_REQUEST['resource_type']) ? '-1' : intval($_REQUEST['resource_type']); $filter['resource_status'] = empty($_REQUEST['resource_status']) ? '-1' : intval($_REQUEST['resource_status']); $filter['resource_rank'] = empty($_REQUEST['resource_rank']) ? '-1' : intval($_REQUEST['resource_rank']); $filter['resource_key'] = empty($_REQUEST['resource_key']) ? '' : trim($_REQUEST['resource_key']); $filter['menuid'] = 7; $where = 'WHERE 1 '; /* 分页大小 */ $filter['page'] = empty($_REQUEST['page']) || (intval($_REQUEST['page']) <= 0) ? 1 : intval($_REQUEST['page']); if (isset($_REQUEST['page_size']) && intval($_REQUEST['page_size']) > 0) { $filter['page_size'] = intval($_REQUEST['page_size']); } elseif (isset($_COOKIE['ECSCP']['page_size']) && intval($_COOKIE['ECSCP']['page_size']) > 0) { $filter['page_size'] = intval($_COOKIE['ECSCP']['page_size']); } else { $filter['page_size'] = 15; } if(!empty($filter['resource_id'])) { $resource_id = $filter['resource_id'] == -1 ? 0 : $filter['resource_id']; $where .= " and r.resource_id='".$resource_id."'"; } if($filter['resource_name']){ $where .= " AND r.resource_name like '%".mysql_like_quote($filter['resource_name'])."%' "; } if($filter['admin_id'] > -1) { $where .= " AND r.admin_id = '".$filter['admin_id']."' "; } if ($filter['resource_type'] > -1) { $where .= " AND r.resource_type = '" . $filter['resource_type'] . "'"; } if ($filter['resource_status'] > -1) { $where .= " AND r.resource_status = '" . $filter['resource_status'] . "'"; } if ($filter['resource_rank'] > -1) { $where .= " AND r.resource_rank = '" . $filter['resource_rank'] . "'"; } if($filter['resource_key']){ $where .= " AND r.remark like '%".mysql_like_quote($filter['resource_key'])."%' "; } /* 记录总数 */ $sql = "SELECT COUNT(r.resource_id) FROM " . $ecs->table('suppliers_resource')." as r LEFT JOIN ".$ecs->table('admin_user') . " as u ON u.user_id = r.admin_id " . $where; $filter['record_count'] = $db->getOne($sql); $filter['page_count'] = $filter['record_count'] > 0 ? ceil($filter['record_count'] / $filter['page_size']) : 1; /* 查询 */ $sql = "SELECT r.resource_id, u.user_name as admin_name, r.resource_name, r.resource_link, r.resource_type, r.resource_status, r.resource_rank, r.remark, r.add_time FROM " . $ecs->table('suppliers_resource') . " as r LEFT JOIN " . $ecs->table('admin_user') . " as u ON u.user_id = r.admin_id " . $where; $sort_by = $filter['sort_by']; $sort_order = $filter['sort_order']; $sql .="GROUP BY r.resource_id ORDER BY " . $sort_by . " " . $sort_order; if($is_pagtion) { $sql .=" LIMIT ".($filter['page'] - 1)*$filter['page_size'].",".$filter['page_size']; } set_filter($filter, $sql); } else { $sql = $result['sql']; $filter = $result['filter']; } $query=$sql; $row = $db->getAll($sql); /* 格式话数据 */ foreach ($row AS $key => $value) { if ($row[$key]['resource_type'] == 1) { $row[$key]['resource_type'] = '中模'; }elseif ($row[$key]['resource_type'] == 2) { $row[$key]['resource_type'] = '泳装'; }elseif ($row[$key]['resource_type'] == 3) { $row[$key]['resource_type'] = '阿里'; }elseif ($row[$key]['resource_type'] == 2) { $row[$key]['resource_type'] = '17网'; } if ($row[$key]['resource_status'] == 1) { $row[$key]['resource_status'] = '已审核'; }elseif ($row[$key]['resource_status'] == 2) { $row[$key]['resource_status'] = '已弃用'; } if ($row[$key]['resource_rank'] == 1) { $row[$key]['resource_rank'] = 'A'; }elseif ($row[$key]['resource_rank'] == 2) { $row[$key]['resource_rank'] = 'B'; }elseif ($row[$key]['resource_rank'] == 3) { $row[$key]['resource_rank'] = 'C'; } if(strpos($row[$key]['resource_link'], 'http://') === false) { if (strpos($row[$key]['resource_link'], 'https://') === false) { $row[$key]['resource_link'] = substr_replace($row[$key]['resource_link'], 'http://', 0, 0); } } } $arr = array('result' => $row, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count'],'query'=>$query); return $arr; }
Related recommendations:
Examples of paging principles in php
Detailed explanation of php paging principle
The above is the detailed content of What is the principle of paging?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



ccsvchst.exe is a common process file that is part of the Symantec Endpoint Protection (SEP) software, and SEP is an endpoint protection solution developed by the well-known network security company Symantec. As part of the software, ccsvchst.exe is responsible for managing and monitoring SEP-related processes. First, let’s take a look at SymantecEndpointProtection(

Analysis of the role and principle of nohup In Unix and Unix-like operating systems, nohup is a commonly used command that is used to run commands in the background. Even if the user exits the current session or closes the terminal window, the command can still continue to be executed. In this article, we will analyze the function and principle of the nohup command in detail. 1. The role of nohup: Running commands in the background: Through the nohup command, we can let long-running commands continue to execute in the background without being affected by the user exiting the terminal session. This needs to be run

Principle analysis and practical exploration of the Struts framework. As a commonly used MVC framework in JavaWeb development, the Struts framework has good design patterns and scalability and is widely used in enterprise-level application development. This article will analyze the principles of the Struts framework and explore it with actual code examples to help readers better understand and apply the framework. 1. Analysis of the principles of the Struts framework 1. MVC architecture The Struts framework is based on MVC (Model-View-Con

Dual-core browser is a browser software that integrates two different browser cores. The kernel is the core part of the browser, responsible for rendering web content and executing web scripts and other functions. Traditional browsers generally use only a single kernel, such as IE browser using Trident kernel, Chrome browser using WebKit/Blink kernel, Firefox browser using Gecko kernel, etc. The dual-core browser integrates two different cores into one browser, and users can freely switch between them as needed. The emergence of dual-core browsers

MyBatis is a popular Java persistence layer framework that is widely used in various Java projects. Among them, batch insertion is a common operation that can effectively improve the performance of database operations. This article will deeply explore the implementation principle of batch Insert in MyBatis, and analyze it in detail with specific code examples. Batch Insert in MyBatis In MyBatis, batch Insert operations are usually implemented using dynamic SQL. By constructing a line S containing multiple inserted values

The RPM (RedHatPackageManager) tool in Linux systems is a powerful tool for installing, upgrading, uninstalling and managing system software packages. It is a commonly used software package management tool in RedHatLinux systems and is also used by many other Linux distributions. The role of the RPM tool is very important. It allows system administrators and users to easily manage software packages on the system. Through RPM, users can easily install new software packages and upgrade existing software

MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis

The chage command in the Linux system is a command used to modify the password expiration date of a user account. It can also be used to modify the longest and shortest usable date of the account. This command plays a very important role in managing user account security. It can effectively control the usage period of user passwords and enhance system security. How to use the chage command: The basic syntax of the chage command is: chage [option] user name. For example, to modify the password expiration date of user "testuser", you can use the following command
