forum pagination trouble?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simmsy
Forum Newbie
Posts: 24
Joined: Wed Apr 14, 2010 9:50 am

forum pagination trouble?

Post by simmsy »

Hi im having trouble getting the topic replies to form in pagination results? please help.
Heres the code:

Code: Select all

 <?php
  $url=curPageURL();
  //Connect to DB 
include("connect.php"); 
// get value of id that sent from address bar
$web_id=$_GET['id'];

  $searchresult=mysql_query("SELECT id, username, title, detail FROM forum_topic WHERE id='$web_id'") or die(mysql_error()); 

while($row=mysql_fetch_array($searchresult)) { 
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
  
  								$id = $row['id'];
                                $username = $row['username'];
                                $title = $row['title'];
								$detail = $row['detail'];
								
 
			
                               echo "
              
                <tr><td width='20'># $id</td><td width='100' align='center' valign='middle'><a href='forum-viewtopic.php?id=$id'><b>$username: </b></a></td>
                <td valign='middle'><em>$title</em></td></tr>
				<tr><td colspan='3'>$detail</td></tr>
				<tr><td colspan='3'><hr size='1'></td></tr>";
} 

?>
<p />
<?php
$limit = 2; //Number of results per page  
$page=$_GET["page"]; //Get the page number to show 
if($page == "") $page=1; //If no page number is set, the default page is 1 

//Get the number of results 
$searchresult=mysql_query("SELECT * FROM search ORDER BY id") or die(mysql_error()); 
$numberofresults=mysql_num_rows($searchresult); 

//Get the number of pages 
$numberofpages=ceil($numberofresults/$limit); 
  $searchresult=mysql_query("SELECT id, username, reply FROM forum_reply ORDER BY id LIMIT " . ($page-1)*$limit . ",$limit") or die(mysql_error()); 
  
while($row=mysql_fetch_array($searchresult)) { 
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
  
  								$id = $row['id'];
                                $username = $row['username'];
                                $reply = $row['reply'];
								
 
			
                               echo "
              
                <tr><td width='100' align='center' valign='middle'><b>$username: </b></td>
				<tr><td colspan='2'>$reply</td></tr>
				<tr><td colspan='2'><hr size='1'></td></tr>";
} 

$nav=""; 
if($page > 1) { 
$nav .= "<a href=\"viewtopic.php?page=" . ($page-1) . "&searchstring=" .urlencode($searchstring) . "\"><< Prev</A>"; 
} 
for($i = 1 ; $i <= $numberofpages ; $i++) { 
if($i == $page) { 
$nav .= "<b>$i</b>"; 
}else{ 
$nav .= "<a href=\"viewtopic.php?page=" . $i . "&searchstring=" .urlencode($searchstring) . "\"> $i </A>"; 
} 
} 
if($page < $numberofpages) { 
$nav .= "<a href=\"viewtopic.php?page=" . ($page+1) . "&searchstring=" .urlencode($searchstring) . "\">Next >></A>"; 
} 

Echo "<br><br>" . $nav; 
?>
It does work but I want the replies just to appear for the correlating topic?, the next and previous page links appear above the replies?? and I want the topic to stay on every next or previous page?
Can anyone help? just this to do then can finally get the site up and running?
Thanks
Last edited by Benjamin on Mon Apr 26, 2010 6:45 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: forum pagination trouble?

Post by social_experiment »

In all 3 your 'search queries you don't have a 'WHERE' clause that might indicate what you are looking for.

Code: Select all

<?php $searchresult=mysql_query("SELECT id, username, title, detail FROM forum_topic WHERE id='$web_id'"); 

$searchresult=mysql_query("SELECT * FROM search ORDER BY id");

$searchresult=mysql_query("SELECT id, username, reply FROM forum_reply ORDER BY id LIMIT " . ($page-1)*$limit . ",$limit"); ?>
What is the purpose of id in your tables? Is it the primary key for the table(s)?

Assume the following: A topic has a topic_id, this is given to each reply so when you search the database, you know which reply goes with which topic. You need to narrow your search query to something similar :

Code: Select all

<?php $searchresult = mysql_query("SELECT id, username, reply FROM forum_reply WHERE topic_id = id_from_database_matching_the_topic_id ORDER BY id LIMIT value value"); ?>
Paste the table forum_reply's structure, then we can narrow it down some more.

If you want the links to appear above the replies, just move the code that creates the links above the code that echo's out the replies.

If you have 'topic' as a field in the table, call it (as you did with id, username and reply) equate it to a variable and echo it just before the reply (and after the links, depending on your preference).

Use the syntax tags to enclose your php code, it will help those who are reading your code ;)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: forum pagination trouble?

Post by Benjamin »

Use

Code: Select all

 tags when posting code in the forums.
Post Reply