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