分页不适用于第二页

时间:2012-06-16 00:05:00

标签: php jquery

此页面与表格中的查询结果完美地加载。分页已到位,并且已按正确数量划分每个记录的页数。页面上还有其他与表无关的项目。当我尝试转到第二页时,我只获得没有其他记录的标题行,而其他信息显示在页面上作为未定义的索引,几乎就像首先没有查询一样。我在网上搜索了答案,我已经筋疲力尽了解解决这个问题的有限知识。我想要做的就是从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";
}

2 个答案:

答案 0 :(得分:0)

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

第一个问题: $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变量的位置,但如果您希望通过分页保留它们,则可以如果没有设置,请在分页网址中添加或使用emtpy $_GET变量覆盖会话。

答案 1 :(得分:-1)

<?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. 
}  

?>