Here is my script I use for pagination but would like to know how I can hide the middle pages. I would like to display the first 4-5 pages and the last 4-5 pages so it looks something like this

<<Prev 1 2 3 4 ........ 20 21 22 23 Next>>

There could be lots of pages and it would look messy with about 100 pages being listed

Any ideas how I could do this?

<? 
//REMEMBER TO CONNECT TO DATABASE! 

include_once("includes/connection.php");

//**EDIT TO YOUR TABLE NAME, ECT. 

$t = mysql_query("SELECT * FROM `events`"); 
  if(!$t) die(mysql_error()); 

$a                = mysql_fetch_object($t); 
$total_items      = mysql_num_rows($t); 
$limit            = $_GET['limit']; 
$page             = $_GET['pagenum']; 

//set default if: $limit is empty, non numerical, less than 1, greater than 50 
if((!$limit)  || (is_numeric($limit) == false) || ($limit < 1) || ($limit > 50)) { 
     $limit = 1; //default 
} 
//set default if: $page is empty, non numerical, less than zero, greater than total available 
if((!$page) || (is_numeric($page) == false) || ($page < 0) || ($page > $total_items)) { 
      $page = 1; //default 
} 

//calcuate total pages 
$total_pages     = ceil($total_items / $limit); 
$set_limit          = $page * $limit - ($limit); 

//query: **EDIT TO YOUR TABLE NAME, ECT. 

$q = mysql_query("SELECT id,title,content, DATE_FORMAT(eventdate, '%W %D %M %Y') as date, photo FROM events ORDER BY id DESC LIMIT $set_limit, $limit"); 
  if(!$q) die(mysql_error()); 
     $err = mysql_num_rows($q); 
       if($err == 0) die("No matches met your criteria."); 

//Results per page: **EDIT LINK PATH** 

//show data matching query: 
while($code = mysql_fetch_array($q)) {
    $EventDate = $code['eventdate'];
    echo "<h3>".$code['title']."</h3><p class='style3' style='text-align:left'><i>Event date: <b>".$code['date']."</b></i></p><p class=\"style3\"><img src=/images/".$code['photo']." align=\"right\" class=\"right\">".nl2br($code['content'])."</p><br>\n";
} 

$id = urlencode($id); //makes browser friendly 

//prev. page: **EDIT LINK PATH** 

$prev_page = $page - 1; 

if($prev_page >= 1) { 
  echo("<b>&lt;&lt;</b> <a href=/events/$prev_page><b>Prev.</b></a>"); 
} 

//Display middle pages: **EDIT LINK PATH** 

for($a = 1; $a <= $total_pages; $a++) 
{ 
   if($a == $page) { 
      echo("<b> $a</b> | "); //no link 
     } else { 
  echo("  <a href=/events/$a> $a </a> | "); 
     } 
} 

//next page: **EDIT THIS LINK PATH** 

$next_page = $page + 1; 
if($next_page <= $total_pages) { 
   echo("<a href=/events/$next_page><b>Next</b></a> &gt; &gt;"); 
} 

//all done 
?>
    Write a Reply...