【问题标题】:Link errors with PHP pagination class and AJAX/JQuery使用 PHP 分页类和 AJAX/JQuery 链接错误
【发布时间】:2012-02-14 16:37:32
【问题描述】:

在我的搜索结果页面上,用户可以使用左侧的按钮修改他的搜索结果。这些按钮使用 jquery 来更新 div 中的结果。但是,当使用 jquery 重新调用结果并将结果回显到 div 时,NEXT PAGE、LAST PAGE 等的链接无法正常工作。这是我正在使用的分页类:

  class pagination
  {
    var $page = 1; // Current Page
    var $perPage = 10; // Items on each page, defaulted to 10
    var $showFirstAndLast = false; // if you would like the first and last page options.

    function generate($array, $perPage = 10)
    {
      // Assign the items per page variable
      if (!empty($perPage))
        $this->perPage = $perPage;

      // Assign the page variable
      if (!empty($_GET['page'])) {
        $this->page = $_GET['page']; // using the get method
      } else {
        $this->page = 1; // if we don't have a page number then assume we are on the first page
      }

      // Take the length of the array
      $this->length = count($array);

      // Get the number of pages
      $this->pages = ceil($this->length / $this->perPage);

      // Calculate the starting point 
      $this->start  = ceil(($this->page - 1) * $this->perPage);

      // Return the part of the array we have requested
      return array_slice($array, $this->start, $this->perPage);
    }

    function links()
    {
      // Initiate the links array
      $plinks = array();
      $links = array();
      $slinks = array();

      // Concatenate the get variables to add to the page numbering string
      if (count($_GET)) {
        $queryURL = '';
        foreach ($_GET as $key => $value) {
          if ($key != 'page') {
            $queryURL .= '&'.$key.'='.$value;
          }
        }
      }

      // If we have more then one pages
      if (($this->pages) > 1)
      {
        // Assign the 'previous page' link into the array if we are not on the first page
        if ($this->page != 1) {
          if ($this->showFirstAndLast) {
            $plinks[] = ' <a href="?page=1'.$queryURL.'">&laquo;&laquo; First </a> ';
          }
          $plinks[] = ' <a href="?page='.($this->page - 1).$queryURL.'">&laquo; Prev</a> ';
        }

        // Assign all the page numbers & links to the array
        for ($j = 1; $j < ($this->pages + 1); $j++) {
          if ($this->page == $j) {
            $links[] = ' <a class="selected">'.$j.'</a> '; // If we are on the same page as the current item
          } else {
            $links[] = ' <a href="?page='.$j.$queryURL.'">'.$j.'</a> '; // add the link to the array
          }
        }

        // Assign the 'next page' if we are not on the last page
        if ($this->page < $this->pages) {
          $slinks[] = ' <a href="?page='.($this->page + 1).$queryURL.'"> Next &raquo; </a> ';
          if ($this->showFirstAndLast) {
            $slinks[] = ' <a href="?page='.($this->pages).$queryURL.'"> Last &raquo;&raquo; </a> ';
          }
        }

        // Push the array into a string using any some glue
        return implode(' ', $plinks).implode($this->implodeBy, $links).implode(' ', $slinks);
      }
      return;
    }
  }
?>

如果结果显示在search.php上,jquery修改后的结果是从search_again.php调用的,分页链接(NEXT,LAST等)会跟在search.php后面,不会用search_again.php更新。

我已尝试更改此设置:

$slinks[] = ' <a href="?page='.($this->page + 1).$queryURL.'"> Next &raquo; </a> ';

到这里:

$slinks[] = ' <a href="'.$_SERVER['PHP_SELF'].'?page='.($this->page + 1).$queryURL.'"> Next &raquo; </a> ';

它确实有点工作,但是点击这个链接,而不是局限于search.php上的结果div,它将被加载到整个页面本身。有人对这个有经验么?谢谢

编辑:这是我的 JQuery:

$(document).ready(function(){
$("#button").click(function(){
    var miles = $('#miles').val();
    $.ajax({
            url: 'thetestsession.php', //the variable miles is set being $_GET
            data: 'miles=' + miles,    //on this page and set to session 
            type: 'GET',               //variable. miles is variable
                                               //that a user can modify his 
                                               //search with.
            success: function(results) {
                $("#results").html(results); //#results is the div 
                //alert("Ok.");
            }
        });                               //then i load thetestresults.php
    $("#results").load('thetestresults.php'); //this is the page where the re-query
        return false;                     //takes place.
});                                               
});

【问题讨论】:

  • 在原帖中添加。

标签: php jquery mysql ajax pagination


【解决方案1】:

如果您将 ID“#button”添加到 php.ini 中的链接标签,它可能会起作用。我想你忘记了......如果我错了,请纠正我! 问候

【讨论】:

  • 喜欢$slinks[] = ' &lt;a href="?page='.($this-&gt;page + 1).$queryURL.'#button"&gt; Next &amp;raquo; &lt;/a&gt; '; 吗?这不起作用。
  • 你必须设置 CSS ID 标签:$slinks[] = ' &lt;a id="button" href="?page='.($this-&gt;page + 1).$queryURL.'"&gt; Next &amp;raquo; &lt;/a&gt; ';
猜你喜欢
  • 1970-01-01
  • 2013-10-31
  • 2013-05-30
  • 2010-11-30
  • 1970-01-01
  • 2013-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多