Quick mastery of PHP article pagination tips _php tutorial

Source: Internet
Author: User

1. Preface

For the veteran of Web programming, writing this code is just as natural as breathing, but for beginners, often do not have a clue to this problem, so specially written this article on this issue to explain in detail, and strive to let the friends after reading this post after reading for the page display of the principle and implementation of the method have some understanding. This article is for beginners to read, and all sample code is written using PHP.

2, PHP article paging principle

The so-called paging display, that is, the result set in the database is artificially divided into a section of the display, here requires two initial parameters:

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

Now just give me a result set and I can show a specific result.
For other parameters, such as the previous page ($PreviousPageID), the next page ($NextPageID), the total number of pages ($numPages), and so on, can be based on the front of these things.
For example, in MySQL database, if you want to intercept a piece of content from within a table, the SQL statement can be used: SELECT * FROM table limit offset, rows. Take a look at the following set of SQL statements and try to find out the rate of compliance.

Top 10 Records: SELECT * FROM table limit 0,10
11th to 20th record: SELECT * FROM table limit 10,10
21st to 30th Record: SELECT * FROM table limit 20,10
......

This set of SQL statements is actually a SQL statement that takes every page of data in the table when $pagesize=10, and we can summarize a template like this:

SELECT * FROM table limit ($CurrentPageID-1) * $PageSize, $PageSize

Take this template and replace the corresponding value with the set of SQL statements above to see if that's the case. Take care of the most important question of how to get the data, all that is left is to pass the parameters, construct the appropriate SQL statement, and then use PHP to retrieve the data from the database and display it. I'll use the specific code to illustrate the following.

3, PHP article paging simple code

Please read the following code in detail, run your own debugging once, it is best to modify it once, plus your own features, such as search and so on.

 
 
  1. < ? PHP
  2. Establishing a database connection
  3. $ Link = mysql_connect ("localhost",
    "Mysql_user", "Mysql_password")
  4. Or Die ("Could Not connect:". Mysql_error ());
  5. Get the current number of pages
  6. if (Isset ($_get[' page ')) {
  7. $ page = intval ($_get[' page ');
  8. }
  9. else{
  10. $ page = 1 ;
  11. }
  12. Quantity per page
  13. $ PageSize = Ten ;
  14. Get 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 how many pages there are in total
  20. if ($amount) {
  21. if ($amount < $page _size) {$page_count = 1 ; }
  22. If the total data volume is less than $pagesize, then there is only one page
  23. if ($amount% $page _size) {
  24. The total amount of data divided by the remainder of each page
  25. $ Page_count = (int) ($amount/$page _size) + 1;
  26. If there is an excess, the number of pages equals the total amount of data divided by each page
    Take the whole fruit and add one more
  27. }else{
  28. $ Page_count = $amount/$page _size;
  29. If there is no remainder, the number of pages equals the total data amount divided by the result of each page
  30. }
  31. }
  32. else{
  33. $ Page_count = 0 ;
  34. }
  35. Page link
  36. $ page_string = '' ;
  37. if ($page = = 1) {
  38. $page _string . = ' first page | previous page | ' ;
  39. }
  40. else{
  41. $page _string . = ' first page
    | . ($page-1). ' > Previous Page a>| ';
  42. }
  43. if (($page = = $page _count) | | ($ Page_count = = 0)) {
  44. $page _string . = ' next page | last ' ;
  45. }
  46. else{
  47. $page _string . = '.( $page + 1). ' >
    Next Page a>| < a href=? page = '. $page _count. ' > last page a>';
  48. }
  49. Get data to 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. There is no code to display the results, that is not the scope of the discussion,
    As long as you use foreach, you can simply use the resulting two-dimensional array to display the results
  60. ?>

The above is the PHP article paging related implementation methods.


http://www.bkjia.com/PHPjc/446034.html www.bkjia.com true http://www.bkjia.com/PHPjc/446034.html techarticle 1, preface for the veteran of Web programming, writing this code is just as natural as breathing, but for beginners, often the problem is not a clue, so specially written ...

  • 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.