PHP entry-level paging function

Source: Internet
Author: User
The code is as follows Copy Code

Function page ($sql, $pagesize =10, $class = "") {

if (!is_string ($sql)) {return 0;exit;}
$result =mysql Tutorial _query ($sql);
if (! $result) {return 0;exit;}
if (! $recordnum =mysql_num_rows ($result)) {return 0;exit;}
$totalpage =ceil ($recordnum/$pagesize);
$page = (int) $_get[' page '];
if ($page <=1) $page = 1;
if ($page >= $totalpage) $page = $totalpage;
$recordno = ($page-1) * $pagesize;
$sql. = "Limit $recordno, $pagesize";
$result =mysql_query ($sql);
$url =$_server["Php_self"];

if ($page ==1) $str = "Home page | prev";
else $str. = "<a href=" $url? ". $class. " &page=1 "> Home </a>|<a href=" $url? ". $class. " &page= ". ($page-1). "" > Prev </a> ";
if ($page = = $totalpage) $str. = "| next page | last";
else $str. = "|<a href=" $url? ". $class. " &page= ". ($page + 1). "" > next page </a>|<a href= "$url?". $class. " &page= $totalpage "> Last </a>";
$str. = "$totalpage page of the current $page page";
Return Array ($result, $STR);
}
Call method

$sql = "SELECT * from Images order by IMiD";
$result =page ($sql, 16);

/*
The simple original is to read from the database through the tutorial to reading the corresponding more records, so that the implementation of pagination.
*/

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.