添加& # 39;prev # 39;和& # 39;未来# 39;按钮到PHP分页.然后在中间去掉大量的数字


Adding 'prev' and 'next' buttons to PHP pagination. And taking out loads of numbers in the middle

我正在处理一个网站,从MySQL数据库显示有关假期的信息。我已经成功地以列表格式显示信息,并包含每页大约20个条目的分页,但由于条目非常多,我最终只在底部显示了大量看起来不美观的数字。我想知道是否有人知道如何在PHP的任何一边包括"prev"answers"next"按钮,并从中间取出一些数字,并将其替换为"…,就像这个网站使用的分页格式,如果你看一下问题页面的底部。

下面是我的代码:

<?php
//connect
mysql_connect('localhost', 'xxxx', 'xxxx') or die(mysql_error()); 
mysql_select_db('hdb') or die(mysql_error());
//pagination
$per_page = 20;
$pages_query = mysql_query("SELECT COUNT('id') FROM `holidaytable` WHERE brandname = 'Spain'");
$count = mysql_result($pages_query, 0);
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
echo '<p>There are '.$count.' holidays<hr></p><br /><br />';
//construct query and insert values
$query = mysql_query("SELECT * FROM holidaytable WHERE brandname = 'Spain' LIMIT $start, $per_page") or die(mysql_error());
while($rows = mysql_fetch_assoc($query))
{
   $name = $rows['name'];
   $id2 = $rows['id2'];
   $hotelname = $rows['hotelname'];
   $desc = $rows['desc'];
   $spec = $rows['spec'];
   $resort = $rows['resort'];
   $awtrack = $rows['awtrack'];
   $awthumb = $rows['awthumb'];
   $awimage = $rows['awimage'];
   $mthumb = $rows['mThumb'];
   $mlink = $rows['mlink'];
   $mimage = $rows['mimage'];
   $awcatid = $rows['awcatid'];
   $mcat = $rows['mcat'];
   $brandname = $rows['brandname'];
   echo "$id2 <br>$name <br>$hotelname <br>$desc <br><h2>$resort, $brandname</h2><br><img src='$mthumb'><br><img src='$mimage'> <br> <a href='$awtrack' target='_blank'>link</a>";
   echo "<br /><br /><hr>";
};
//pagination
if ($pages >= 1 && $page <= $pages) {
for ($x=1; $x<=$pages; $x++) {
    echo ($x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a> </strong> ' : '<a href="?page='.$x.'">'.$x.'</a> ';
}
}
mysql_close();
?>

我以前在我的一个项目中使用过这个函数,请随意尝试:我正在使用表格,但是如果你愿意,你可以删除它。

<style type="text/css">
    table,td { font-family:tahoma;}
    a.pn {padding:4px;}
</style>
<?php
 function pager($page,$rowsCount, $rowsPerPage = 10,$pageGroup = 10, $hrefPrefix='?page=')
 {
    $pageCount = ceil($rowsCount/$rowsPerPage);
    $pageIndex = $page > 1 ? $page : 1;        
    $htm = array();
    $htm[] = '<table class="pager" cellspacing="0" cellpadding="0" border="0">';
    $htm[] = '<tr>';
    $htm[] = '<td class="label"><label>Page ' . $pageIndex  . ' of ' . $pageCount . '</label> &nbsp; &nbsp;</td>';
    if($pageCount > 1) {            
        $offset = ($pageIndex % $pageGroup == 0) ? $pageIndex - 1 : $pageIndex;
        $startPage = (floor($offset/$pageGroup) * $pageGroup) + 1;
        $lastPage = ($startPage + $pageGroup) - 1;
        if($lastPage > $pageCount){
            $lastPage = $pageCount;
        }
        if($pageIndex > $pageGroup){
            $htm[] = '<td class="prev"><a href="'. $hrefPrefix. ($startPage-1) . '">&laquo;</a></td>';
        }
        for($i=$startPage;$i<=$lastPage; $i++) {
            if($pageIndex==$i)
                $htm[] = '<td><b>' . $i . '</b></td>';
            else
                $htm[] = '<td><a class="pn" href="' .$hrefPrefix. $i . '">' . $i . '</a></td>';
        }
        if($lastPage < $pageCount) {
            $htm[] = '<td class="next"><a href="' .$hrefPrefix. ($lastPage+1) . '">&raquo;</a></td>';
        }    
    }//pagecount>1 
    $htm[] = '</tr>';
    $htm[] = '</table>';
    echo implode("'n", $htm);
}

//sample usage:
$cp = (isset($_GET['page'])) ? intval($_GET['page']) : 1;
$total_rows = 120;
pager($cp, $total_rows);

您也可以使用Zend_Paginator。它实现了您要求的所有功能,并且您编写的代码要少得多。你可以传递给它一个Zend_Db实例,但如果你不想使用它,你也可以传递给它一个PHP数组,这意味着Zend Paginator是一个真正的组件,你可以独立使用,而不需要任何其他Zend框架组件。典型的设置是这样的:

$paginator = Zend_Paginator::factory($array);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$paginator->setItemCountPerPage(8);
$paginator->setPageRange(3);
$paginator->setView($this->view);

在你看来:

<html>
<body>
<h1>Example</h1>
<?php if (count($this->paginator)): ?>
<ul>
<?php foreach ($this->paginator as $item): ?>
  <li><?php echo $item; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php echo $this->paginationControl($this->paginator,
                                    'Sliding',
                                    'my_pagination_control.phtml'); ?>
</body>
</html>

请参阅Zend Framework手册中的用法章节,以获取分页控件phtml文件的示例。