在php中更改分页的目标页面

时间:2014-07-30 05:04:29

标签: php mysql pagination

我在php中有以下分页代码:

    $targetpage = "demodesk.php";   //your file name  (the name of this file)
$limit = 10;                                //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 * FROM desktop where Place in($commalist) 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\">&lt&lt previous</a>";
    else
        $pagination.= "<span class=\"disabled\">&lt&lt 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 &gt&gt</a>";
    else
        $pagination.= "<span class=\"disabled\">next &gt&gt</span>";
    $pagination.= "</div>\n";       
}
 ?>

此分页代码在与文件名相同的页面上显示结果。我想要一种方法将目标网页更改为另一个页面<div>。任何建议都会有所帮助。

2 个答案:

答案 0 :(得分:0)

您应该将目标网页$targetpage = "demodesk.php";更改为新页面$targetpage = "yournewpage.php#idOfDiv"; 然后在另一页上,您应该为您的div添加ID,并将上面的网址中的idOfDiv替换为您的div


但是因为您在代码中进一步使用$targetpath来放置任何参数,所以您应该做其他事情。

你可以做出变化。例如$idOfDiv = myDiv;并放置在您使用$targetpath #$idOfDiv

的每个链接之后

所以你得到了

<a href=\"$targetpage?page=$prev#$idOfDiv\">&lt&lt previous</a>
<a href=\"$targetpage?page=$counter#$idOfDiv\">$counter</a>
<a href=\"$targetpage?page=$counter#$idOfDiv\">$counter</a>
<a href=\"$targetpage?page=$lpm1#$idOfDiv\">$lpm1</a>
<a href=\"$targetpage?page=$lastpage#$idOfDiv\">$lastpage</a>
<a href=\"$targetpage?page=1#$idOfDiv\">1</a>
<a href=\"$targetpage?page=2#$idOfDiv\">2</a>
<a href=\"$targetpage?page=$counter#$idOfDiv\">$counter</a>
<a href=\"$targetpage?page=$lpm1#$idOfDiv\">$lpm1</a>
<a href=\"$targetpage?page=$lastpage#$idOfDiv\">$lastpage</a>
<a href=\"$targetpage?page=1#$idOfDiv\">1</a>
<a href=\"$targetpage?page=2#$idOfDiv\">2</a>
<a href=\"$targetpage?page=$counter#$idOfDiv\">$counter</a>
<a href=\"$targetpage?page=$next#$idOfDiv\">next &gt&gt</a>

您可以优化代码,但这是您需要做的事情。 详细了解锚标记here

答案 1 :(得分:0)

首先,在使用$ _GET [page]值设置之前放置默认的$ page值。

$page = 1;
if(isset($_GET['page'])) $page = intval($_GET['page']);

之后验证$ page - 应该是:大于或等于1,小于或等于$ maxpage。

之后将其全部转化为函数:

function pagination($target) {
}

并传递参数

$str = pagination('demodesk.php');

应该这样做。