PHP Paging function

Source: Internet
Author: User
Tags php code
Page Pagination | functions

?

Paging function

Function Genpage (& $sql, $page _size=20)

{

Global $prepage, $nextpage, $pages, $sums; Out param

$page = $_get["page"]>=1?$_get["page"]:1;

$eachpage = $page _size;

$pagesql = Strstr ($sql, "from");

$pagesql = "SELECT count (*) as IDs". $pagesql;

$result = mysql_query ($pagesql);

if ($rs = mysql_fetch_array ($result)) $sums = $rs [0];

$pages = Ceil (($sums -0.5)/$eachpage)-1;

$pages = $pages >=0 $pages: 0;

$prepage = ($page >0) $page -1:0;

$nextpage = ($page < $pages) $page + 1: $pages;

$startpos = $page * $eachpage;

$sql. = "Limit $startpos, $eachpage";

}

Show page pagination

function ShowPage ()

{

Global $page, $pages, $prepage, $nextpage, $queryString; Param from Genpage function
$shownum =10/2;

$startpage = ($page >= $shownum)? $page-$shownum: 0;

$endpage = ($page + $shownum <= $pages)? $page + $shownum: $pages;

echo "altogether". ($pages + 1). " Page: ";

if ($page >0) echo "<a href= $PHP _self?page=0& $queryString > First page </a>";

if ($startpage >0)

echo "... <b><a href= $PHP _self?page=". ($page-$shownum *2). " & $queryString >«</a></b> ";

for ($i = $startpage; $i <= $endpage; $i + +)

{

if ($i = = $page) echo "<b>[". ( $i + 1). "] </b> ";

else echo "<a href= $PHP _self?page= $i & $queryString >". ($i + 1). " </a> ";

}

if ($endpage < $pages)

echo "<b><a href= $PHP _self?page=". ($page + $shownum *2). " & $queryString >»</a></b> ... ";

if ($page < $pages)

echo "<a href= $PHP _self?page= $pages & $queryString > Last page </a>";

}

?>


--------------------------------------------------------------------------------

Call:

PHP Code:-------------------------------------------------------------------------------- $sql = "SELECT * FROM table1 where 1 order by id DESC";  Genpage ($sql); Just the normal code plus this line will be OK. $result =mysql_query ($sql); while ($rs = Mysql_fetch_array ($result)) {//display}showpage (); Show Page?>



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.