tags:

views:

94

answers:

1

I have a pagination function that I use for my database search that limits the results per page to 25. However, I have roughly 2300 entries and when someone does a search that queries a lot of results I end up with 90 or so pagination links at the bottom of my page. I would like to limit the pagination navigator to only show 10 pages at a time, adjusting accordingly with page spelunking.

I'm not quite sure how to adjust my script.

Any help would be greatly appreciated.

My current function is so:

$search_function is a javascript function for getting the proper URL, $classical_guitar is referring to images.

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;
}
A: 

Here, I've modified your function:

<?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;
}

?>

Hope it is helpful...

Otar
Thank you so much! This worked perfectly.
MBguitarburst