Quick PHP article paging tips _ PHP Tutorial

Source: Internet
Author: User
Quickly learn PHP article paging skills. 1. preface for web programming veterans, writing such code is as natural as breathing, but for beginners, they often have no clue about this issue.

1. Preface

For veterans of web programming, writing such code is as natural as breathing, but for beginners, they often have no clue about this issue. Therefore, I wrote this article to explain this issue in detail, we strive to make friends who have read this article understand the principle and implementation methods of paging display. This article is suitable for beginners. all sample code is written in php.

2. paging principles of PHP articles

The so-called paging display means that the result set in the database is manually divided into segments for display. here two initial parameters are required:

How many records per page ($ PageSize )?
What is the current page ($ CurrentPageID )?

Now, you only need to give me another result set, so that I can display a specific result.
Other parameters, such as the previous page ($ PreviousPageID), Next page ($ NextPageID), and total page ($ numPages), can all be obtained based on the front.
Take the mysql database as an example. if you want to extract some content from the table, you can use the SQL statement: select * from table limit offset, rows. Take a look at the following SQL statements and try to find the rule rate.

First 10 records: select * from table limit 0, 10
11th to 20 records: select * from table limit 10, 10
21st to 30 records: select * from table limit 20, 10
......

This set of SQL statements is the SQL statement used to retrieve data of each page in the table when $ PageSize = 10. we can summarize this template:

Select * from table limit ($ CurrentPageID-1) * $ PageSize, $ PageSize

Use this template to compare the corresponding values with the preceding SQL statements to see if this is the case. After solving the most important problem of how to obtain data, the rest is simply passing parameters, constructing appropriate SQL statements, and then using php to obtain data from the database and display it. I will describe the code below.

3. simple code for pagination of PHP articles

Please read the following code in detail and debug and run it once. you 'd better modify it once and add your own functions, such as search.

 
  1. <? Php
  2. // Establish a database connection
  3. $ Link = mysql_connect ("localhost ",
    "Mysql_user", "mysql_password ")
  4. Or die ("cocould not connect:". mysql_error ());
  5. // Obtain the current page number
  6. If (isset ($ _ GET ['Page']) {
  7. $ Page = intval ($ _ GET ['Page']);
  8. }
  9. Else {
  10. $ Page = 1;
  11. }
  12. // Number of pages per page
  13. $ PageSize = 10;
  14. // Obtain the total data volume
  15. $ SQL = "select count (*) as amount from table ";
  16. $ Result = mysql_query ($ SQL );
  17. $ Row = mysql_fetch_row ($ result );
  18. $ Amount = $ row ['amount'];
  19. // Count the total number of pages
  20. If ($ amount ){
  21. If ($ amount <$ page_size) {$ page_count = 1 ;}
  22. // If the total data volume is less than $ PageSize, there is only one page
  23. If ($ amount % $ page_size ){
  24. // Divide the total data volume by the remainder of each page
  25. $ Page_count = (int) ($ amount/$ page_size) + 1;
  26. // If there is a remainder, the number of pages equals the total data volume divided by the end of each page
    If the result is an integer, add one.
  27. } Else {
  28. $ Page_count = $ amount/$ page_size;
  29. // If there is no remainder, the number of pages equals the total data volume divided by the result of each page
  30. }
  31. }
  32. Else {
  33. $ Page_count = 0;
  34. }
  35. // Flip link
  36. $ Page_string = '';
  37. If ($ page = 1 ){
  38. $ Page_string. = 'Page 1 | previous page | ';
  39. }
  40. Else {
  41. $ Page_string. = 'Page 1
    |. ($ Page-1). '> Previous page | ';
  42. }
  43. If ($ page = $ page_count) | ($ page_count = 0 )){
  44. $ Page_string. = 'next page | last page ';
  45. }
  46. Else {
  47. $ Page_string. = '. ($ page + 1).'>
    Next Page | last page ';
  48. }
  49. // Obtain data and return results in two-dimensional array format
  50. If ($ amount ){
  51. $ SQL = "select * from table order by id desc
    Limit ". ($ page-1) * $ page_size.", $ page_size ";
  52. $ Result = mysql_query ($ SQL );
  53. While ($ row = mysql_fetch_row ($ result )){
  54. $ Rowset [] = $ row;
  55. }
  56. } Else {
  57. $ Rowset = array ();
  58. }
  59. // The code that does not contain the display result is not in the scope of the discussion,
    With foreach, you can simply use the two-dimensional array to display the result.
  60. ?>

The above is the implementation of PHP article paging.


NLP for web programming veterans, writing this kind of code is as natural as breathing, but for beginners, it is often hard to find a clue about this problem, so I wrote it specially...

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.