分页php页面

时间:2012-09-03 13:20:04

标签: php pagination

我需要帮助让这个分页脚本变得更复杂一些。 我需要将区域从第61行更改为80,以允许这些相同行之间的注释中描述的复杂性。

请看看你是否能想出一个简单的解决方案并输出到$ pages_content ......

我提供了2个贴纸......

的index.php: http://codepad.org/tWNvATES

数据库(MYSQL):http://codepad.org/cP8hGzWN

!!! - 请,我只需要基本上替换第61行和第80行之间的输出来输出类似

的内容

“...... 4 5 [6] 7 8 ......”

as $ pages_content ...

[编辑]

以下是代码段:

$pages_content = '';
for($i = 1;$i <= $pages;$i++) {
    if($pages >= 1) {
        if($i == $page) {
            $pages_content .= "<b>{$i}</b>";
        }
        else {
            $pages_content .= "<a href='?page={$i}'>{$i}</a>";
        }
    }
}

5 个答案:

答案 0 :(得分:2)

在这里为你提供可用于分页的分页的最佳代码,请参阅

<?php
/*
    Place code to connect to your DB here.
*/
include('config.php');  // include your code to connect to DB.

$tbl_name="";       //your table name
// How many adjacent pages should be shown on each side?
$adjacents = 3;

/* 
   First get total number of rows in data table. 
   If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = "SELECT COUNT(*) as num FROM $tbl_name";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

/* Setup vars for query. */
$targetpage = "filename.php";   //your file name  (the name of this file)
$limit = 2;                                 //how many items to show per page
$page = $_GET['page'];
if($page) 
    $start = ($page - 1) * $limit;          //first item to display on this page
else
    $start = 0;                             //if no page var is given, set start to 0

/* Get data. */
$sql = "SELECT column_name FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);

/* Setup page vars for display. */
if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
$prev = $page - 1;                          //previous page is page - 1
$next = $page + 1;                          //next page is page + 1
$lastpage = ceil($total_pages/$limit);      //lastpage is = total pages / 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\">";
    //previous button
    if ($page > 1) 
        $pagination.= "<a href=\"$targetpage?page=$prev\">� previous</a>";
    else
        $pagination.= "<span class=\"disabled\">� previous</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?page=$counter\">$counter</a>";                 
        }
    }
    elseif($lastpage > 5 + ($adjacents * 2))    //enough pages to hide some
    {
        //close to beginning; only hide later pages
        if($page < 1 + ($adjacents * 2))        
        {
            for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
            $pagination.= "...";
            $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
            $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
        }
        //in middle; hide some front and some back
        elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
        {
            $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
            $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
            $pagination.= "...";
            for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
            $pagination.= "...";
            $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
            $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
        }
        //close to end; only hide early pages
        else
        {
            $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
            $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
            $pagination.= "...";
            for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
        }
    }

    //next button
    if ($page < $counter - 1) 
        $pagination.= "<a href=\"$targetpage?page=$next\">next �</a>";
    else
        $pagination.= "<span class=\"disabled\">next �</span>";
    $pagination.= "</div>\n";       
}
?>

<?php
    while($row = mysql_fetch_array($result))
    {

    // Your while loop here

    }
?>

<?=$pagination?>

用于造型的css在这里:

div.pagination {
padding: 3px;
margin: 3px;
}

div.pagination a {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #AAAADD;

text-decoration: none; /* no underline */
color: #000099;
}
div.pagination a:hover, div.pagination a:active {
border: 1px solid #000099;

color: #000;
}
div.pagination span.current {
padding: 2px 5px 2px 5px;
margin: 2px;
    border: 1px solid #000099;

    font-weight: bold;
    background-color: #000099;
    color: #FFF;
}
div.pagination span.disabled {
    padding: 2px 5px 2px 5px;
    margin: 2px;
    border: 1px solid #EEE;

    color: #DDD;
}

希望它有所帮助,快乐编码!

答案 1 :(得分:0)

这样的事情应该有效

$pages_content = '';

$minPage = max(1, $page - 2);
$maxPage = min($pages, $page + 2);
for($i = $minPage; $i <= $maxPage;$i++) {
    if($pages >= 1) {
        if($i == $page) {
            $pages_content .= "<b>{$i}</b>";
        }
        else {
            $pages_content .= "<a href='?page={$i}'>{$i}</a>";
        }
    }
}

答案 2 :(得分:0)

如果页数可能很大,请考虑使用“对数”分页:

How to do page navigation for many, many pages? Logarithmic page navigation

(我的答案中包含的PHP代码示例)。

答案 3 :(得分:0)

PHP中的基本分页作者:Amol Gholap

public function getPagination($pid,$totalCount=500,$countPerPage=50,$pageNumberingStart=1,$url=BASE_PATH,$maxDisplayPageNumbers=10){
        $response= "<a href='".($pid > 1 ? "$url?pid=".($pid-1) : "" )."' style='text-decoration:none'><-</a>";
        $maxNum = $pid+($maxDisplayPageNumbers/2);
        $minNum = $pid-($maxDisplayPageNumbers/2)+1;//-$pid);
        $response.= $minNum > 1 ? "<a style='text-decoration:none'>---</a>":"";
        for($t=0;$t<$totalCount;$t+=$countPerPage){
            if(($maxNum >= $pageNumberingStart && $minNum <= $pageNumberingStart)
                ){
                if($pid == $pageNumberingStart)
                    $response.= " <a href='$url?pid=$pageNumberingStart'><strong>".$pageNumberingStart."</strong></a> ";
                else
                    $response.= " <a href='$url?pid=$pageNumberingStart' style='text-decoration:none'>".$pageNumberingStart."</a> ";
            }
            $pageNumberingStart++;
        }
        $response.= $maxNum < $pageNumberingStart-1 ? "<a style='text-decoration:none'>---</a>":"";
        $response.= "<a href='".($pid < $pageNumberingStart ? "" : "$url?pid=".($pid+1) )."' style='text-decoration:none'>-></a>";
        return $response;
    }

echo getPagination($pageNumber=5,$totalCount=500,$countPerPage=50,$pageNumberingStart=1,$url=BASE_PATH,$maxDisplayPageNumbers=10);

答案 4 :(得分:0)

获取当前页码

if (isset($_GET['pageno'])) {
    $pageno = $_GET['pageno'];
} else {
    $pageno = 1;
}

分页公式

$no_of_records_per_page = 10;
$offset = ($pageno-1) * $no_of_records_per_page;

获取总页数

$total_pages_sql = "SELECT COUNT(*) FROM table";
$result = mysqli_query($conn,$total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);

构建分页的SQL查询

$sql = "SELECT * FROM table LIMIT $offset, $no_of_records_per_page"; 

参考create Pagination with PHP and MySql