0

I have my pagination page links like this.And the pagegrows like:

1 2 3 4 5 6 7 ,8,9,10 and goes on.

But i want to limit this so if there is more than 5 pages it shall only show 5 links like this:

1 2 3 4 5... 97 98 99 where 99 is the last page.

And if you go to next page it will only change the first pages like this:

3 4 5 ... 97 98 99

 function pagination($current_page_number, $total_records_found, $query_string = null)
 {
$page = 1;

echo "Page: ";

for ($total_pages = ($total_records_found/NUMBER_PER_PAGE); $total_pages > 0;   $total_pages--)
{
    if ($page != $current_page_number)
        echo "<a href=\"?page=$page" . (($query_string) ? "&$query_string" : "") .   "\">";

    echo "$page ";


  require_once('inc/database.php'); 

  define("NUMBER_PER_PAGE", 5); //number of records per page of the search results

  $page = ($_GET['page']) ? $_GET['page'] : 1;
  $start = ($page-1) * NUMBER_PER_PAGE;

  $sql = "SELECT * FROM members WHERE 1=1";


    $total_records = mysql_num_rows(mysql_query($sql));

    //we limit our query to the number of results we want per page
    $sql .= " LIMIT $start, " . NUMBER_PER_PAGE;


    // we display our pagination at the top of our search results

    pagination($page, $total_records, "id=$id&username=$username&email=$email");

    $loop = mysql_query($sql)
    or die ('cannot run the query because: ' . mysql_error());

      while ($record = mysql_fetch_assoc($loop))
    echo "<br/>{$record['id']}) " . stripslashes($record['username']) . " -    {$record['email']}";

    echo "<center>" . number_format($total_records) . " search results found</center>";


    if ($page != $current_page_number)
        echo "</a>";

    $page++;
user3815328
  • 47
  • 2
  • 11
  • see the answer in this similar post: http://stackoverflow.com/questions/8361808/limit-pagination-page-number – mOna Nov 20 '14 at 18:42

1 Answers1

0

Pagination is one of those things that has been done so many times that you're better off figuring out how to use a pre made class like this one http://code.tutsplus.com/tutorials/how-to-paginate-data-with-php--net-2928

The basics of what you need to do are calculate how many links you want to see on either side of the elipses ... and then make loops to create those links. The ceil() function may be new for you, but it returns a fraction always rounded up.

$buffer = 3;
$results_per_page = 5;
$page = ($_GET['page']) ? $_GET['page'] : 1;
$start = ($page-1) * $results_per_page;

$sql = "SELECT * FROM members WHERE 1=1";
$total_records = mysql_num_rows(mysql_query($sql));
$total_pages = ceil(intval($total_records) / $results_per_page);

for ($x = $page - $buffer; $x < $page; $x++){
    echo "<a href=\"?page=$x" . (($query_string) ? "&$query_string" : "") .   "\"> $x</a>";
}
echo " ... ";

for ($x = $total_pages - $buffer; $x <= $total_pages; $x++){
    echo "<a href=\"?page=$x" . (($query_string) ? "&$query_string" : "") .   "\"> $x</a>";
}

This quick little bit of code does not take in to consideration result sets small enough to fit all links in to one line. Which is why I suggest using the pagination classes that exist already.

Zombiesplat
  • 943
  • 8
  • 19
  • your code works but the problem is when someone is in page 0 it shows -2,-1 page links and whenever is clicked there is error as expected – user3815328 Aug 04 '14 at 08:33
  • That was exactly my point with using a pre made class, they take those considerations already. Your question was about how to make it skip 50 pages worth and do the 3 first and 3 last which is what I answered, however I realized that you'd need more than that and I wasn't willing to write an entire pagination class when someone else already has. – Zombiesplat Aug 04 '14 at 14:13