Jump to content

Pagination Help


marcus

Recommended Posts


I am creating pagination for forums, to limit 20 rows per page. I have the limiting correct, but when it comes to echoing the "Previous PAGENUMBERS Next" part it shows:

 

Previous 1 Next

 

It shows this even when there should be 5 pages. Also, the Previous 1 and Next are not linked, Previous and 1 are only linked when page > 1.

 

$limit          = 20;               
$query_count    = "SELECT count(*) FROM `replies` WHERE `topic` =$id";    
$result_count   = mysql_query($query_count);    
$totalrows      = mysql_num_rows($result_count); 
$pie = $_GET

;
intval($pie);
if(!$pie){
$pie=1;
}
if($pie != 1){ 
        $pageprev = $pie-1;
        
        echo "<a href=\"?act=topic&id=$id&page=$pageprev\">Previous </a> "; 
    }else{
        echo "Previous ";
    }
$limit          = 20;               
    	$query_count    = "SELECT count(*) FROM `replies` WHERE `topic` =$id";    
    	$result_count   = mysql_query($query_count);    
    	$totalrows      = mysql_num_rows($result_count); 
    $pageamt = $totalrows/$limit;
    
    for($pid = 1; $pid <= $pageamt; $pid++){
        if($i == $pie){
            echo "$pid ";
        }else{
            echo "<a href=\"?act=topic&id=$id&page=$pid\">$pid</a> ";
        }
    }

$giv = $totalrows%$limit;
    if($giv != 0){
        if($pid == $pie){
            echo "$pid ";
        }else{
            echo "<a href=\"?act=topic&id=$id&page=$pid\">$pid</a> ";
        }
    }
$give = $totalrows-($limit*$pie);
    if($give > 0){
        $pagenext = $pie+1;
         
        echo "<a href=\"?act=topic&id=$id&page=$pagenext\">Next</a>"; 
    }else{
        echo "Next";
    } 

Link to comment
Share on other sites

I think you have a few errors...

Why are you repeating this twice?

$limit          = 20;               
$query_count    = "SELECT count(*) FROM `replies` WHERE `topic` =$id";    
$result_count   = mysql_query($query_count);    
$totalrows      = mysql_num_rows($result_count);

You would want to use the first one to count the rows, yes that is fine. The second time though, you need to fetch the rows...

 

Probably the biggest error, is that your not actually limiting your results... That is why it is showing you only the one.

 

I recommend that you follow this guide. It is oddly similar to your code.

Here is the full pagination code:

http://www.phpfreaks.com/tutorials/43/5.php

 

Here is the start of the guide (skipping the intro):

http://www.phpfreaks.com/tutorials/43/1.php

 

If you still need help, I will certainly help you! I just think that the guide is probably the best way to help you at this point.

Link to comment
Share on other sites

  • 4 months later...

he... ive just used that tutorial and its not working very good... on my NEXT it counts down 1 and its only one page 1 the page lik actual counts right... then u see alot og response to the tutorial... but on this great php site, which was supposed to be proff... u cant see the responses... i must laugh LOL

Link to comment
Share on other sites

my script looks like this now and it works....

 

<?php
$limit = 20;
db_connect();
$result_count = mysql_query("SELECT * FROM galleries");     
$totalrows = mysql_num_rows($result_count);
echo $totalrows;
if (empty($page)) {
$page = 1;
}
$limitValue = $page*$limit - ($limit);

$sql = mysql_query("SELECT * FROM galleries LIMIT $limitValue,$limit");

$div=0;
while ($gal = mysql_fetch_array($sql)) {
if ($div == 0) {
	echo '<div class="outer_gal">'; }
echo '<div class="one_gal"><a href="'.$gal['link'].'" target="_blank"><img src="'.$gal['thumb'].'" /></a></div>';
$div++;
if ($div == 5) { echo '<div class="clr"></div></div>';$div=0; }

}
if ($div < 5) { echo '<div class="clr"></div></div>'; }
echo '<div>';
if($page != 1){  
        $pageprev = $page-1; 
         
        echo("<a href=\"index.php?action=gallery&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?action=gallery&page=$i\">$i</a> "); 
        } 
    } 


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

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

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.