...

View Full Version : pagination question



karlosio
02-06-2007, 01:02 AM
Hi,

I am trying out a simple test pagination script on records in my database (i have good few stored) I have managed to write a script that displays the page numbers and previous and next links (displaying 10 records per page), but I would like the numbers to cut off, as they go right across the page (nearly:eek: ) say for example, like this:

< Previous 1,2...10,11,12...39,40 Next >

how is this done :confused:

here is the code:



<?php
mysql_connect("localhost", "root", "") or die("ERROR - CANNOT CONNECT TO SERVER");
mysql_select_db("new_test") or die("ERROR - CANNOT OPEN DATABASE");

if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}

$max_results = 10;

$from = (($page * $max_results) - $max_results);

$sql = mysql_query("SELECT * from mix_send order by mix_send_id desc LIMIT $from, $max_results");
while ($row = mysql_fetch_array($sql)) {
?>

<?php echo $row['mix_send_djname']; ?><br />

<?php
}

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num from mix_send"),0);

$total_pages = ceil($total_results / $max_results);

if ($page > 1) {
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">&lt;&lt;Previous</a> ";
}

for($i = 1; $i <= $total_pages; $i++) {
if (($page) == $i) {
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}

if ($page < $total_pages) {
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">&gt;&gt;Next Page</a> ";
}
?>

vinyl-junkie
02-06-2007, 02:13 AM
There is a pagination class (http://www.codingforums.com/showthread.php?t=74614&highlight=pagination) in the Post a PHP Snippet forum which should do this for you.

karlosio
02-06-2007, 03:16 PM
Thanks :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum