_php example of a simple paging (reading from a database) using PHP

Source: Internet
Author: User
PHP Novice, has always wanted to do a page to forget, today was fortunate to be reminded of the Internet search a bit. Some 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 and the total number of pages compared to change the status of the connection

4. Use limit control to read records from the database

Here's the code:
Copy CodeThe code is as follows:
$conn = mysql_connect (' localhost ', ' root ', ');
mysql_select_db (' Db_bookstore ', $conn);
mysql_query ("Set names UTF8");
if (Isset ($_get[' page ')))//Determine if the page parameter is present, to get the value of the pages, 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 link
$page _string = "";
if ($page = = 1)
{
$page _string. = "Home | Previous Page ";
}
Else
{
$page _string. = "Home | Previous Page ";
}
$page _string. = "| $page | ";
if ($page = = $page _count)
{
$page _string. = "Next Page | Last ";
}
Else
{
$page _string. = "Next Page | Last ";
}
$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 a very simple way, you road people, if there are other types of methods, please tell it.

  • 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.