0

我有一个问题,我设置了一个从 mysql 获取的 PHP 分页表。一切正常,尽管我遇到的问题是当我尝试浏览数据时。

基本上,我希望为相关项目过滤数据,这些数据是从上一页的帖子值中获得的。

虽然当我点击我的链接移动到下一个数据集时,它是空白的,因为我的帖子数据已被分页替换并且我的 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";   
  }
?>
4

2 回答 2

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'];
}
于 2013-06-14T19:41:10.413 回答
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>";
于 2013-06-14T19:43:36.110 回答