我有一个问题,即我设置了一个PHP分页表,这是从mysql中获取的。一切都运行良好,虽然我遇到的问题是我试图浏览数据。
基本上我想要为相关项目过滤数据,这是从上一页的帖子值中获得的。
虽然当我点击我的链接移动到下一个数据集时,因为我的帖子数据已经被分页取代而我的WHERE语句失败了。如果我只是做一般选择一切过程工作正常。如果我对id进行硬编码,它也可以正常工作,但这需要是动态的。
所以,我的问题是 - 无论如何要保持原始状态或可能保存值?
有更简单的方法吗?
代码:
<?php
$id= $_GET['search'];
require ("db.php");
$tbl_name="LOG"; //your table name
// How many adjacent pages should be shown on each side?
$adjacents = 3;
$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE host_id_fk='$id';";
$total_items = mysql_fetch_array(mysql_query($query));
/* Setup vars for query. */
$targetpage = "search3.php"; //your file name (the name of this file)
$limit = 10; //how many items to show per page
if(isset($_GET['page'])) {
$page = $_GET['page'];
$start = ($page - 1) * $limit; //first item to display on this page
} else {
$page = 0;
$start = 0; //if no page var is given, set start to 0
}
/* Get data. */
$sql = "Select *,
(
CASE
WHEN (LOG.status = 1) THEN
'UP'
WHEN (LOG.status = 0 ) THEN
'DOWN'
ELSE
'State Unknown'
END
) AS `outcome` from Ping.LOG where host_id_fk='$id' LIMIT $start, $limit ;";
$result = mysql_query($sql);
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_items[0]/$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 pagination-centered\"><ul>";
//previous button
if ($page > 1)
$pagination.= "<li><a href=\"$targetpage?page=$prev\">previous</a></li>";
else
$pagination.= "<li class=\"disabled\"><a href=\"#\">previous</a></li>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li class=\"active\"><a href=\"#\">$counter</a></li>";
else
$pagination.= "<li><a href=\"$targetpage?page=$counter\">$counter</a></li>";
}
}
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.= "<li class=\"active\"><a href=\"#\">$counter</a></li>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "<a href=\"#\">...</a>";
$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.= "<a href=\"#\">...</a>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<li class=\"active\"><a href=\"#\">$counter</a></li>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "<a href=\"#\">...</a>";
$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.= "<a href=\"#\">...</a>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li class=\"active\"><a href=\"#\">$counter</a></li>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<li><a href=\"$targetpage?page=$next\">next</a></li>";
else
$pagination.= "<li class=\"disabled\"><a href=\"#\">next</a></li>";
$pagination.= "</ul></div>\n";
}
?>
答案 0 :(得分:2)
将您的search = $ id添加到您的所有分页链接中。例如
$pagination.= "<li><a href=\"$targetpage?page=$prev&search=$id\">previous</a></li>";
另一种选择是将其存储在会话变量中:
session_start();
if(isset($_GET['search']) && is_numeric($_GET['search'])) {
$id= intval($_GET['search']);
$_SESSION['search'] = $id;
} else {
$id = $_SESSION['id'];
}
答案 1 :(得分:1)
您可以将$_GET
值保存到会话中,从而使其保持跨页面加载
<?php
session_start();
if ( ! isset($_SESSION['search'])) $_SESSION['search'] = '';
$_SESSION['search'] = (isset($_GET['search'])) ? $_GET['search'] : $_SESSION['search'];
...
$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE host_id_fk='{$_SESSION['search']}';"
...
$sql = "Select *,
(
CASE
WHEN (LOG.status = 1) THEN
'UP'
WHEN (LOG.status = 0 ) THEN
'DOWN'
ELSE
'State Unknown'
END
) AS `outcome` from Ping.LOG where host_id_fk='{$_SESSION['search']}' LIMIT $start, $limit ;";
显然,您希望使用上述方法确保它是正确的数据类型!
或者作为替代方案,将$_GET['search']
附加到您的分页链接,从而使其保持跨页面加载。
我在这里举一个例子,因为有很多行代码:
$pagination.= "<li><a href=\"$targetpage?page=$counter&search={$_GET['search']}\">$counter</a></li>";