Jump to content

Archived

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

Noskiw

still have a pagination problem!

Recommended Posts

<?php

$id = $_GET['id'];

$page = (!$_GET['page'] || $_GET['page'] < 0) ? "1" : $_GET['page'];
$page = ceil($page);
$limit = 10;

$start = $limit;
$end = $page*$limit-($limit);

if(isset($id)){
$sql = "SELECT * FROM forum_topics WHERE id='".$id."'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0){
echo "This topic does not exist!\n";
}else{
$row = mysql_fetch_assoc($res);
$sql2 = "SELECT admin FROM forum_sub_cats WHERE id='".$row['cid']."'";
$res2 = mysql_query($sql2) or die(mysql_error());
$row2 = mysql_fetch_assoc($res2);
if($row2['admin'] == 1 && $admin_user_level == 0){
echo "You cannot view this topic because you are not an admin!\n";
}else{
$a = (isa($row['uid'])) ? "<font style=\"color:#800000;\">ADMIN</font>" : "";
echo "<table border=\"0\" width=\"100%\" cellspacing=\"3\" cellpadding=\"3\">\n";
echo "<tr><td colspan=\"2\" align=\"left\"  class=\"forum_header\"><b><font size=\"3\">".$row['title']."</font></b><font size=\"3\"> - Posted On: <em>".$row['date']."</em></font></td></tr>\n";
echo "<tr><td align=\"left\" width=\"15%\" valign=\"top\"  class=\"forum_header\">".uid($row['uid'], true)."<br>Posts: 0<br>".$a."</td>";
echo "<td align=\"left\" valign=\"top\" class=\"forum_header\">";
echo topic($row['message']);
echo "</td>\n";
echo "</tr>\n";

$amount_check = "SELECT * FROM forum_replies WHERE tid='".$tid."'";
$amount_check_res = mysql_query($amount_check) or die(mysql_error());
$amount_count = mysql_num_rows($amount_check_res);
$pages = ceil($amount_count/$limit);

$previous = ($page-1 <= 0) ? "« Prev" : "<a href=\"./index.php?act=topic&id=".$id."&page=".($page-1)."\">« Prev</a>";
$nextpage = ($page+1 > $pages) ? "Next »" : "<a href=\"./index.php?act=topic&id=".$id."&page=".($page+1)."\">Next »</a>"; 
echo "<tr><td align=\"right\" colspan=\"2\">\n";
echo "Pages: ";

echo $previous;
for($i=1;$i<=$pages;$i++){
    $href = ($page == $i) ? " ".$i." " : " <a href=\"./index.php?act=create&id=".$i."\">".$i."</a> ";
    
    echo $href;
}
echo $nextpage;
echo "</td></tr>\n";
$select_sql = "SELECT * FROM `forum_replies` ORDER BY id ASC LIMIT ".$end.",".$start."";
$select_res = mysql_query($select_sql) or die(mysql_error()); 

while($rowr = mysql_fetch_assoc($select_res)){
echo "<tr><td colspan=\"2\" align=\"left\"  class=\"forum_header\"><font size=\"3\"> - Posted On: <em>".$rowr['date']."</em></font></td></tr>\n";
echo "<tr><td align=\"left\" width=\"15%\" valign=\"top\"  class=\"forum_header\">".uid($rowr['uid'], true)."<br>Posts: 0<br>".$a."</td>";
echo "<td align=\"left\" valign=\"top\" class=\"forum_header\">";
echo topic($rowr['message']);
echo "</td>\n";
echo "</tr>\n";
}

echo "<form method=\"post\" action=\"./forum-index.php?act=reply&id=".$row['id']."\">\n";
echo "<tr><td colspan=\"2\" align=\"center\"><textarea style=\"width:90%\" name=\"reply\"></textarea><br><input type=\"submit\" name=\"submit\" value=\"add reply!\" style=\"width:90%\" /></td></tr>\n";

echo "</table>\n";
}
}
}else{
echo "Please view a valid topic!\n";
}

?>

 

the problem is that the replies show up in both topics, theyre not meant to.

 

and the page number and links to the next page wont show up.

Share this post


Link to post
Share on other sites

$select_sql = "SELECT * FROM `forum_replies` ORDER BY id ASC LIMIT ".$end.",".$start."";

You need a WHERE clause that specifies to just get the replies for the topic in question. Missed that step but otherwise looks pretty good.

Do that and then we'll see what can be done about the "previous" and "next" links not showing up...

Share this post


Link to post
Share on other sites

$select_sql = "SELECT * FROM `forum_replies` ORDER BY id ASC LIMIT ".$end.",".$start."";

You need a WHERE clause that specifies to just get the replies for the topic in question. Missed that step but otherwise looks pretty good.

Do that and then we'll see what can be done about the "previous" and "next" links not showing up...

 

but i don't know where to add the where statement

 

maybe it could be

 

$select_sql = "SELECT * FROM `forum_replies` ORDER BY id ASC LIMIT WHERE id= ".$end.",".$start."";

Share this post


Link to post
Share on other sites

$select_sql = "SELECT * FROM `forum_replies`WHERE id= ".$id." ORDER BY id ASC LIMIT  ".$end.",".$start."";

I just noticed, end and start seem to be reversed. lol

example of getting the first through 30th entry

$select_sql = "SELECT * FROM `forum_replies`WHERE id= ".$id." ORDER BY id ASC LIMIT 0,29"

the next 30

$select_sql = "SELECT * FROM `forum_replies`WHERE id= ".$id." ORDER BY id ASC LIMIT 30,59"

Share this post


Link to post
Share on other sites

what you are doing with pagination is getting reply x through z, right?

on page one, how many replies do you want to show?

if you wanted to show 30 replies, you'd use a query of

$select_sql = "SELECT * FROM `forum_replies`WHERE id= ".$id." ORDER BY id ASC LIMIT 0,29"

0 is the first returned result from mysql (1 is the second returned)...

I'm guessing you didn't write this stuff yourself then, you've just been modifying it.

Share this post


Link to post
Share on other sites

how many replies were there supposed to be? I may be missing something in your code (4 hours of sleep will do that sometimes)

Share this post


Link to post
Share on other sites

wait a second, what is the table structure for your forum_replies table? What are the fields?

$select_sql = "SELECT * FROM `forum_replies`WHERE XXX= ".$id." LIMIT ".$Start.", ".$End." ORDER BY id ASC "

XXX column is the column that relates the reply to the original post.

with

$select_sql = "SELECT * FROM `forum_replies`WHERE id= ".$id." ORDER BY id ASC LIMIT 0,29"

u are only pulling the reply that happens to have the same id as the id of the original post hence why you only get one.

Share this post


Link to post
Share on other sites

forum_replies structure

 

id: int(11) primary key auto_increment
tid: int(11)
uid: int(11)
message: text
date: varchar(64)
time: int(25)

Share this post


Link to post
Share on other sites

I'm guessing tid is "Topic id" creating the relation between the topic and the reply. correct?

so, query for all replies for topic selected would be

$select_sql = "SELECT * FROM `forum_replies`WHERE tid = ".$id." LIMIT ".$Start.", ".$End." ORDER BY id ASC "

Share this post


Link to post
Share on other sites

comes up with this

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' ORDER BY id ASC' at line 1 

Share this post


Link to post
Share on other sites

umm, hmm, lets add something for debugging purpose.

$select_res = mysql_query($select_sql) or die(mysql_error()); 

to

$select_res = mysql_query($select_sql) or die(mysql_error()."<br>".$select.res); 

that way if gives us the error message plus the query that was made so we can see what may have gone wrong. post what you get please.

Share this post


Link to post
Share on other sites

umm, hmm, lets add something for debugging purpose.

$select_res = mysql_query($select_sql) or die(mysql_error()); 

to

$select_res = mysql_query($select_sql) or die(mysql_error()."<br>".$select.res); 

that way if gives us the error message plus the query that was made so we can see what may have gone wrong. post what you get please.

 

this again

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' ORDER BY id ASC' at line 1
res 

Share this post


Link to post
Share on other sites

sorry, 4 hours of sleep is catching up with me dude.

$select_res = mysql_query($select_sql) or die(mysql_error()."<br>".$select.res); 

to

$select_res = mysql_query($select_sql) or die(mysql_error()."<br>".$select_sql); 

 

again, sorry about that. try it again...

Share this post


Link to post
Share on other sites

even more

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY id ASC' at line 1
SELECT * FROM `forum_replies` WHERE tid=21 LIMIT 0, 10 ORDER BY id ASC 

Share this post


Link to post
Share on other sites

ummm, crap. we need a mysql guru for quick answer! meanwhile, test a fail to test and success.

move the LIMIT to the end like so:

$select_sql = "SELECT * FROM `forum_replies`WHERE tid = ".$id." ORDER BY id ASC LIMIT ".$Start.", ".$End

this is just shooting in the dark, that's all. I'm not an expert, barely would consider myself above novice.

Share this post


Link to post
Share on other sites

ummm, crap. we need a mysql guru for quick answer! meanwhile, test a fail to test and success.

move the LIMIT to the end like so:

$select_sql = "SELECT * FROM `forum_replies`WHERE tid = ".$id." ORDER BY id ASC LIMIT ".$Start.", ".$End

this is just shooting in the dark, that's all. I'm not an expert, barely would consider myself above novice.

 

ok now it's working

 

this is it now

 

$select_sql = "SELECT * FROM `forum_replies`WHERE tid = ".$id." ORDER BY id ASC LIMIT ".$end.", ".$start."";

 

but now onto the pagination problem, i can't seem to get the number to show up, + the fact the the next link wont show up.

Share this post


Link to post
Share on other sites

The problem I think is with this

$nextpage = ($page+1 > $pages) ? "Next »" : "<a href=\"./index.php?act=topic&id=".$id."&page=".($page+1)."\">Next »</a>"; 

could it be

$nextpage = ($page+1 >= $pages) ? " Next »" : " <a href=\"./index.php?act=topic&id=".$id."&page=".($page+1)."\">Next »</a>"; 

I think that your fist page is 0, $pages has become 2

(13/10) = 1.3

round up is 2

$page = 1 initially, so

(1+1 >= 2) instead of (1+1 > 2)

Share this post


Link to post
Share on other sites

The problem I think is with this

$nextpage = ($page+1 > $pages) ? "Next »" : "<a href=\"./index.php?act=topic&id=".$id."&page=".($page+1)."\">Next »</a>"; 

could it be

$nextpage = ($page+1 >= $pages) ? " Next »" : " <a href=\"./index.php?act=topic&id=".$id."&page=".($page+1)."\">Next »</a>"; 

I think that your fist page is 0, $pages has become 2

(13/10) = 1.3

round up is 2

$page = 1 initially, so

(1+1 >= 2) instead of (1+1 > 2)

 

i have tried the one that you gave me, all it did was seperate the next and prev, the page number doesn't show up and the link still isn't there. i think i need to get rid of the question mark.

Share this post


Link to post
Share on other sites

(see the messages I posted on your forum  lol)

I added spaces to be sure they came up not as all one word.

the ? is there as part of the logic.

if $page+1 >= $pages then if it is show " Next »" else show the link.

!!!!F*CK, i'm tired.

less than! you want next as a link to the next one if there is a next page. god damn it!

$nextpage = ($page+1 <= $pages) ? " Next »" : " <a href=\"./index.php?act=topic&id=".$id."&page=".($page+1)."\">Next »</a>";

umm, but don't count on that working... lol    SOME ONE ELSE HELP PLEASE TOO

 

lets get this working than work on the numbers issue.

for debugging purposes, echo $page and $pages somewhere and make sure they are what you expect them to be.

echo "<br>page = ".$page;
echo "<br>pages = ".$pages;

Share this post


Link to post
Share on other sites

the pages keep on going, how do i make html entities work? like the ampersand (&) and the < and >. cause i don't know how to yet. also, i saw the posts you put, i will work on it.

 

and what i mean by the pages keep on going, when it goes to &page=2, it carries on forever, i went all the way to 100 and it was still there.

Share this post


Link to post
Share on other sites

×

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.