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>");
}
?>