I have a pagination script but currently it echos out all the page numbers which is well over 1000, Can anyone tell me how I would make it like 1 2 3 .... 1000..

Like vb has it.

Recommended Answers

All 2 Replies

Just to give you an idea, I made this code once (output is here: http://www.pritaeas.net/public/php/pagination/). $current is current page, $total is total pages, $enclose is how many to show next to the current page, $endpoint is used for showing the first and last N pages.

<html>
<head>
<style>
  .current {
    font-weight: bold;
    color: red;
  }
  .previous {
  }
  .next {
  }
</style>
</head>
<body>
<?php
  function createNavigation($current = 1, $total = 1, $enclose = 2, $endpoint = 2) {
    if ($current < 1)
      $current = 1;
    if ($total < 1)
      $total = 1;
    if ($current > $total)
      $current = $total;

    $front = array();
    $middle = array();
    $last = array();

    for ($i = 1; $i <= $endpoint; $i++)
      $front[] = $i;
    for ($i = $current - $enclose; $i <= $current + $enclose; $i++)
      $middle[] = $i;
    for ($i = $total - $endpoint + 1; $i <= $total; $i++)
      $last[] = $i;

    $nav = array_merge($front, $middle, $last);
    $nav = array_unique($nav, SORT_NUMERIC);

    $nav2 = array ();
    foreach ($nav as $val)
      if ($val > 0 && $val <= $total)
        $nav2[] = $val;

    $nav = array ();
    $prev = 0;
    if ($current > 1)
      $nav[] = '<div class="previous"></div>';

    foreach ($nav2 as $val) {
      if ($val > $prev + 1)
        $nav[] = '..';

      if ($val == $current)
        $nav[] = "<span class='current'>$val</span>";
      else
        $nav[] = "$val";

      $prev = $val;
    }

    if ($current < $total)
      $nav[] = '<div class="next"></div>';

    return implode(' ', $nav);
  }
	
echo createNavigation(1, 11);
echo '<hr/>';
echo createNavigation(2, 11);
echo '<hr/>';
echo createNavigation(3, 11);
echo '<hr/>';
echo createNavigation(4, 11);
echo '<hr/>';
echo createNavigation(5, 11);
echo '<hr/>';
echo createNavigation(6, 11);
echo '<hr/>';
echo createNavigation(7, 11);
echo '<hr/>';
echo createNavigation(8, 11);
echo '<hr/>';
echo createNavigation(9, 11);
echo '<hr/>';
echo createNavigation(10, 11);
echo '<hr/>';
echo createNavigation(11, 11);
echo '<hr/>';
echo createNavigation(3, 5);
echo '<hr/>';
echo createNavigation(2, 3);
echo '<hr/>';
echo createNavigation(20, 40, 5, 5);
echo '<hr/>';
echo createNavigation(1, 11, 5);
?>
</body>
</html>

It could do with some optimization, but I've never taken the time to do so.

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.