如何将分页功能导航限制为每页10个

时间:2010-08-24 06:33:54

标签: php pagination

我有一个分页功能,我用于我的数据库搜索,将每页的结果限制为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;
}

1 个答案:

答案 0 :(得分: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;
}

?>

希望它有用......