Page 1 of 1

help improve code to show ellipses in pagination

Posted: Sat Oct 01, 2011 2:52 am
by malakiahs
Please feel free to use this code in any way if you need to:
I will appreciate any help in rewriting this code to improve it by showing ellipsis.

The way the code is now shows this:
Previous 1 2 3 4 5 6 7 8 9 10 Next

I would like some help in rewriting the code so that we can get an ellipsis and show something like this:
Previous 1 ... 4 5 6 7 ... 10 Next

Please post your improved version of this code (showing the ellipsis).

Thank you in advance.

Code: Select all


//Number of records from query to display per page
$display = 20 ;	
	


if ( isset($_GET['np'])) { // Already been determined.
	
	$num_pages = $_GET['np'];
	
	} else {
		//Now we count the number of records in the query
		
		$query = "SELECT COUNT(*) FROM postings ORDER BY posted_date DESC"; 
		$result = mysql_query($query);
		$row = mysql_fetch_array($result, MYSQL_NUM);
		$num_records = $row[0]; 
		
		//Now we calculate the number of pages
		
		if ($num_records > $display) {  //More than 1 page
			$num_pages = ceil ($num_records/$display);
		} else {
			$num_pages = 1; 
			
			
			}
		
		
		} // End of np IF
	
	
	//Determine where in the database to start returning results
	
	if (isset($_GET['s'])) {
		$start = $_GET['s'];
		
		
		} else {
			$start = 0;
			
			}

      
           //Add code for query here
           $query = //whatever you need from the database tables

      while { // show the results from query here 

        }

if ($num_pages > 1) {
		echo '<br /><p>' ;
		
		$current_page = ($start/$display) + 1 ;
		
		//If it is not the first page, then we make a previous button.
		
		if ($current_page != 1 ) {
			echo ' <a href="viewpostings.php?s=' . ($start - $display) . '&np=' . $num_pages . ' &sort=' . $sort . '">Previous </a>'; 
			
			}
		
		//Make all the numbered pages.
		
		for ($i = 1; $i <= $num_pages; $i++) {
			
			if ($i != $current_page) {
				echo '<a href="viewpostings.php?s=' . (($display * ($i - 1 ))) . '&np=' . $num_pages . ' &sort=' . $sort . '"> ' . $i . ' </a>'; 
				
				} else {
					echo $i. ' '; 
					
					
					}
			
		}
		
		//If it is not the last page, then we make a Next button;
		if ($current_page != $num_pages) {
		echo '<a href="viewpostings.php?s=' . ($start + $display) . '&np=' . $num_pages . ' &sort=' . $sort . '">Next</a>';
		}
		echo '</p>';
		
		
		}