PHP Pagination Code Learning sample Sharing

Source: Internet
Author: User

  This article mainly introduces a simple PHP paging code, provided for you to learn, there are comments in the code, you can also directly use the

The code is as follows: <?php      Header ("Content-type:text/html;charset=utf-8");    //database connection     $conn = mysql_connect ("localhost", "root", "Mr") or die ("Not connnected:". Mysql_err or ());     mysql_select_db ("test", $conn);     mysql_query ("Set names UTF8");      //query total number of data     $sql 1 = "SELECT count (*) from user";     $ret 1 = mysql_query ($sql 1);     $row 1 = mysql_fetch_row ($ret 1);     $tot = $row 1[0];       //per page How many rows of data     $length = 5;          //Total pages       $totpage = ceil ($tot/$length);      //Current page     $page = @$_get[' P ']? $_get[' P ']: 1;    //limit Lower bound     $offset = ($page-1) * $LENGTH;       echo "<center>";     echo "<h2>php padding</h2>";     echo "<table width= ' 700px ' border= ' 1px ' >";     Echo"<tr>";     echo "<th>ID</th>";     echo "<th>USER</th>";     echo "<th>PASS</th>";     echo "</tr>";      //To display the query data in the form of     $sql 2 = "SELECT * from user order by ID limit {$offset}, {$length}";     $ret 2 = mysql_query ($sql 2);     while ($row 2 = MYSQL_FETCH_ASSOC ($ret 2)) {        echo "<tr>";     &NBS P   echo "<td>{$row 2[' id ']}</td><td>{$row 2[' name ']}</td><td>{$row 2[' pass ']}< /TD> ";         echo "</tr>";    }       echo "</table>";      //previous page and next     $prevpage = $page-1;     if ($page >= $totpage) {        $nextpage = $totpage;    } else {        $nextpage = $page + 1;    }      //goto     Echo "<h3><a href= ' index.php?p={$prevpage} ' > Page </a>|<a href= ' index.php?p={$nextpage} ' > Next </a></h3> ";     echo "</center>";       Core point:   <1> "$sql 2 =" SELECT * from user order by ID limit {$offset}, {$length} ";", $offset, $ The relationship between length and number of pages.   <2> access to previous and next pages, and critical points.
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.