php 限制分页页码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8361808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Limit pagination page number
提问by Irene Ling
$objConnect = mysql_connect("localhost","root","") or die(mysql_error());
$objDB = mysql_select_db("Test");
$strSQL = "SELECT * FROM UserAddedRecord WHERE (Name LIKE '%".$getname."%' and State LIKE '%".$getstate."%' and Cell LIKE '%".$getcell."%' and Custgroup LIKE '%".$getgroup."%') AND user_id=$id";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);
$Per_Page = 5;
if (!isset($_GET['Page'])) {
$Page = 1;
} else {
$Page = $_GET['Page'];
}
$Prev_Page = $Page - 1;
$Next_Page = $Page + 1;
$Page_Start = (($Per_Page * $Page) - $Per_Page);
if ($Num_Rows <= $Per_Page) {
$Num_Pages = 1;
} elseif (($Num_Rows % $Per_Page) == 0) {
$Num_Pages = ($Num_Rows / $Per_Page) ;
} else {
$Num_Pages = ($Num_Rows / $Per_Page) + 1;
$Num_Pages = (int) $Num_Pages;
}
$strSQL .=" order by addedrec_ID DESC LIMIT $Page_Start , $Per_Page";
$objQuery = mysql_query($strSQL) or trigger_error(mysql_error());;
if ($Prev_Page) {
echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'><< Back</a> ";
}
for ($i=1; $i <= $Num_Pages; $i++) {
if ($i != $Page) {
echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$i&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'>$i</a> ";
} else {
echo "<b> $i </b>";
}
}
if ($Page!=$Num_Pages) {
echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'>Next>></a> ";
}
mysql_close($objConnect);
This is the code I use to create pagination page for data search.But I just noticed that I have too many records then it will have too many page numbers.I'm trying to limit the page number shown,and look for many examples ..it have many ways to do it but I still don't have idea how to limit it for my method...
这是我用来为数据搜索创建分页页面的代码。但我只是注意到我有太多的记录,然后它会有太多的页码。我试图限制显示的页码,并寻找很多例子。 .它有很多方法可以做到,但我仍然不知道如何限制我的方法......
回答by Sean H Jenkins
Ok if you mean show something like
好的,如果你的意思是显示类似的东西
Prev 1 2 3 4 5 6 .. 40 41 Next
Prev 1 2 .. 6 7 8 9 10 .. 40 41 Next
First thing we need to is create a function that can process what we need to output the pagination. Heres a function I use and it works well.
我们需要做的第一件事是创建一个函数,该函数可以处理输出分页所需的内容。这是我使用的一个函数,它运行良好。
function get_paging_info($tot_rows,$pp,$curr_page)
{
$pages = ceil($tot_rows / $pp); // calc pages
$data = array(); // start out array
$data['si'] = ($curr_page * $pp) - $pp; // what row to start at
$data['pages'] = $pages; // add the pages
$data['curr_page'] = $curr_page; // Whats the current page
return $data; //return the paging data
}
Now this function is pretty solid and works very well for me.
现在这个功能非常可靠,对我来说效果很好。
So you pass this function
所以你通过这个函数
- $tot_rows = counted rows for query
- $pp = items per page
- $curr_page = the current page number
- $tot_rows = 查询的计数行
- $pp = 每页项目
- $curr_page = 当前页码
Ok, now that you have the data you need, you'll need to display it.
好的,现在您有了所需的数据,您需要显示它。
Heres what I use and please read it before you think, 'ah, it's too long'. It's actually very simple.
这是我使用的内容,请先阅读,然后再想“啊,太长了”。其实很简单。
Heres a snapshot of what it will return
这是它将返回的内容的快照
<!-- Create the query -->
<?php $count = mysql_fetch_assoc( mysql_query ( "SELECT COUNT( rows ) as count FROM table" ) ) ;
<?php $count = $count[0]['count']; ?>
<!-- Call our function from above -->
<?php $paging_info = get_paging_info($count,5,34); ?>
<p>
<!-- If the current page is more than 1, show the First and Previous links -->
<?php if($paging_info['curr_page'] > 1) : ?>
<a href='' title='Page 1'>First</a>
<a href='' title='Page <?php echo ($paging_info['curr_page'] - 1); ?>'>Prev</a>
<?php endif; ?>
<?php
//setup starting point
//$max is equal to number of links shown
$max = 7;
if($paging_info['curr_page'] < $max)
$sp = 1;
elseif($paging_info['curr_page'] >= ($paging_info['pages'] - floor($max / 2)) )
$sp = $paging_info['pages'] - $max + 1;
elseif($paging_info['curr_page'] >= $max)
$sp = $paging_info['curr_page'] - floor($max/2);
?>
<!-- If the current page >= $max then show link to 1st page -->
<?php if($paging_info['curr_page'] >= $max) : ?>
<a href='' title='Page 1'>1</a>
..
<?php endif; ?>
<!-- Loop though max number of pages shown and show links either side equal to $max / 2 -->
<?php for($i = $sp; $i <= ($sp + $max -1);$i++) : ?>
<?php
if($i > $paging_info['pages'])
continue;
?>
<?php if($paging_info['curr_page'] == $i) : ?>
<span class='bold'><?php echo $i; ?></span>
<?php else : ?>
<a href='' title='Page <?php echo $i; ?>'><?php echo $i; ?></a>
<?php endif; ?>
<?php endfor; ?>
<!-- If the current page is less than say the last page minus $max pages divided by 2-->
<?php if($paging_info['curr_page'] < ($paging_info['pages'] - floor($max / 2))) : ?>
..
<a href='' title='Page <?php echo $paging_info['pages']; ?>'><?php echo $paging_info['pages']; ?></a>
<?php endif; ?>
<!-- Show last two pages if we're not near them -->
<?php if($paging_info['curr_page'] < $paging_info['pages']) : ?>
<a href='<?php echo str_replace('/page'.$paging_info['curr_page'], '', $paging_info['curr_url']) . '/page'.($paging_info['curr_page'] + 1); ?>' title='Page <?php echo ($paging_info['curr_page'] + 1); ?>'>Next</a>
<a href='<?php echo str_replace('/page'.$paging_info['curr_page'], '', $paging_info['curr_url']) . '/page'.$paging_info['pages']; ?>' title='Page <?php echo $paging_info['pages']; ?>'>Last</a>
<?php endif; ?>
</p>
回答by Irene Ling
$count_pages = ceil($total / $items_per_page);
$count_pages = ceil($total / $items_per_page);
回答by Ivan Laharnar mink.si
make it simple, here is code:
简单点,这里是代码:
<?php
//put this code on the begining of the page
$page=$_GET['page']; //Getting page number from url | example: www.mydomain.com/index.php?page=3
if(!is_numeric($page)) $page=1; //If there is no page number specified we set number 1
$queryCount = "SELECT * FROM table "; //select from db
$query = $DB->query( $queryCount);
$num = mysql_num_rows ($query); //count num rows
$per_page=5; //default 5 results per page
$start=$per_page*($page-1); //start for select on next page (page2, 3, 4,5)
$end=min($num,$page*$per_page); //end
//here is select for your results. Be careful for LIMIT in the select!
$query = "SELECT * FROM page LIMIT $start, $per_page ";
//page bottom, where you want to put your numbers
$pages=ceil($num/$per_page);
for($s=1; $s<=$pages; $s++)
{
if($s==$page)
$numPage .= "[$s] ";
else
$numPage .= "<a href='index.php?page=$s'>$s</a> ";
}
echo $numPage;
?>
?>
I didn't test it, but I think it must work :-)
我没有测试它,但我认为它必须工作:-)
Cind regards, Ivan
问候,伊万
回答by Cyclonecode
You will need to use a LIMIT
clause in your sql statement. For example:
您将需要LIMIT
在 sql 语句中使用子句。例如:
SELECT <column>
FROM <table>
LIMIT 0, 5
Reference: SELECT Syntax
参考:SELECT 语法
回答by Yes Barry
I solved a very similar issue for someone else with a very similar script here: How to limit pages shown in pagination script
我在这里用非常相似的脚本为其他人解决了一个非常相似的问题:如何限制分页脚本中显示的页面
Also, I noticed a few other anomalies with your script (besides the unreadable formatting that I fixed). You should change all occurrences of $_SERVER[SCRIPT_NAME]
to $_SERVER['SCRIPT_NAME']
. In your script, for instance:
此外,我注意到您的脚本还有其他一些异常(除了我修复的不可读的格式)。您应该将所有出现的更改$_SERVER[SCRIPT_NAME]
为$_SERVER['SCRIPT_NAME']
。例如,在您的脚本中:
echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=...";
Would become:
会成为:
echo " <a href ='{$_SERVER['SCRIPT_NAME']}?Page=...";
回答by NgoKien
my code always show at least 9 pages in case of total pages>9 whatever you are on page 1,2,3
我的代码总是显示至少 9 页,以防总页数> 9,无论您在第 1、2、3 页上是什么
<ul class="pagination pagination-sm" style="align:center">
<li><a href="./?page=1"><</span></a></li>
<?php
if ($current_page <4){
if ($total_pages<9)
{
for($i = 1; $i <= $total_pages; $i++)
{
if ($i == $current_page)
{
echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n";
} else {
echo ' <li><a href="./?page='.$i.'">'; echo $i.'</a></li>'."\n";
}
}
}
else
{
for($i = 1; $i <= 9; $i++)
{
if ($i == $current_page)
{
echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n";
} else {
echo ' <li><a href="./?trang='.$i.'">'; echo $i.'</a></li>'."\n";
}
}
}
}
elseif ($current_page >$total_pages-4)
{
if ($total_pages<9)
{
for($i = 1; $i <= $total_pages; $i++){
if ($i == $current_page){
echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n";
} else {
echo ' <li><a href="./?page='.$i.'">'; echo $i.'</a></li>'."\n";
}
}
}else{
for($i = $total_pages-8; $i <= $total_pages; $i++){
if ($i == $current_page){
echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n";
} else {
echo ' <li><a href="./?page='.$i.'">'; echo $i.'</a></li>'."\n";
}
}
}
}else{
for($i = max(1, $current_page - 4); $i <= min($current_page + 4, $total_pages); $i++){
if ($i == $current_page){
echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n";
} else {
echo ' <li><a href="./?trang='.$i.'">'; echo $i.'</a></li>'."\n";
}
}
}
?>
<li>
<a href="./?trang=<?php echo $total_pages ?>">></a>
</li>
</ul>
回答by Andris
Purpose of this code is only to show definite (certain) number of pages in pagination
此代码的目的只是为了在分页中显示确定的(某些)页数
Get current page number from url and define $curr_pg_numb
从 url 获取当前页码并定义 $curr_pg_numb
Get number of total pages from mysql and define $total_pages
从mysql获取总页数并定义 $total_pages
And here is code for pagination
这是分页代码
$counter=0;
$init_num_to_left = 3;//initial number of pages to show to left from current page
$init_num_to_right = 4;
if( $curr_pg_numb - $init_num_to_left < 1 ){
$init_num_to_right = $init_num_to_right + ( 1 - ($curr_pg_numb - $init_num_to_left) );
$init_num_to_left = $curr_pg_numb - 1;
}
else if( $curr_pg_numb + $init_num_to_right > $total_pages ){
$init_num_to_left = $init_num_to_left + ( ($curr_pg_numb + $init_num_to_right) - $total_pages );
$init_num_to_right = ($curr_pg_numb + $init_num_to_right) - $total_pages;
}
while($counter <= $total_pages ) {
if ( $counter > 0 ){
if ($curr_pg_numb==$counter){
echo '<strong> '. ( htmlspecialchars($curr_pg_numb , ENT_QUOTES, "UTF-8") + 0 ). '</strong> ';
}
else {
if ( $counter >= ($curr_pg_numb - $init_num_to_left ) and $counter <= ($curr_pg_numb + $init_num_to_right ) ) {
echo '<a href="?cpn='. htmlspecialchars(($counter), ENT_QUOTES, "UTF-8"). '" style="margin:5px;">'. htmlspecialchars(($counter), ENT_QUOTES, "UTF-8"). '</a>';
}
}//else {
}//if ( $counter > 0 ){
$counter++;
}//while($counter <= $total_pages ) {