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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.