Quickly master PHP article pagination skills_PHP tutorial

WBOY
Release: 2016-07-15 13:33:45
Original
1202 people have browsed it

1. Preface

For web programming veterans, writing this kind of code is really like breathing Naturally, but for beginners, they are often confused about this issue, so I specially wrote this article to explain this issue in detail, and strive to make friends who read this article understand the principle and implementation of paging display after reading it. Understand the method. This article is suitable for beginners to read, and all sample codes are written in PHP.

2. PHP article paging principle

The so-called paging display means that the result set in the database is artificially divided into segments for display. Two initializations are required here. Parameters:

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

Now as long as you give me another result set, I can display a specific result.
As for other parameters, such as: previous page ($PreviousPageID), next page ($NextPageID), total number of pages ($numPages), etc., they can all be obtained based on the previous things.
Take the mysql database as an example. If you want to intercept a certain piece 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.

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

This set of sql statements is actually the sql statement that fetches data from each page of the table when $PageSize=10. We can summarize such a template:

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

Take this template and substitute the corresponding value with the above set of sql statements to see if it is the same. thing. After solving the most important problem of how to obtain the data, all that is left is to pass the parameters, construct the appropriate SQL statement and then use PHP to obtain the data from the database and display it. Below I will explain it with specific code.

3. Simple PHP article paging code

Please read the following code in detail, debug and run it yourself, it is best to modify it once and add your own functions, such as Search and more.

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?php  </span></span></li><li><span>// 建立数据库连接  </span></li><li class="alt"><span>$</span><span class="attribute">link</span><span> = </span><span class="attribute-value">mysql_connect</span><span>("localhost", <br />"mysql_user", "mysql_password")   </span></li><li><span>or die("Could not connect: " . mysql_error());   </span></li><li class="alt"><span>// 获取当前页数  </span></li><li><span>if( isset($_GET['page']) ){  </span></li><li class="alt"><span>$</span><span class="attribute">page</span><span> = </span><span class="attribute-value">intval</span><span>( $_GET['page'] );  </span></li><li><span>}  </span></li><li class="alt"><span>else{  </span></li><li><span>$</span><span class="attribute">page</span><span> = </span><span class="attribute-value">1</span><span>;  </span></li><li class="alt"><span>}   </span></li><li><span>// 每页数量  </span></li><li class="alt"><span>$</span><span class="attribute">PageSize</span><span> = </span><span class="attribute-value">10</span><span>;   </span></li><li><span>// 获取总数据量  </span></li><li class="alt"><span>$</span><span class="attribute">sql</span><span> = </span><span class="attribute-value">"select count(*) as amount from table"</span><span>;  </span></li><li><span>$</span><span class="attribute">result</span><span> = </span><span class="attribute-value">mysql_query</span><span>($sql);  </span></li><li class="alt"><span>$</span><span class="attribute">row</span><span> = </span><span class="attribute-value">mysql_fetch_row</span><span>($result);  </span></li><li><span>$</span><span class="attribute">amount</span><span> = $row['amount'];   </span></li><li class="alt"><span>// 记算总共有多少页  </span></li><li><span>if( $amount ){  </span></li><li class="alt"><span>if( $amount </span><span class="tag"><</span><span> $page_size ){ $</span><span class="attribute">page_count</span><span> = </span><span class="attribute-value">1</span><span>; } </span></li><li class="alt"><span>//如果总数据量小于$PageSize,那么只有一页  </span></li><li><span>if( $amount % $page_size ){ </span></li><li><span>//取总数据量除以每页数的余数  </span></li><li class="alt"><span>$</span><span class="attribute">page_count</span><span> = (int)($amount / $page_size) + 1; </span></li><li class="alt"><span>//如果有余数,则页数等于总数据量除以每页数的结<br />果取整再加一  </span></li><li><span>}else{  </span></li><li class="alt"><span>$</span><span class="attribute">page_count</span><span> = $amount / $page_size; </span></li><li class="alt"><span>//如果没有余数,则页数等于总数据量除以每页数的结果  </span></li><li><span>}  </span></li><li class="alt"><span>}  </span></li><li><span>else{  </span></li><li class="alt"><span>$</span><span class="attribute">page_count</span><span> = </span><span class="attribute-value">0</span><span>;  </span></li><li><span>}  </span></li><li class="alt"><span> </span></li><li><span>// 翻页链接  </span></li><li class="alt"><span>$</span><span class="attribute">page_string</span><span> = </span><span class="attribute-value">''</span><span>;  </span></li><li><span>if( $</span><span class="attribute">page</span><span> == 1 ){  </span></li><li class="alt"><span>$page_string </span><span class="attribute">.</span><span>= </span><span class="attribute-value">'第一页|上一页|'</span><span>;  </span></li><li><span>}  </span></li><li class="alt"><span>else{  </span></li><li><span>$page_string </span><span class="attribute">.</span><span>= </span><span class="attribute-value">'<a href=?page=1>第一页</a><br>|<a href=?page='</span><span>.($page-1).'</span><span class="tag">></span><span>上一页</span><span class="tag"></</span><span class="tag-name">a</span><span class="tag">></span><span>|';  </span></span></li>
<li class="alt"><span>}   </span></li>
<li>
<span>if( ($</span><span class="attribute">page</span><span> == $page_count) || ($</span><span class="attribute">page_count</span><span> == 0) ){  </span>
</li>
<li class="alt">
<span>$page_string </span><span class="attribute">.</span><span>= </span><span class="attribute-value">'下一页|尾页'</span><span>;  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>else{  </span></li>
<li>
<span>$page_string </span><span class="attribute">.</span><span>= </span><span class="attribute-value">'<a href=?page='</span><span>.($page+1).'</span><span class="tag">><br></span><span>下一页</span><span class="tag"></</span><span class="tag-name">a</span><span class="tag">></span><span>|</span><span class="tag"><</span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=?</span><span class="attribute">page</span><span>=</span><span class="attribute-value">'.$page_count.'</span><span class="tag">></span><span>尾页</span><span class="tag"></</span><span class="tag-name">a</span><span class="tag">></span><span>';  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>// 获取数据,以二维数组格式返回结果  </span></li>
<li class="alt"><span>if( $amount ){  </span></li>
<li>
<span>$</span><span class="attribute">sql</span><span> = </span><span class="attribute-value">"select * from table order by id desc <br>limit "</span><span>. ($page-1)*$page_size .", $page_size";  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">result</span><span> = </span><span class="attribute-value">mysql_query</span><span>($sql);  </span>
</li>
<li><span> </span></li>
<li class="alt">
<span>while ( $</span><span class="attribute">row</span><span> = </span><span class="attribute-value">mysql_fetch_row</span><span>($result) ){  </span>
</li>
<li><span>$rowset[] = $row;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>}else{  </span></li>
<li class="alt">
<span>$</span><span class="attribute">rowset</span><span> = </span><span class="attribute-value">array</span><span>();  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>// 没有包含显示结果的代码,那不在讨论范围,<br>只要用foreach就可以很简单的用得到的二维数组来显示结果  </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

The above is the relevant implementation method of PHP article paging.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446034.htmlTechArticle1. Foreword For veterans of web programming, writing this kind of code is as natural as breathing, but for web programming veterans Beginners are often confused about this issue, so I specially wrote...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!