【问题标题】:Pagination not working for second page分页不适用于第二页
【发布时间】:2014-04-25 17:51:10
【问题描述】:

此页面与表格中的查询结果完美加载。分页已就位,并且已按记录数正确划分页面。页面上还有其他与表格无关的项目。当我尝试转到第二页时,我只得到没有其他记录的标题行,其他信息在页面上显示为未定义的索引,几乎就像一开始没有查询一样。我已经在网上搜索了答案,并且我已经用尽了解决这个问题的有限知识。我想做的就是对来自 mysql 数据库的查询结果进行分页,并仅在每次用户更改页面时刷新表。我已经查看了 ajax 和 jquery 来做到这一点,但无法掌握在我的代码中实现它的内容和方式。我可以理解页面刷新问题,但我会假设分页无论如何都会起作用。以下是我的代码,如果有人能指出任何错误或建议如何最好地完成这项工作,我将不胜感激。

$brandname = $_GET['brandname'];
$picked = $_GET['picked'];
$pickcheck = $_GET['pickcheck'];


$brands =($brandname);
$_SESSION['$brandname']= $brandname;
$pick =($picked);
$_SESSION['$picked']= $pick;
$picker =($pickcheck);
$_SESSION['$pickcheck']=$picker;

$tbl_name="pickme";
$adjacents = 3;

$query = "SELECT COUNT(*) as num FROM tirestock";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages["num"];

$targetpage = "connecttest.php";    //your file name  (the name of this file)
$limit = 5;                                 //how many items to show per page
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$page=mysql_real_escape_string($page);
$page = $_GET['page'];
if($page)
    $start = ($page - 1) * $limit;          
else
    $start = 0;

$sql = "SELECT * FROM tirestock LIMIT $start, $limit";
$result = mysql_query($sql);

if ($page == 0) $page = 1;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$lpm1 = $lastpage - 1;

$pagination = "";
if($lastpage > 1)
{
    $pagination .= "<div class=\"pagination\">";

    if ($page > 1)
        $pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>";
    else
        $pagination.= "<span class=\"disabled\">« previous</span>";

    //pages
    if ($lastpage < 7 + ($adjacents * 2))
    {
        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))
    {

        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>";
        }

        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>";
        }

        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>";
            }
        }
    }


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

【问题讨论】:

    标签: php jquery


    【解决方案1】:

    你有很多冗余代码,所以我先清理一下,看看它是否与问题有关。

    第一个问题:$page上的赋值和检查太多

    $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
    $page=mysql_real_escape_string($page);
    $page = $_GET['page'];
    if($page)
        $start = ($page - 1) * $limit;          
    else
        $start = 0;
    
    if ($page == 0) $page = 1;
    

    例如应该只是:

    $page = isset($_GET['page']) ? max((int)$_GET['page'], 1) : 1;
    $start = ($page - 1) * $limit;
    

    现在您不必检查它是否设置为 0 等。

    第二个问题:我看不到你在哪里使用你的其他 $_GET$_SESSION 变量,但如果你想通过分页保留它们,你必须将它们添加到如果没有设置分页 url,则不使用 emtpy $_GET 变量覆盖会话。

    【讨论】:

    • 感谢您的回复。我已按照您的建议开始清理冗余代码。 $_GET 或 $_SESSION 变量在我发布的代码下方使用,如下所示:
    • 代码是为了提醒用户他们选择了什么。 echo "
      你选择了品牌:$brandname
      " ; echo "
      你选择了 Size: $picked
      "; echo "
      你选择了类型:$pickcheck
      ";
    • 如何通过将 $_GET 和 $_SESSION 变量添加到分页 URL 中来引用它们?
    • @Urob 关于您的最后一个问题,您可以将所有必需的变量(getsession)添加到数组 $query,将页码添加到该数组并使用构建查询字符串http_build_query。在你使用"$targetpage?page=...\" 的地方你现在必须使用"$targetpage?$query\"
    • @Urob 但是您需要先让查询和分页在没有任何其他变量的情况下工作......
    【解决方案2】:
    <?php 
    
    if($nume > $limit ){ // Let us display bottom links if sufficient records are there for paging
    
    /////////////// Start the bottom links with Prev and next link with page numbers /////////////////
    echo "<table align = 'center' width='50%'><tr><td  align='left' width='30%'>";
    //// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
    if($back >=0) { 
    print '<a href="'.$page_name.'?start='.$back.'&sex='.$field.'&searching=yes&find='.$find.'"><font face="Verdana" size="2">PREV</font></a>'; 
    } 
    //////////////// Let us display the page links at  center. We will not display the current page as a link ///////////
    echo "</td><td align=center width='30%'>";
    $i=0;
    
    $l=1;
    
    for($i=0;$i < $nume;$i=$i+$limit){
    if($i <> $eu){
    echo '<a href="'.$page_name.'?start='.$i.'&sex='.$field.'&searching=yes&find='.$find.'"><font face="Verdana" size="2">'.$l.'</font></a> ';
    }
    else { echo "<font face='Verdana' size='4' color=red> ".$l."</font>";}        /// Current page is not displayed as link and given font color red
    
    $l=$l+1;
    }
    
    echo "</td><td  align='right' width='30%'>";
    ///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
    if($this1 < $nume) { 
    print '<a href="'.$page_name.'?start='.$next.'&sex='.$field.'&searching=yes&find='.$find.'"><font face="Verdana" size="2">NEXT</font></a>';} 
    echo "</td></tr></table>";
    
    }// end of if checking sufficient records are there to display bottom navigational link. 
    }  
    
    ?>
    

    【讨论】:

    猜你喜欢
    • 2012-05-11
    • 2016-07-16
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多