How to make simple paging (read records from database) in PHP _php example

Source: Internet
Author: User

PHP Novice, has been trying to do a page to forget, today is fortunate to be reminded so the Internet search a bit. Some of them do not understand and do not see how to read. Finally find a relatively simple.

The general idea is:

  1. Set the maximum number of records to display per page.

2. Calculate the total number of pages

3. The current page compares the total number of pages to change the status of the connection

4. Read records from the database with limit control

Here's the code:

Copy Code code as follows:

$conn = mysql_connect (' localhost ', ' root ', ');
mysql_select_db (' Db_bookstore ', $conn);
mysql_query ("Set names UTF8");
if (Isset ($_get[' page '))//To determine if there is a page parameter, get the pages value, otherwise take 1
{
$page = intval ($_get[' page '));
}
Else
{
$page = 1;
}
$page _size = 2; Maximum number of record bars
$sql = "SELECT count (*) as amount from Tb_bookinfo";
$result = mysql_query ($sql);
$row = Mysql_fetch_array ($result);
Calculate Total Pages
$amount = $row [' Amount '];
if ($amount)
{
if ($amount < $page _size) {$page _count = 1;}
if ($amount% $page _size) {$page _count = (int) ($amount/$page _size) + 1;}
else{$page _count = $amount/$page _size;}
}
Else
{
$page _count = 0;
}
Page links
$page _string = "";
if ($page = = 1)
{
$page _string. = "Home | Previous Page ";
}
Else
{
$page _string. = "<a href= '? page=1" > Home </a> | <a href= '? page= '. ($page-1). "' > Prev </a> ";
}
$page _string. = "| $page | ";
if ($page = = $page _count)
{
$page _string. = "Next Page | Last ";
}
Else
{
$page _string. = "<a href= '? page=". ($page + 1). "' > next Page </a> | <a href= '? page= $page _count ' > Last </a> ';
}
$sql = "SELECT * from table ORDER BY id desc limit". ($page-1) * $page _size. ", $page _size";
$result = mysql_query ($sql);
while ($row = Mysql_fetch_row ($result)) {
$rowset [] = $row;
}
? >

This is just a very simple way, you people of the road, if there are other types of methods, please tell me.

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.