Jump to content

Paginating


supanoob

Recommended Posts

Can anyone see why this is showing two pages yet when i click page two it only shows what was on page one again?

 

<?php
//connect to the database
$dbh=mysql_connect ("localhost", "user", "pass") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("wwwcrim_legends");
//end db connect


    $limit          = 25;               
    $query_count    = "SELECT * FROM forum where reply_to='$reply_to' AND post_type='reply' AND deleted='0'";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count); 

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

    $limitvalue = ($page * $limit)- $limit; 
    $query  = "SELECT * FROM forum where reply_to='$reply_to' AND post_type='reply' AND deleted='0' ORDER BY post_id asc LIMIT $limitvalue, $limit";        
    $result = mysql_query($query) or die("Error: " . mysql_error()); 

    if(mysql_num_rows($result) == 0){
        echo("");
    }
    
while($row = mysql_fetch_array($result))
{
$post_body=($row['post_body']);
$posted_on=($row['posted_on']);
$posted_at=($row['posted_at']);
$posted_by_id=($row['posted_by_id']);
$forum_name=($row['forum_name']);
$post_id=($row['post_id']);

$query2="select account_name, account_level from accounts where account_id='$posted_by_id'";
$result2=mysql_query($query2);
if (!$result2)
{
die(mysql_error());
}
$num_rows=mysql_num_rows($result2);

$row=mysql_fetch_array($result2);
$account_name_1=($row['account_name']);
$account_level_1=($row['account_level']);

echo "<center><a name=$post_id><table border=\"0\" width=\"90%\" id=\"table1\" cellspacing=\"1\">

<tr>
	<td width=\"24%\" align=\"center\" bgcolor=\"#999999\">Author Info</td>
	<td align=\"center\" bgcolor=\"#999999\">Message</td>
</tr>
<tr>
	<td width=\"24%\" valign=\"top\" bgcolor=\"#666666\">Name: <a href=\"profile.php?step=main&profile=$posted_by_id\">$account_name_1</a> ($posted_by_id)<br>

	Level: $account_level_1<br>
    Posted At: $posted_at<br>
    Posted On: $posted_on<br>
    Post ID: $post_id</td></td>
	<td valign=\"top\" bgcolor=\"#666666\">$post_body<br>
	<br><br><br><br>
 <table border=\"0\" width=\"100%\" id=\"table2\">
		<tr>
			<td bgcolor=\"#999999\">This will be the signature<br>
 </td>
		</tr>
	</table>[Options: Delete | Edit | Fine | Ban]
	</td>
</tr>
</table><br>
";
}

echo "</center>";

echo "<center>";
if($page != 1){ 
        $pageprev = ($page-1);
        
        echo("<a href=\"view_post.php?post_id=$reply_to&step=$step&page=$pageprev\"><</a> "); 
    }else{
        echo("< ");
    }

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


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

    if(($totalrows - ($limit * $page)) > 0){
        $pagenext = ($page+1);
         
        echo("<a href=\"view_post.php?post_id=$reply_to&step=$step&page=$pagenext\">></a>"); 
    }else{
        echo(">"); 
    }
    
    mysql_free_result($result); 
    
    echo "<br><br>";
    
    ?>

Link to comment
Share on other sites

$page is undefined, and your first query is inefficient. Replace this:

 


   $limit          = 25;               
   $query_count    = "SELECT * FROM forum where reply_to='$reply_to' AND post_type='reply' AND deleted='0'";    
   $result_count   = mysql_query($query_count);    
   $totalrows      = mysql_num_rows($result_count); 

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

 

With this:

 

$limit          = 25;               
$query    = "SELECT COUNT(*) FROM forum where reply_to='$reply_to' AND post_type='reply' AND deleted='0'";    
$result   = mysql_query($query) or trigger_error(mysql_error(),E_USER_ERROR);    
$totalrows      = mysql_result($result,0);
$totalpages = ceil($totalrows/$limit);

if(!isset($_GET['page'])){
$page = 1;
}else{
$page = (int) $_GET['page'];
}
if($page < 1 || $page > $totalpages){//reset $page if the page number does not exist
$page = 1;
}

 

While you beat me to it ProjectFear, thought i'd post anyway since i included simple validation as well as the change to the query.

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.