View Full Version : Pagination Code Not working


liam1412
18-01-2007, 23:52
Hello Techy Peeps

I have ripped this pagination code word for word from a php tutorial site and its not working. Had a post on that site for ages now and no one seems to want to help so wondering if anyone on here had any clue.

Basically what is happening is this.

My limit is set at 10 and on the particular page I am testing it on I know there are 13 records in my result set

The pagination looks like this

PREV 1 NEXT

But it should read

PREV 1 2 NEXT as there should be a second page.

Also NONE OF THEM ARE LINKS

its as if it is ignoring the rest of the result set after the limit

<?php
$limit = 10;
$query_count = "SELECT count(*)FROM forum_answer WHERE ans_topic_id = $topic_id";
$result_count = mysql_query($query_count);
$total_rows = mysql_num_rows($result_count);

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

$limitvalue = $page * $limit - ($limit);

$topic_query = "SELECT * FROM forum_answer WHERE ans_topic_id = $topic_id ORDER BY ans_id LIMIT $limitvalue, $limit";
$topic_result = mysql_query($topic_query)or die("Error: " . mysql_error());


while
($fetch_replies = mysql_fetch_array($topic_result)){
$ans_text = $fetch_replies['ans_text'];
$ans_id = $fetch_replies['ans_id'];
$ans_topic_id = $fetch_replies['ans_topic_id'];
$ans_text = nl2br($ans_text);
$ans_text = BBCODE($ans_text);
?>
<tr>
<td valign="center" width="150" bgcolor="#565656"><p class="grey">
<?php echo $fetch_replies['ans_datetime'];?>
<br />
<br />
<?php echo $fetch_replies['ans_poster_username'];?>
<br />
<img src="images/profilepics/vw.jpg" width="50" height="50" border="2" />
</p></td>
<td valign="top" width="450" bgcolor="#6d6d6d">
<table width="450" cellspacing="0" cellpadding="0" border="0">
<tr>
<td colspan="2"><p class="grey"><?php echo $ans_text; ?></p></td>
</tr>
<tr>
<td width="350" align="right"></td>
<td width="100" align="right">
<br />
<br />
<br />
<br />
<?php if($fetch_replies['ans_poster_username'] == $_SESSION['username']){
?>
<a href="edit_reply.php?ans_id=<?php echo $ans_id ?>&ans_topic_id=<?php echo $ans_topic_id; ?>"><img src="images/edit_post.jpg" width="25" height="25" border="0" alt="Edit Reply" /></a>&nbsp;&nbsp;
<?php
};
?>
</td>
</tr>
</table>
</td>
</tr>
<?php

}
if($page != 1){
$pageprev = $page--;
echo("<a href=\"$PHPSELF&page=$pageprev\">PREV</a> ");
}else{

echo ("PREV");

$numofpages = $total_rows / $limit;
for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else
echo("<a href=\"$PHP_SELF&page=$i\">$i</a> ");
}
}
if(($total_rows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF&page=$i\">$i</a> ");
}
}

if(($total_rows - ($limit * $page)) > 0){
$pagenext = $page++;
echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT</a>");
}else{
echo("NEXT");

}
if($no_of_views == 199){
$sql_hot_topic = "UPDATE forum_question SET topic_status = $topic_update WHERE topic_id = '$topic_id'";
mysql_query($sql_hot_topic)or die("unable to update topic status");
}
$sql_update_topic_view = "UPDATE forum_question SET topic_view = '$topic_view' WHERE topic_id = '$topic_id'";
mysql_query($sql_update_topic_view)or die("failed to update topic_reply");
mysql_close();
?>


Been looking at it for three days now and can't spot anything different to the tutorial apart from I echo my php as and when rather than echoing all the styling etc.

Ghozer
19-01-2007, 01:42
wow, thats a lot for that :O here's a snippet from my blog site that has a similar thing, I dont have Previous / Next (But thats a simple $var + 1 or $var - 1 thing, so easy enough to add...)


if($num != 0)
{
$output .= '<div class="pagetext">View Page: ';
for ($i = 1; $i <= $numP; $i++)
{
$output .= ' | <a href="?viewcomment=' . $p_id . '&commentpage=' . $i . '">' . $i . '</a>'."\n";
}
$output .= '</div>'."\n";
}


That is the page number generator, for the comments pages... I took that out so you could see it speratly,... below is the WHOLE show comments function. hope this helps..


function show_comment($p_id,$stuff,$u_id)
{
if($_GET["commentpage"] == NULL) $page = 0;
else $page = $_GET["commentpage"]-1;

$comPerPage = 10;
$limit = $page*$comPerPage;
$count = count_comments($p_id);
$numP = ceil($count / $comPerPage);
$uid = $user_id;
$sql = "SELECT
*
,com.id AS commentID
FROM
comments com
,users usr
WHERE
com.blog_id = '$p_id'
AND
usr.id = com.user_id
ORDER BY
com.id
LIMIT
$limit, $comPerPage
";
$res = mysql_query($sql) or die(mysql_error());
$num = mysql_num_rows($res);
$output = '<div class="his1">Viewing Comment(s)</div>'."\n";
$output .= '<div class="his2">Current Blog Entry: ' . $p_id . '</div><br />'."\n";
if ($num == 0)
{
$output .= '<div class="hisnoitem">'."\n";
$output .= 'Sorry, There are no comments for this blog entry.'."\n";
$output .= '</div><br /><br />';
}
elseif($num != 0)
{
$output .= '<div class="pagetext">View Page: ';
for ($i = 1; $i <= $numP; $i++)
{
$output .= ' | <a href="?viewcomment=' . $p_id . '&commentpage=' . $i . '">' . $i . '</a>'."\n";
}
$output .= '</div>'."\n";
}
while($row = mysql_fetch_assoc($res))
{
$date = date('l d-m-y \@ H:i', $row["date"]);
$output .= '<div class="itemt">By: <span class="itemtext"><a href="?viewprofile=' . $row['user_id'] . '">' . $row['user'] . '</a></span></div>'."\n";
$output .= '<div class="itemd">Date: <span class="itemtext">' . $date . '</span></div><br />'."\n";
$output .= '<div class="commentcontent">'."\n";
$output .= bb2html($row["content"]);
$output .= '<br /><br />'."\n";
$output .= '</div>'."\n";
if($row['user_id'] == $u_id)
{
$output .= '<div id="editbutt"><span class="editbutttext"><a href="?page=editcomment&cid=' . $row['commentID'] . '&u_id=' . $row['user_id'] . '&b_id=' . $p_id . '">Edit Comment</a></span></div>';
}
$c++;
}
if($num != 0)
{
$output .= '<div class="pagetext">View Page: ';
for ($i = 1; $i <= $numP; $i++)
{
$output .= ' | <a href="?viewcomment=' . $p_id . '&commentpage=' . $i . '">' . $i . '</a>'."\n";
}
$output .= '</div>'."\n";
}
if($stuff == 1)
{
$output .= '<div class="commentbutt"><span class="commentbutttext"><a href="?page=comment&blog=' . $p_id . '">Post Comment</a></span></div>';
}
return $output;
}

John
19-01-2007, 02:33
But I think this is because you are messed up with your block structures.

Tidy up the code by lining up the { and } then you might see what is wrong. For example, I think you are meant to have a } after echo ("PREV");

if($page != 1)
{
$pageprev = $page--;
echo("<a href=\"$PHPSELF&page=$pageprev\">PREV</a> ");
}
else
{
echo ("PREV");


Another thing.

Rather than...

$numofpages = $total_rows / $limit;
for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else
echo("<a href=\"$PHP_SELF&page=$i\">$i</a> ");
}
}
if(($total_rows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF&page=$i\">$i</a> ");
}
}


do this:

$numofpages = $total_rows / $limit;
if(($total_rows % $limit) != 0)
$numofpages++;

for($i = 1; $i <= $numofpages; $i++)
{
if($i == $page)
echo($i." ");
else
echo("<a href=\"$PHP_SELF&page=$i\">$i</a> ");
}

liam1412
21-01-2007, 10:30
Thanks. I'll give these a try now. had a few days off coz it was doin my head in. Ha ha. i'll let you know if they work or not but im sure they will.

Liam