【问题标题】:How to limit pagination function navigation to 10 per page如何将分页功能导航限制为每页 10 个
【发布时间】:2010-08-24 06:33:54
【问题描述】:

我有一个用于我的数据库搜索的分页功能,它将每页的结果限制为 25 个。但是,我有大约 2300 个条目,当有人进行查询大量结果的搜索时,我最终得到 90 个左右页面底部的分页链接。我想将分页导航器限制为一次仅显示 10 页,并根据页面探索进行相应调整。

我不太确定如何调整我的脚本。

任何帮助将不胜感激。

我现在的功能是这样的:

$search_function 是一个用于获取正确 URL 的 javascript 函数,$classical_guitar 指的是图像。

function generate_page_links($cur_page, $num_pages) {
  global $search_function, $classical_guitarL, $classical_guitarR;
  $page_links = '';

  // If this page is not the first page, generate the "previous" link
  if ($cur_page > 1) {
    $page_links .= '<a href="javascript:' . $search_function . "('', '" . ($cur_page - 1) . "');\">" . $classical_guitarL . "</a> ";
  }
  else {
    $page_links .= '';
  }

  // Loop through the pages generating the page number links
  for ($i = 1; $i <= $num_pages; $i++) {
    if ($cur_page == $i) {
      $page_links .= ' ' . $i;
    }
    else {
      $page_links .= '<a href="javascript:' . $search_function . "('', '" . $i . "');\"> " . $i . "</a> ";
    }

  }

  // If this page is not the last page, generate the "next" link
  if ($cur_page < $num_pages) {
    $page_links .= '<a href="javascript:' . $search_function . "('', '" . ($cur_page + 1) . "');\">" . $classical_guitarR . "</a> ";
  }
  else {
    $page_links .= '';
  }

  return $page_links;
}

【问题讨论】:

    标签: php pagination


    【解决方案1】:

    在这里,我修改了你的函数:

    <?php
    
    function generate_page_links($cur_page, $num_pages)
    {
        global $search_function, $classical_guitarL, $classical_guitarR;
        $page_links = '';
    
    
        // If this page is not the first page, generate the "previous" link
        if ($cur_page > 1)
        {
            $page_links .= '<a href="javascript:' . $search_function . "('', '" . ($cur_page - 1) . "');\">" . $classical_guitarL . "</a> ";
        }
        else
        {
            $page_links .= '';
        }
    
        $pager_num = 7; // How many page number you wish to display to the left and right sides of the current page
        $index_start = ($cur_page - $pager_num) <= 0 ? 1 : $cur_page - $pager_num;
        $index_finish = ($cur_page + $pager_num) >= $num_pages ? $num_pages : $cur_page + $pager_num;
        if (($cur_page - $pager_num) > 1) { $page_links .= '...'; } // Display ... when there are more page items than $page_num
    
        // Loop through the pages generating the page number links
        // NOTE: I've modified the for index pointers here...
        for ($i = $index_start; $i <= $index_finish; $i++)
        {
            if ($cur_page == $i)
            {
                $page_links .= ' ' . $i;
            }
            else
            {
                $page_links .= '<a href="javascript:' . $search_function . "('', '" . $i . "');\"> " . $i . "</a> ";
            }
        }
    
        if (($cur_page + $pager_num) < $num_pages) { $page_links .= '...'; } // Display ... when there are more page items than $page_num
    
        // If this page is not the last page, generate the "next" link
        if ($cur_page < $num_pages)
        {
            $page_links .= '<a href="javascript:' . $search_function . "('', '" . ($cur_page + 1) . "');\">" . $classical_guitarR . "</a> ";
        }
        else
        {
            $page_links .= '';
        }
    
        return $page_links;
    }
    
    ?>
    

    希望对你有帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多