Getting Started with PHP (16) using MYSQLI to implement simple paging

Source: Internet
Author: User
Tags getting started with php

A few days ago just learned how to use PHP's own API to operate the MySQL database, just take a few pages of this small case practice hands.


There are many algorithms for paging, and here's just a simple one.


The principle of paging:

First get the total number of rows $row_num, divided by the number of rows per page $page_size, rounding up the total number of pages $page_num.

The first record that is easy to get page one is the whole table ($page-1) *page_size records, recorded as $offset

(Note: The row index starts at 0)

Then apply the MySQL limit keyword to get each page of the record results displayed.

SELECT * FROM test limit offset, page_size;

Specific implementation code: two files conn.php indes.php

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/88/44/wKiom1fs0TigiqMMAAASVZgoU3g654.jpg-wh_500x0-wm_3 -wmp_4-s_1641262107.jpg "title=" 1.jpg "alt=" Wkiom1fs0tigiqmmaaasvzgou3g654.jpg-wh_50 "/>

/*conn.php*/<?php$mysqli = new mysqli ("localhost", "root "," 123456 "," MyDB ")  or die (" Database connection failed! ". $mysqli->errno); 
/*index.php*/<?phpinclude_once ("conn/conn.php"), $page  = $_get["page"];if ($page  ==  "") {      $page  = 1;} if (Is_numeric ($page))  {     $page _size = 4;     $res  =  $mysqli->query ("Select * from test");     $row _count =   $res->num_rows;     $page _num = ceil ($row _count /  $page _size);      $offset  =  ($page  - 1)  *  $page _size;      $res  =  $mysqli->query ("select * from test limit  $offset, $page _size ");    while  ($row  =  $res->fetch_assoc ())  {         echo  "<table><tr>";         echo  "<td>". $row ["id"]. " </td> ";         echo  "</table>";    }}echo  "Total". $page _num. " Page record ";echo " <br> "if ($page  != 1)  {    echo " <a href= Index.php?page=1> Home </a> "//index.php path without quotation marks can also     echo " <a href= Index.php?page= ". ($page  - 1). " > Prev </a> ";//If the expression is not a direct variable, it must be calculated after use. Connection}for ($i =1; $i <= $page _num; $i + +) {    echo   "<a href=index.php?page=". $i. " > Page $i  &nbsp;</a> ";} if ($page  <  $page _num)  {    echo  "<a href=index.php?page=". ( $page  + 1). " > Next </a> ";    echo " <a href=index.php?page= $page _num> last </a> ";}


This article is from the "thick Product Thin Hair" blog, please make sure to keep this source http://joedlut.blog.51cto.com/6570198/1857808

Getting Started with PHP (16) using MYSQLI to implement simple paging

Related Article

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.