Paging program in php Based on Message Board

Source: Internet
Author: User
Tags echo date

The article focuses on the paging principle in php. At the same time, we use an implementation to explain how to implement paging in php. This is a left-side program based on our own writing, if you need it, you can refer to it. It is suitable for beginners.

The key issue of paging lies in the two numbers following the Mysql keyword limite. The first is the number of start lines to be recorded, the second number is the number of records obtained after the number of rows starting from this number-do not mistakenly assume that the number of rows is from the beginning to the end of the number of rows, the difference is very long.

Let's look at row 19th of index. php:

The Code is as follows: Copy code

$ RecordSql = $ SQL. "LIMIT". $ page * $ pagesize. ",". $ pagesize;

Where, the $ page variable is obtained from row 8th and row 9th of the program:

The Code is as follows: Copy code

If (isset ($ _ GET ['page']) & $ _ GET ['page']! = ") $ Page = $ _ GET ['page'];
Else $ page = 0;

Of course, you can also see the $ _ GET ['page'] variable, which will be discussed below.

The variable $ pagesize is defined by ourselves in row 7th, that is, the number of records displayed on each page. We define 10 records. When the variable $ page = 0, our statement will eventually be like this:

The Code is as follows: Copy code
$ RecordSql = "SELECT a. *, B. name, B. email, B. qq, c. revert_time, c. revert
FROM post
Left join revert c ON (a. id = c. post_id), guest B
WHERE a. guest_id = B. id
Order by a. id DESC
"LIMIT 0, 10 ";

It is to get 10 results from 0 records -- This includes 0th records;
What happens when $ page = 1? You can imagine -- $ page * $ pagesize = 10 at this time, of course, from ...... If you don't talk about it, then you will be confused.

It is also worth noting that, why do we need to add this limit after getting the total number of rows? It is very simple. If we add this limit when getting the number of results records, the total number of rows will never exceed 10, because the maximum number of records that will be returned after the addition is 10. -- This obviously does not meet the actual situation. So we are executing:

The Code is as follows: Copy code
Mysql_num_rows (mysql_query ($ SQL ));

The limit keyword is added after the total number of records is obtained.

Well, we have obtained the total number of records and set the number of records displayed on each page to 10. Now we only need the total number of pages. I will not talk about this primary school question? Of course, the total number of records divided by the number displayed on each page is the total number of pages-of course, there are also endless periods, such as the total number of records 11, each page shows 10, how many pages do we need? Of course it is two pages. How can we make 11/10 = 2? -- It is obviously wrong, but in real life it is not perfect mathematics. I used the ceil function. I also mentioned it in the previous chapter.

Let's see what we have now:

The Code is as follows: Copy code
Total number of records, $ numRecord = mysql_num_rows (mysql_query ($ SQL ));
Total number of pages, $ totalpage = ceil ($ numRecord/$ pagesize );

If we don't have anything left blank, we can see how to display the next page on the previous page. It's very easy to make the two judgments. Let's look at the 159-163 rows of index. php:

Row 3:

The Code is as follows: Copy code
If ($ page> 0) echo "<a href = index. php? Page = ". ($ page-1)."> Previous page | </a> ";

If the variable $ page> 0 indicates that a previous page exists if the current page number is greater than 0, the "Previous page" link is displayed. The $ page in this link needs to be reduced by one, for example, if the current page is 2, the previous page is of course 1. When we click the previous page, as this link passes page = 1, the get method is used to pass variables through links. This will return to the 8 rows and 9 rows of the Program for processing.

In row 3, we determine that if the current page number is smaller than the total number of records-1-because our page number starts from 0, therefore, the total number of records must be reduced by one to make a judgment. If this judgment is true, there will be a next page, and $ page will certainly be added when the next page is displayed.

Well, the whole page is like this. Just remember how to get the total number of records, add the limit keyword to the SQL statement, and determine how to display the top and bottom pages.

Complete code

The Code is as follows: Copy code

$ Pagesize = 10; // The number of message records displayed on each page
If (isset ($ _ GET ['page']) & $ _ GET ['page']! = '') $ Page = $ _ GET ['page'];
Else $ page = 0;

$ SQL = "SELECT a. *, B. name, B. email, B. qq, c. revert_time, c. revert
FROM post
Left join revert c ON (a. id = c. post_id), guest B
WHERE a. guest_id = B. id
Order by a. id DESC ";
$ NumRecord = mysql_num_rows (mysql_query ($ SQL ));
$ Totalpage = ceil ($ numRecord/$ pagesize );

$ RecordSql = $ SQL. "LIMIT". $ page * $ pagesize. ",". $ pagesize;
$ Result = mysql_query ($ recordSql );

<Table width = "800" border = "0" align = "center" bgcolor = "# fefefe">
<? Php
While ($ rs = mysql_fetch_object ($ result )){
?>
<Tr>
<Td class = "tdhx"> contact: <? Php echo $ rs-> name?> | Email: <? Php echo $ rs-> email?> | QQ: <? Php echo $ rs-> qq?> | Message time: <? Php echo date ("Y-m-d H: I: s", $ rs-> post_time + 8*3600)?> </Td>
</Tr>
<? Php
If (isset ($ _ SESSION ['login']) & $ _ SESSION ['login']) {
?>
<Tr>
<Td class = "tdhx"> <a href = "revert. php? Id = <? Php echo $ rs-> id?> "> Reply </a> | <a href =" delete. php? Id = <? Php echo $ rs-> id?> "> Delete </a> </td>
</Tr>
<? Php
}
?>
<Tr>
<Td> message content: <? Php echo nl2br (htmlspecialchars ($ rs-> post)?> <Br/>
<Font color = "Red">
Reply content: <? Php echo nl2br (htmlspecialchars ($ rs-> revert)?> [<? Php if ($ rs-> revert_time! = "") Echo date ("Y-m-d H: I: s", $ rs-> revert_time + 8*3600)?> ]
</Font>

</Td>
</Tr>
<Tr> <td height = "3px" bgcolor = "# FF6600"> </td> </tr>
<? Php
}
?>
</Table>
<Table width = "800" border = "0" align = "center" bgcolor = "# B1C3D9">
<Tr>
<Td>
<? Php
If ($ page> 0) echo "<a href = 'index. php? Page = ". ($ page-1)." '> Previous page | </a> ";
If ($ page <$ totalpage-1) echo "<a href = 'index. php? Page = ". ($ page + 1)." '> next page </a> ";
?> </Td>
</Tr>
</Table>

Related Article

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.