我目前正在使用下面的php分页脚本。分页显示每页六个结果,带有上一个/下一个链接。我想知道是否有人可能知道我可以添加什么,以便分页还显示前两页和后两页的链接?与此示例页面中的分页一样:http://www.winelog.net/wines/Oregon
$data=file("data.txt");
$pages=0;
foreach($data as $temp){
$x=explode("|",$temp);
if($x[0] > 0){
$pages=$pages+1;
}
}
if($_GET['p']){
$page=$_GET['p'];
}
if($_GET['i']){
$index=$_GET['i'];
}
if($index == "p"){
$page=$page-1;
}
if($index == "n"){
$page=$page+1;
}
if($page < 1){
$page=1;
}
if($page > $pages){
$page=$pages;
}
$line=$data[$page-1];
$fields=explode("|",$line);
显示的导航:
$show=6;
echo "<li><a href='?i=p&p=$page'>« PREV</li></a>";
if($page-($show/2) > 1){
$temp=$page-$show;
echo "<li><a href='?p=$temp'>...</li></a>";
}
if($page-($show/2) >= 1 && $page+($show/2) <= $pages){
$start=$page-($show/2);
$stop=$page+($show/2);
}
if($page-($show/2) < 1){
$start=1;
$stop=$show;
}
if($page+($show/2) > $pages){
$start=$pages-$show;
$stop=$pages;
}
for($i=$start; $i<=$stop; $i++){
if($page==$i){
echo "<li class='active'>$i</li></a>";
}
else{
echo "<li><a href='?p=$i'>$i</li></a>";
}
}
if($page+($show/2) < $pages){
$temp=$page+$show;
echo "<li><a href='?p=$temp'>...</li></a>";
}
echo "<li><a href='?i=n&p=$page'>NEXT »</li></a>";
答案 0 :(得分:1)
这是你需要做的。
首先计算一下“页数”的数量
如果少于10页 - 输出所有页面。
否则:
当前页面的输出 - 5到当前页面+ 5.
在输出之前,放一个'First'按钮 - page = 1
输出后,输入“上一页”按钮 - 页面=总页数。
如果您想要倒数第二个按钮,只需转到页面=总页数 - 1等。
您可能需要查看Zend_Paginator - 您不需要使用整个Zend Framework来使用它的各个部分,它的设计使其可以拆分。
答案 1 :(得分:1)
他们的分页脚本的完整代码免费提供@ Stranger Studios可以下载php
和Perl
脚本。
代码PHP版本:
<?php
//function to return the pagination string
function getPaginationString($page = 1, $totalitems, $limit = 15, $adjacents = 1, $targetpage = "/", $pagestring = "?page=")
{
//defaults
if(!$adjacents) $adjacents = 1;
if(!$limit) $limit = 15;
if(!$page) $page = 1;
if(!$targetpage) $targetpage = "/";
//other vars
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($totalitems / $limit); //lastpage is = total items / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\"";
if($margin || $padding)
{
$pagination .= " style=\"";
if($margin)
$pagination .= "margin: $margin;";
if($padding)
$pagination .= "padding: $padding;";
$pagination .= "\"";
}
$pagination .= ">";
//previous button
if ($page > 1)
$pagination .= "<a href=\"$targetpage$pagestring$prev\">� prev</a>";
else
$pagination .= "<span class=\"disabled\">� prev</span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination .= "<span class=\"current\">$counter</span>";
else
$pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";
}
}
elseif($lastpage >= 7 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 3))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination .= "<span class=\"current\">$counter</span>";
else
$pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";
}
$pagination .= "<span class=\"elipses\">...</span>";
$pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>";
$pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>";
$pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>";
$pagination .= "<span class=\"elipses\">...</span>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination .= "<span class=\"current\">$counter</span>";
else
$pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";
}
$pagination .= "...";
$pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>";
$pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>";
$pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>";
$pagination .= "<span class=\"elipses\">...</span>";
for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination .= "<span class=\"current\">$counter</span>";
else
$pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination .= "<a href=\"" . $targetpage . $pagestring . $next . "\">next �</a>";
else
$pagination .= "<span class=\"disabled\">next �</span>";
$pagination .= "</div>\n";
}
return $pagination;
}
?>
更改代码以满足您的需求,并添加一些CSS
。
关于分页有很多问题,我希望这对需要分页脚本的用户有帮助。
答案 2 :(得分:0)
到winelog网站的链接已断开。因此,我看不到您感兴趣的样本。 我已经用PHP编写了一个分页脚本,该脚本显示了活动页面的两个相邻页面,即
< 1 2 3 4 5 > // 3 being the active page
< 1 2 3 4 > // 2 being the active page
代码如下:
/* set total records */
$total_records = 26; // this is the total count of values in an array or query results from the database
/* set records per page */
$records_per_page = 5;
/* calculate total pages */
$total_pages = ceil($total_records / $records_per_page);
/* get and set value for current page */
if (isset($_GET['page'])) {
$current_page = $_GET['page'];
} else {
$current_page = 1;
}
/* calculate and set previous and next page values */
$previous = $current_page - 1;
$next = $current_page + 1;
/* set start page value */
$start_page = 1;
/* set number of pages to display to the left */
/* maximum value should be 4 */
$pages_to_left = 2;
/* set number of pages to display to the right */
/* maximum value should be 4 */
$pages_to_right = 2;
/* show previous pages to the left */
if ($current_page <= $total_pages && $current_page > $start_page + $pages_to_left) {
$start_page = $current_page - $pages_to_left;
}
/* show next pages to the right */
if ($current_page <= $total_pages && $current_page > $start_page - $pages_to_right) {
$end_page = $current_page + $pages_to_right;
if ($current_page == $total_pages || $current_page + 1 == $total_pages || $current_page + 2 == $total_pages || $current_page + 3 == $total_pages) {
$end_page = $total_pages;
}
} else {
$end_page = $total_pages;
}
/* show previous button */
if ($current_page > 1) {
echo '<a href="?page='.$previous.'">«</a>';
echo " ";
}
/* display pages */
for ($page = $start_page; $page <= $end_page; $page++) {
echo '<a href="?page='.$page.'">'.$page.'</a>';
echo " ";
}
/* show last page button */
if ($end_page + $pages_to_right <= $total_pages || $end_page != $total_pages) {
echo '<a href="?page='.$total_pages.'">…' . $total_pages . '</a>';
}
/* show next button */
if ($current_page < $total_pages) {
echo " ";
echo '<a href="?page='.$next.'">»</a>';
}
以下是该脚本的pastebin链接:https://pastebin.com/espTvimy
请检查一下,让我知道它是否适合您。