0

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

2 回答 2

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变量,但是如果您想通过分页保留它们,则必须将它们添加到分页 url 中,或者$_GET如果没有设置,则不要用 emtpy 变量覆盖会话。

于 2012-06-16T00:56:34.910 回答
-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. 
}  

?>
于 2014-04-25T09:32:23.683 回答