10 days to learn the eighth day of PHP

Source: Internet
Author: User

Learning Purpose: Make a paging display

The key is to use the limit in the SQL statement to qualify the displayed records from several to several. We need a variable $page that records the current page and a total number of records $num

For $page if not we let it = 0, if there is <0 let it also = 0, if more than the total number of pages let him = total number of pages.

$EXECC = "SELECT count (*) from TableName";
$RESULTC =mysql_query ($EXECC);
$RSC =mysql_fetch_array ($RESULTC);
$num = $RSC [0];

This will get the total number of records
Ceil ($num/10) If a page 10 records, this is the total number of pages

So you can write that.
if (Empty ($_get[' page '))
{
$page = 0;
}
Else
{
$page =$_get[' page '];
if ($page <0) $page = 0;
if ($page >=ceil ($num/10)) $page =ceil ($num/10) -1;//because page is starting from 0, so to 1
}

So $exec can write $exec = "SELECT * FROM tablename limit". ($page *10). ", 10";
One page is 10 records.

The last thing we need to do is a couple of connections:
<a href= "xxx. Php?page=0 ">FirstPage</a>
<a href= "xxx. Php?page=<?= ($page-1)?> ">PrevPage</a>
<a href= "xxx. php?page=<?= ($page + 1)?> ">NextPage</a>
<a href= "xxx. Php?page=<?=ceil ($num/10) -1?> ">LastPage</a>

This is a general idea, we can think how to optimize? Today, let's talk about some of the issues of attention tomorrow.

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.