Jump to content

[SOLVED] Pagination Displays - PREV5 1 NEXT5


JSHINER

Recommended Posts

I'm using the following code, based on the PHP Freaks tutorial "Easy as PREV 1 2 3 NEXT":

 

@mysql_connect($localhost, $user, $password) or die("ERROR--CAN'T CONNECT TO SERVER"); 
    @mysql_select_db($database) or die("ERROR--CAN'T CONNECT TO DB");

    $limit          = 5;                
    $query_count    = "SELECT count(*) FROM table";     
    $result_count   = mysql_query($query_count);     
    $totalrows      = mysql_num_rows($result_count);  

    if(empty($page)){ 
        $page = 1; 
    } 
         

    $limitvalue = $page * $limit - ($limit);  
    $query  = "SELECT * FROM table ORDER BY date DESC LIMIT $limitvalue, $limit";         
    $result = mysql_query($query) or die("Error: " . mysql_error());  

    if(mysql_num_rows($result) == 0){ 
        echo("Nothing to Display!"); 
    } 

    $bgcolor = "#E0E0E0"; // light gray 
     
    while($row = mysql_fetch_array($result)){ 
    
        if ($bgcolor == "#E0E0E0"){ 
            $bgcolor = "#FFFFFF"; 
        }else{ 
            $bgcolor = "#E0E0E0"; 
        } 

        echo 'Title:', $row['title'], '';
    } 

    if($page != 1){  
        $pageprev = $page--; 
         
        echo("<a href=\"index.php?page=$pageprev\">PREV".$limit."</a> ");  
    }else{ 
        echo("PREV".$limit." "); 
    } 

    $numofpages = $totalrows / $limit;  
     
    for($i = 1; $i <= $numofpages; $i++){ 
        if($i == $page){ 
            echo($i." "); 
        }else{ 
            echo("<a href=\"index.php?page=$i\">$i</a> "); 
        } 
    } 


    if(($totalrows % $limit) != 0){ 
        if($i == $page){ 
            echo($i." "); 
        }else{ 
            echo("<a href=\"index.php?page=$i\">$i</a> "); 
        } 
    } 

    if(($totalrows - ($limit * $page)) > 0){ 
        $pagenext = $page++; 
          
        echo("<a href=\"index.php?page=$pagenext\">NEXT".$limit."</a>");  
    }else{ 
        echo("NEXT".$limit);  
    } 
     
    mysql_free_result($result); 

 

It works well, however it displays PREV5 1 NEXT 5 (text - not links) at the bottom instead of PREV 1 2 3 NEXT. When I type "index.php?page=2" it works, so I know there is a page 1,2,3 etc... however the links at the bottom are not working.

 

Any thoughts ?

Link to comment
https://forums.phpfreaks.com/topic/47150-solved-pagination-displays-prev5-1-next5/
Share on other sites

Looking into this a little more - I have discovered by echoing "totalrows" that it is saying there is only 1 rows, when in fact there are 11 rows. So, what is wrong with my code here:

 

$limit          = 5;                
    $query_count    = "SELECT count(*) FROM table";     
    $result_count   = mysql_query($query_count);     
    $totalrows      = mysql_num_rows($result_count);  

 

$totalrows is not working.

There were a few issues I found in the code:

 

FIRST - starting at $limit - replace with this:

 $limit            = 5;                
    $query    		  = "SELECT count(*) FROM table";     
    $count        	  = mysql_query($query);
    $totalrows        = mysql_result($count,0,0);

 

The end result for $totalrows before was not working.

 

 

SECOND - replace the PREV code with the following:

    if($page != 1){  
        $pageprev = $page-1; 
         
        echo("<a class=\"nextprev\" href=\"index.php?page=$pageprev\">&#171; PREV</a>");  
    }else{ 
        echo("<span class=\"nextprev\">&#171; PREV</span>"); 
    } 

 

The $page-- needs to be $page-1 to work:

 

THIRD - Same thing for NEXT as with PREV

if(($totalrows - ($limit * $page)) > 0){ 
        $pagenext = $page+1; 
          
        echo("<a class=\"nextprev\" href=\"index.php?page=$pagenext\">NEXT &#187;</a>");  
    }else{ 
        echo("<span class=\"nextprev\">NEXT &#187;</span>");  
    } 

 

The $page++ needs to be $page+1

 

 

And that should do it. Let me know if you have any more problems with it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.