PHP Paging function code (simple and practical type)

Source: Internet
Author: User
Prepare 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, for example:
1 1 roaches
2 0 Little Red
3 0 Little Li
4, 1 soldiers.
5 1 Zhang Three
6 0 Dick
7 0 Wu Xin

Write PHP statements (index.php):
Copy CodeThe code is as follows:
<?php
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 altogether $rows record";
$pagenav. = "<a href="? page=1 "> Home </a>";
$pagenav. = "<a href=" page= $pre _page "> Prev </a>";
$pagenav. = "<a href=" page= $next _page "> After </a>";
$pagenav. = "<a href="? page= $page _count "> Last </a>";
$pagenav. = "Jump to <select name=" ToPage "size=" 1 "page=\" +this.value ">\n";
for ($i =1; $i <= $page _count; $i + +) {
if ($i = = $page) $pagenav. = "<option value=" $i "selected> $i </option>\n";
else $pagenav. = "<option value=" $i "> $i </option>\n";
}
}//Page paging function
Using the sample
if (! $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 retrieving, $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, as shown in figure:

It's time to say Byebye, it really works!

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.