In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about how to quickly master the paging of PHP articles. The article is rich in content and analyzes and narrates for you from a professional point of view. I hope you can get something after reading this article.
PHP language brings us the realization of many functions, and it has a wide range of applications. Today, we will explain in detail the relevant implementation methods of paging articles on PHP. I hope more of you can help.
1. Preface
For veterans of web programming, writing this kind of code is as natural as breathing, but for beginners, they are often confused about this problem, so I wrote this article to explain this problem in detail, and strive to make friends who finish reading this article understand the principle and implementation of paging display. This article is suitable for beginners, and all sample code is written in php.
2. The paging principle of PHP articles
The so-called paging display is to artificially divide the result set in the database into segments to display, here you need two initial parameters:
How many records per page ($PageSize)?
What is the current page ($CurrentPageID)?
Now just give me another result set and I can display a specific result.
As for other parameters, such as the previous page ($PreviousPageID), the next page ($NextPageID), the total number of pages ($numPages), etc., can be obtained from the previous few things.
Take the mysql database as an example. If you want to intercept a section of content from the 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 the rules in it.
The first 10 records: select * from table limit 0pm 10
Records 11 to 20: select * from table limit 10
Records 21 to 30: select * from table limit 20jin10
……
This set of sql statements is actually a sql statement that fetches each page of data in the table when $PageSize=10. We can sum up a template like this:
Select * from table limit ($CurrentPageID-1) * $PageSize, $PageSize
Compare this template with the corresponding value with the above set of sql statements to see if that's the case. Once you've solved the most important question of how to get the data, all that's left is to pass the parameters, construct the appropriate sql statement, and then use php to get the data from the database and display it. I will explain it with specific code below.
3. Simple code for paging PHP articles
Please read the following code carefully, debug and run it once, modify it once, and add your own features, such as search, and so on.
< ?php // 建立数据库连接 $link = mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect: " . mysql_error()); // 获取当前页数 if( isset($_GET['page']) ){ $page = intval( $_GET['page'] ); } else{ $page = 1; } // 每页数量 $PageSize = 10; // 获取总数据量 $sql = "select count(*) as amount from table"; $result = mysql_query($sql); $row = mysql_fetch_row($result); $amount = $row['amount']; // 记算总共有多少页 if( $amount ){ if( $amount < $page_size ){ $page_count = 1; } //如果总数据量小于$PageSize,那么只有一页 if( $amount % $page_size ){ //取总数据量除以每页数的余数 $page_count = (int)($amount / $page_size) + 1; //如果有余数,则页数等于总数据量除以每页数的结 果取整再加一 }else{ $page_count = $amount / $page_size; //如果没有余数,则页数等于总数据量除以每页数的结果 } } else{ $page_count = 0; } // 翻页链接 $page_string = ''; if( $page == 1 ){ $page_string .= '***页|上一页|'; } else{ $page_string .= '***页 |上一页|'; } if( ($page == $page_count) || ($page_count == 0) ){ $page_string .= '下一页|尾页'; } else{ $page_string .= ' 下一页|尾页'; } // 获取数据,以二维数组格式返回结果 if( $amount ){ $sql = "select * from table order by id desc limit ". ($page-1)*$page_size .", $page_size"; $result = mysql_query($sql); while ( $row = mysql_fetch_row($result) ){ $rowset[] = $row; } }else{ $rowset = array(); } // 没有包含显示结果的代码,那不在讨论范围, 只要用foreach就可以很简单的用得到的二维数组来显示结果 ?>The above is the editor for you to share how to quickly master PHP article paging, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.