PHP Paging function code (simple and practical) _php tutorial
Source: Internet
Author: User
Prepare the data:
Create a new database test
Execute the following statement (Create a new table Test:id, sex, name three fields)
CREATE TABLE ' Test ' (
' ID ' INT (4) Not NULL auto_increment PRIMARY KEY,
' Sex ' INT (1) Not NULL,
' Name ' VARCHAR (not NULL) ENGINE = MYISAM CHARACTER SET UTF8 COLLATE utf8_bin;
Add data to the test table, such as:
1 1 Xiao Qiang
2 0 Little Red
3 0 Little Li
4 1 Creeps
5 1 Zhang Three
6 0 John Doe
7 0 Wu believes
Write PHP statement (index.php):
Copy CodeThe code is as follows:
Page Paging function
$page = $_get["page"];
function Page ($rows, $page _size) {
Global $page, $select _from, $select _limit, $pagenav;
$page _count = ceil ($rows/$page _size);
if ($page <= 1 | | $page = = ") $page = 1;
if ($page >= $page _count) $page = $page _count;
$select _limit = $page _size;
$select _from = ($page-1) * $page _size. ', ';
$pre _page = ($page = = 1)? 1: $page-1;
$next _page= ($page = = $page _count)? $page _count: $page + 1;
$pagenav. = "$page/$page _count page Total $rows records";
$pagenav. = "Home";
$pagenav. = "Previous page";
$pagenav. = "Next page";
$pagenav. = "Last";
$pagenav. = "Jump to \ n ";for ($i =1; $i <= $page _count; $i + +) {if ($i = = $page) $pagenav. = "$i\ n ";else $pagenav. = "$i\ n ";}}//Page paging functionUsing the exampleif (! $conn = mysql_connect ("localhost", "root", "root") die (' Database selection failed! ');if (!mysql_select_db ("Test", $conn)) Die (' Database selection failed! ');mysql_query (' Set names GBK ');Use the page function to calculate $select _from from which record to start the retrieval, $pagenav output paging navigation$rows = mysql_num_rows (mysql_query ("SELECT * from Test");Page ($rows, 2);$sql = "SELECT * FROM Test limit $select _from $select _limit";$rst = mysql_query ($sql);while ($row = Mysql_fetch_array ($rst)) {echo "$row [ID]-$row [sex]-$row [name]";}Echo $pagenav;?> Browse index.php pages,Should say Byebye, really good!PHP Simple page Paging functionWrite a PHP simple paging function, database calls are also written into the inside, the use of people can be deleted by themselves!Copy CodeThe code is as follows:function Getask () {$sql = "SELECT * from Cms_ask where ansower <> '";We're going to change the method here.$q _sq = mysql_query ($sql);$count = mysql_num_rows ($q _sq);$page _size = 8;$page _current = isset ($GLOBALS [' page '])? Intval ($GLOBALS [' page ']): 1;$page _count = ceil ($count/$page _size);$page _start = $page _current-4;$page _end = $page _current + 4;if ($page _current < 5) {$page _start = 1;$page _end = 5;}if ($page _current > $page _count-4) {$page _start = $page _count-8;$page _end = $page _count;}if ($page _start < 1)$page _start = 1;if ($page _end > $page _count)$page _end = $page _count;$pagebar = "";$sql = "SELECT * from Cms_ask where Ansower <> ' ORDER BY id desc limit '. (($page _current-1) * $page _size). "," . $page _size;$row = GetAll ("$sql"), $this, user;foreach ($row as $v) {Echo 'Q: '. $v ["question"]. 'Answer: '. $v ["Ansower"].date ("y-m-d h:i:s", $v ["Posttime"]). '';}$pagebar. = "";$pagebar. = "";if ($page _current! = 1) {$pagebar. = 'First';}for ($i = $page _start; $i <= $page _end; $i + +) {if ($i = = $page _current) {$pagebar. = "" . $i. "";} else {$pagebar. = "" . $i. "";}}if ($page _current! = $page _count) {$pagebar. = "END";}$pagebar. = "";$pagebar. = "";Echo $pagebar;}Http://www.bkjia.com/PHPjc/322617.htmlWww.bkjia.comTrueHttp://www.bkjia.com/PHPjc/322617.htmlTecharticlePrepare data: Create a new database test execute the following statement (Create a new table Test:id, sex, name three fields) CREATE TABLE ' Test ' (' ID ' INT (4) not NULL auto_incremen ...
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service