Paging problems
Posted: Sat Dec 11, 2004 1:20 pm
I have the following paging function that works perfectly apart from 2 things, the code is not mine, found on the net.
1. I want to display what page you are on ie Page 1 of 10. At the moment I can show the second number but not the first.
2. I have put the code into a table but am having problems with closing the table. All works as expected apart from when you get to the last page. I cant get the closing table tag to appear in the correct place. The code did NOT have tables to start with, I have added these in myself.
Lastly can the code be improved, made more effecient in any way?
1. I want to display what page you are on ie Page 1 of 10. At the moment I can show the second number but not the first.
2. I have put the code into a table but am having problems with closing the table. All works as expected apart from when you get to the last page. I cant get the closing table tag to appear in the correct place. The code did NOT have tables to start with, I have added these in myself.
Lastly can the code be improved, made more effecient in any way?
Code: Select all
<?php
//error_reporting(E_ALL);
$id = $_GET['id'];
$query = 'SELECT COUNT(image_id) FROM skinz_images WHERE cat_id='.$id.'';
$result = mysql_query($query, $skinz) or die(mysql_error());
$row = mysql_fetch_row($result);
$maxrows = 1; // max number of rows per page
$pages = ceil($row[0] / $maxrows);
$firstitem = $_GET['page'] * $maxrows; // 'page' is passed to the script, holding the current page number starting with 0
//$id = '2';
// building the nav bar within a function (see below) and displaying it
//echo buildNavbar($pages);
// repeat the query from above for retrieving the 'real' and LIMITed result
// "SELECT * FROM table WHERE ... LIMIT $firstitem, $maxrows"
// query used in actual preview.php page
function buildNavbar($pages, $id)
{
$tmp_max = 5; // set this to the number of page links to display
$page = $_GET['page'];
$html = "<table class='pnav_bdr' border='0' cellpadding='3' cellspacing='1'><tr>\n";
$html .= "<td class='pnav_control'>Page ?? of $pages</td>\n";
if($page > 0){
$prev = $page - 1;
$html .= "<td class='alt1'><a class='smallfont' href="preview.php?id=$id&page=0" title='First page'><strong>«</strong> First</a></td>\n";
$html .= "<td class='alt1'><a class='smallfont' href="preview.php?id=$id&page=$prev" title='Previous Page'><</a></td>\n";
}
// scrollable page links: show a maximum of $tmp_max links at once
if($pages > $tmp_max){
$tmp_half = floor($tmp_max / 2);
$tmp_start = $page - $tmp_half;
if($tmp_start < 0)
$tmp_start = 0;
$tmp_end = $tmp_start + $tmp_max;
if($tmp_end > $pages)
$tmp_end = $pages;
if($tmp_start > ($tmp_end - $tmp_max))
$tmp_start = $tmp_end - $tmp_max;
}
else{
$tmp_start = 0;
$tmp_end = $pages;
}
for($i = $tmp_start; $i < $tmp_end; $i++){
$num = $i + 1;
if($i == $page){
$html .= "<td class='alt2'>$num</td>\n";
}
else{
$html .= "<td class='alt1'><a class='smallfont' href="preview.php?id=$id&page=$i" title='Show page ".$num."'>$num</a></td>\n";
}
}
if($page < $pages-1){
$last = $pages - 1;
$next = $page + 1;
$html .= "<td class='alt1'><a class='smallfont' href="preview.php?id=$id&page=$next" title='Next page'>></a></td>\n";
$html .= "<td class='alt1'><a class='smallfont' href="preview.php?id=$id&page=$last" title='Last page'>Last <strong>»</strong></a></td>\n";
$html .= "</tr></table>\n";
}
return $html;
}
?>