range in pagination showing non existent pages

so i have this set up so that it shows the current page and 5 pages numbers on either side… when its near or at the end, it keeps showing pages that dont exist. example: 24 pages… still shows 25, 26, 27, 28 if ur on page 23

heres the code. maybe u can help

[code]<?php

$host = "xxxxxxxx";
$username = "xxxxxx";
$password = "xxxxxx";
$db = "xxxxxxxxx";
mysql_connect($host,$username,$password) or die ("error2");
mysql_select_db($db) or die("error3");

if($_GET[‘type’] == “thing”) {

//// save the script as pager.php 
// create a database names sam_test
// create a table MyTable with field name and email
// that's all u need to run the script 
 
///////////////////////////////////////////////
// getPagetData Function
// parameter -- 
 // 			$numHits = total no of elements , 
//			$limit   = no of elements per page,
//      $ctbpage    = current page no
///////////////////////////////////////////////	

function getPagerData($numHits, $limit, $ctbpage) 
{ 
       $numHits  = (int) $numHits; 
       $limit    = max((int) $limit, 1); 
       $ctbpage     = (int) $ctbpage; 
       $numPages = ceil($numHits / $limit); 

       $ctbpage = max($ctbpage, 1); 
       $ctbpage = min($ctbpage, $numPages); 

       $offset = ($ctbpage - 1) * $limit; 

       $ret = new stdClass; 

       $ret->offset   = $offset; 
       $ret->limit    = $limit; 
       $ret->numPages = $numPages; 
       $ret->ctbpage     = $ctbpage; 

       return $ret; 
} 

// connect with mysql database 
    


// get page no from user to move user defined page    
$ctbpage = $_GET['ctbpage']; 
$id = $_GET['id'];

// no of elements per page 
$limit = 5; 

// simple query to get total no of entries
$result = mysql_query("select count(*) from apctboxes ORDER BY `id` DESC"); 
$total = mysql_result($result, 0, 0); 

// work out the pager values 
$pager  = getPagerData($total, $limit, $ctbpage); 
$offset = $pager->offset; 
$limit  = $pager->limit; 
$ctbpage   = $pager->ctbpage; 

// use pager values to fetch data 
$query = "select * from apctboxes ORDER BY `id` DESC limit $offset, $limit"; 
$result = mysql_query($query); 

$color = array("#333333","#2a2a2a");
$tcolor = #008aff
$num_colors = count($color)-1;
$i = 0;
$l = $pager->numPages; //for if i want to make a last page button
$last = ($l * $numPages); // same thing i guess
while($row = mysql_fetch_array($result)){
//Resets the color counter to 0 if it gets too high
if($i > $num_colors){$i = 0;}

echo "stuff here";

$i++;
}
echo “

”;
// use $result here to output page content 

// output paging system (could also do it before we output the page content) 
if ($ctbpage == 1) // this is the first page - there is no previous page 
    ; 
else            // not the first page, link to the previous page 
    echo "<ul><li><a href="?page=stuff&type=thing&ctbpage=" . ($ctbpage - 1) . ""> &lt;Prev</a></li>"; 

for ($i = $pager->ctbpage - 5; $i <= $pager->ctbpage + 5; $i++) {
if ($i < 2){$i = 1;} //so when on 1st pg, it dont show pages 0 and -#'s
if ($i == $pager->ctbpage)

        echo "<li class="current">$i</li>"; 
    else 
        echo "<li><a href="?page=stuff&type=thing&ctbpage=$i">$i</a></li>"; 
} 

if ($ctbpage == $pager->numPages) // this is the last page - there is no next page 
   ; 
else  // not the last page, link to the next page 
    echo "<li><a href="?page=stuff&type=thing&ctbpage=" . ($ctbpage + 1) . ""> Next></a></li>";

echo " ( $l total pages )



";
} else{
echo “lol”;
}
?>[/code]

sooo… nothing at all?

i’t weekend, give us some time to reply:

for ($i = $pager->ctbpage - 5; $i <= $pager->ctbpage + 5 and $pager->ctbpage <= $l; $i++)

gave that a try. it had no effect at all :(

what do u get on echo($l); ?
or maybe on echo($last); ?

echo($l); gives me the number 24 (the real last page)

echo($last); Gives me a zero 0

note i just noticed that im not sure why i have $last in there at all, i was going to use it until $l worked first (seeing how it doesnt work anyway… i guess u can disregard it lol.)

my fault:

for ($i = $pager->ctbpage - 5; $i <= $pager->ctbpage + 5 and $i <= $l; $i++)

wow i thank you sooooo much. ive been trying to fix that for about 3 days now lol. You are one of the 2 people out of many that took the time. Means alot to a semi new person at this stuff:P

who else?

a guy over at Deviant Art

he helped me with setting up the initial range part and setting so it dont show negative pages

for ($i = $pager->ctbpage - 5; $i <= $pager->ctbpage + 5; $i++) { if ($i < 2){$i = 1;}

Sponsor our Newsletter | Privacy Policy | Terms of Service