Page 1 of 1

Paging problems

Posted: Sat Dec 11, 2004 1:20 pm
by therat
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?

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>&laquo;</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>&raquo;</strong></a></td>\n";
       $html .= "</tr></table>\n";
   }
  
   return $html;
}

?>

Posted: Sat Dec 11, 2004 2:00 pm
by Christopher

Code: Select all

$html .= "Page $page of $tmp_end";

Posted: Sun Dec 12, 2004 9:11 am
by therat
arborint wrote:

Code: Select all

$html .= "Page $page of $tmp_end";
With this the page number is always 1 behind, showing Page 0 of 5 when I am on page 1 etc, this happens all the way through. Also when you first go to the page I get an Undefined index: page, how do I fix that aswell.

Posted: Sun Dec 12, 2004 12:00 pm
by rehfeld
if its always 1 behind, then just add 1 before you display it


for the undefined index, use isset

Code: Select all

$page = 1; // default

// make sure its set, 
// and then that its ONLY a number, 
// and then that the num is greater than 0
if (isSet($_GET['page']) && ctype_digit($_GET['page']) && $_GET['page'] > 0) {
    $page = $_GET['page'];
}

Posted: Sun Dec 12, 2004 1:37 pm
by therat
rehfeld wrote:if its always 1 behind, then just add 1 before you display it


for the undefined index, use isset

Code: Select all

$page = 1; // default

// make sure its set, 
// and then that its ONLY a number, 
// and then that the num is greater than 0
if (isSet($_GET['page']) && ctype_digit($_GET['page']) && $_GET['page'] > 0) {
    $page = $_GET['page'];
}
I have added this to the very top of the page, outside of the function but it still shows Undefined index page on two lines. They are

Code: Select all

$firstitem = $_GET['page'] * $maxrows;
and

Code: Select all

$page = $_GET['page'];
The first one is outside of the function, while the second is inside. Should I be using global $page inside the function.

Posted: Sun Dec 12, 2004 1:56 pm
by rehfeld
learn how and why to use

isset() and empty()

Posted: Sun Dec 12, 2004 6:33 pm
by timvw
yet 3 functions to add to your never-to-forget list :)
[php_man]isset[/php_man]
[php_man]empty[/php_man]
[php_man]array_key_exists[/php_man]

Posted: Mon Dec 13, 2004 1:24 am
by localhost
this seems to work for me

Code: Select all

<?
 				   $sizer=mysql_query("select news_id from tbl_news");
				   $total_numbers=mysql_num_rows($sizer);
				   $divided=$total_numbers/5;
				   $rounded=round($divided);
				   $temp=$divided-$rounded;
				   if($temp>0)
				   $total_number2=$rounded+1;
				   else
				   $total_number2=$rounded;
				   for($pk=1;$pk<=$total_number2;$pk++)
				       &#123;
					   $s_max=$pk*5;
					   $s_min=$s_max-5;
					   if($pk==1)
					      &#123;
					   $resultset=mysql_query("select news_id from tbl_news order by news_id limit 5");
					   $totalcal=mysql_num_rows($resultset);
					   $min_here=mysql_result($resultset,0,'news_id');
					   $max_here=mysql_result($resultset,$totalcal-1,'news_id');
					   print "<a href='news.php?maximum=$max_here&minimum=$min_here'>$pk</a>&nbsp;";
					   	      &#125;
						else
						    &#123;
						$min_here=$max_here;
						$max_here=$max_here+5;
						$resultset=mysql_query("select news_id from tbl_news where news_id>$min_here order by news_id limit 5");
						$totalcal=mysql_num_rows($resultset);
					    $min_here=mysql_result($resultset,0,'news_id');
					    $max_here=mysql_result($resultset,$totalcal-1,'news_id');
						print "<a href='news.php?maximum=$max_here&minimum=$min_here'>$pk</a>&nbsp;";
					   	   	&#125;
					   &#125;
				  
				  ?>

Posted: Tue Dec 14, 2004 4:35 pm
by therat
Thanks for all the suggestions, I have managed to get rid of all the undefined index problems. I do however have another problem.
When setting $page = 1 it makes the part that displays Page 1 of 5 display correctly, however the clickable links show the current page as 2. Changing it to $page = 0 swaps things round with the links correct and Page 0 of 5 incorrect.
Is this an easy fix, any pointers please.