簡體   English   中英

在php中更改分頁的目標頁面

[英]Changing the target page of pagination in php

我在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> 任何的意見都將會有幫助。

您應該更改目標頁$targetpage = "demodesk.php"; 到新頁面$targetpage = "yournewpage.php#idOfDiv"; 然后,在另一頁上,您應該給div一個ID,並將上面URL中的idOfDiv替換為div的ID


但是,由於您在代碼中進一步使用$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>

你可以優化你的代碼,但是這東西給你做...查看更多有關錨標記在這里

首先,放置默認$ page值,然后再將其設置為$ _GET [page]值。

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

之后,驗證$ page-應該為:大於或等於1,小於或等於$ maxpage。

之后,將它們全部變成一個函數:

function pagination($target) {
}

並像這樣傳遞參數

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

那應該做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM