Ten days to learn php

Source: Internet
Author: User
Tags learn php

Objective: To create a paging display

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

For $ page, if it does not exist, let it = 0. If there is <0, let it also = 0. If it exceeds the total number of pages, let it = the total number of pages.

$ Execc = "select count (*) from tablename ";
$ Resultc = mysql_query ($ execc );
$ Rsc = mysql_fetch_array ($ resultc );
$ Num = $ rsc [0];

In this way, the total number of records can be obtained.
Ceil ($ num/10) If a page contains 10 records, this is the total number of pages.

So you can write it like this.
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 starts from 0, so-1
}

In this way, $ exec can write $ exec = "select * from tablename limit". ($ page * 10). ", 10 ";
// One page is 10 records

What we need to do at last is several 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. You can think about how to optimize it? Today, let's talk about some important issues 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.