0

Hi,

I have code below works fine but I want links to be listed different way. If you want you can use my code, otherwise I am open for your codes.

Original style:
Previous 1 | 2 | 3 | 4 | 5 | 6 | 7 Next
If it is 1000 pages then, this list will be very long.

What I want is something like this:
« 7 | 8 | 9 | 10 | 11 | 12 | 13 »
or
<< Back 5 6 7 8 Next >>
or
<< Back 1 2 3 ... 6 7 8 Next >>
or
<< Back ... 5 6 7 8 ... Next >>

Simply I don't want to print all the links.

Thanks in advance

<?php

$connection = mysql_connect('localhost', 'root', '');
if (!$connection) {
   	die('Could not connect: ' . mysql_error());
} else {
	mysql_select_db('cartoy', $connection) or die('Could not select database.');
}

//Count how many product we have.
$sql="SELECT COUNT(*) AS totalProduct FROM product";
$runSql=mysql_query($sql);

//Return an object that correspond to the fetched row.
$fetched = mysql_fetch_object($runSql);
$totalItems = $fetched->totalProduct;

echo "Total Number of products in Database: ".$totalItems."<br><br>";

//Get how many record will be displayed at a time.
//Get the current page url.
$limit= $_GET['limit'];
$page= $_GET['page'];

//Set default if: $limit is empty, non numerical.
if((!$limit) || (is_numeric($limit) == FALSE)) {
     $limit = 5; //default
}

//Set default if: $page is empty, non numerical.
if((!$page) || (is_numeric($page) == FALSE)) {
      $page = 1; //default
}

//Calculate total pages.
$totalPages=ceil($totalItems / $limit);
$setLimit=$page * $limit - ($limit);

//Select the products to be listed.
$sql2 = "SELECT * FROM product LIMIT $setLimit, $limit";
$runSql2=mysql_query($sql2);

if(mysql_num_rows($runSql2)==0) die("No matches met your criteria.");
while($code = mysql_fetch_object($runSql2)) {

  //print your data here. 

}

//Previous page.
$previousPage = $page - 1;
if($previousPage >= 1) {
  echo("<a href=http://localhost/pag/pagination.php?limit=$limit&page=$previousPage><b>Prev</b></a>");
}

//Display middle pages.
for($fetched = 1; $fetched <= $totalPages; $fetched++) {
	if($fetched == $page) {
		echo("<b>$fetched</b> | ");
	} else {
		echo(" <a href=http://localhost/pag/pagination.php?limit=$limit&page=$fetched>$fetched</a> | ");
	}
}

//Next page
$nextPage = $page + 1;
if($nextPage <= $totalPages) {
	echo("<a href=http://localhost/pag/pagination.php?limit=$limit&page=$nextPage><b>Next</b></a>");
}


?>

Edited by veledrom: ,

2
Contributors
2
Replies
12
Views
7 Years
Discussion Span
Last Post by veledrom
0

Something like this, output of the code below:
http://www.pritaeas.net/public/php/pagination/

<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>

Edited by pritaeas: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.