Jump to content

Recommended Posts

Hi Guys,

 

iv made a fairly basic forum type script , it works great apart from 1 thing, whenever a user posts a reply to a topic that topic stays exactly where it was posted , it doesnt move to the top (like when you post a message in these forums)

 

code is:

 

<?php
     // grab the nzb id...///////////////////////////////////////////////////////////////
     $forum_id = $_GET['id'];
     
     ## deal with the reply #############################################################
     if ($_GET['action'] == "replied") {
     
     ## grab the data ###################################################################
     $the_replier = $_POST['repliedby'];
     $the_top_id = $_POST['topicid'];
     $the_body = CleanPosts($_POST['body'], 1);
     $the_fid = $_POST['fid'];
          
     ## error check #####################################################################
     if(empty($the_body)) {
     
     stderr("Error", "You never entered anything in the reply field.");
     include("includes/footer.php");
     exit;
     
     }
          
     ####################################################################################
     # insertion
     ####################################################################################
     
     $q2 = "INSERT INTO `forum_posts` (`id`,`topic_id`,`user_id`,`date`,`post_body`) VALUES ('','$the_top_id','$the_replier',now(),'$the_body')";
     $r2 = mysql_query($q2) or die (mysql_error());
     
     ## update the post count ###########################################################
     $q_sections2 = "UPDATE `forum_sections` SET `post_count`=post_count+1 WHERE `id`='$the_fid'";
     $r_sections2 = mysql_query($q_sections2) or die (mysql_error());
     
     $q_sections3 = "UPDATE `forum_topics` SET `replies`=replies+1 WHERE `id`='$the_top_id'";
     $r_sections3 = mysql_query($q_sections3) or die (mysql_error());         
     
     ####################################################################################
     # insertion
     ####################################################################################
     
     ## update this topic as the last post read #########################################
     mysql_query("UPDATE `forum_topics` SET `last_post`='$the_top_id' WHERE `id`='$the_top_id'");
   
     stderr("Success", "Stand by....");
     echo "<meta http-equiv=\"refresh\" content=\"0;URL=viewforum.php?action=viewtopic&topicid=$the_top_id\">";
     include("includes/footer.php");
     exit;
     
     }
     
     ## reply topic #####################################################################
     if ($_GET['action'] == "reply") {
     
     ## grab the get data ###############################################################
     $reply_topic_id = $_GET['topicid'];
     
     ## GET TOPIC NAME ##################################################################
     $q4 = "SELECT * FROM `forum_topics` WHERE `id`='$reply_topic_id'";
     $r4 = mysql_query($q4) or die (mysql_error());
     $r = mysql_fetch_array($r4) or die (mysql_error());
     $subj = $r['subject'];
     $fid = $r['forum_id'];
     
     ## get the logged in users details #################################################
     $q_uid = "SELECT `id` FROM `membership` WHERE `username`='$member'";
     $r_uid = mysql_query($q_uid) or die (mysql_error());
     $liuid = mysql_fetch_array($r_uid);
     $u_id = $liuid['id'];
     
     ## make the reply box ##############################################################
     echo '<br />';
     echo '<b>Your about to reply to the topic: <a href="viewforum.php?action=viewtopic&topicid='.$reply_topic_id.'">'.$subj.'</a></b>';
     echo '<br /><br />
           <table width="80%" border="1" cellspacing="0" cellpadding="10"><tr><td align="center">
           <form method="POST" action="?action=replied">
           <input type="hidden" name="forumid" value='.$forumid.'>
           <br />
           <table class="forum_border" border="1" cellspacing="0" cellpadding="5">
           <tr>
           <td class="forum_border" align="right" valign="top"><b>Body</b></td><td class="forum_border" align=left style=\'padding: 0px\'><textarea name="body" cols="100" rows="20" style=\'border: 0px\'></textarea></td>
           </tr>
           <tr>
           <td class="forum_border" colspan="2" align="center"><input type=submit class="btn" value=\'Submit Reply\'>                                                               
                                                               <input type="hidden" name="topicid" value="'.$reply_topic_id.'">
                                                               <input type="hidden" name="fid" value="'.$fid.'">
                                                               <input type="hidden" name="repliedby" value="'.$u_id.'">
           </td>
           </tr>
           </td>
           </tr>
           </table>
           </form>    
           </table><br />';
           
     ## update last poster ##############################################################
           
     include("includes/footer.php");
     exit;
     
     }
     
     ## view topic ######################################################################
     if ($_GET['action'] == "viewtopic") {
     
     ## grab the post data ##############################################################
     $the_topic_id = $_GET['topicid'];
     
     ## ERROR ###########################################################################
     if(empty($the_topic_id)) {
     
     stderr("Error", "Bad ID, or the topic no longer exhists.");
     include("includes/footer.php");
     exit;
     
     }
     
     ## also update the views ###########################################################
     $query_update_views = "UPDATE `forum_topics` SET `views`=views+1 WHERE `id`='$the_topic_id'";
     $result_update_views = mysql_query($query_update_views) or die (mysql_error());
     
     ## get and display the topic and relevant data #####################################
     $query_db1 = "SELECT * FROM `forum_topics` WHERE `id`='$the_topic_id'";
     $result_db1 = mysql_query($query_db1) or die (mysql_error());
     $row = mysql_fetch_array($result_db1) or die (mysql_error());
     $subject = $row['subject'];
     $user_id = $row['user_id'];
     $forum_id = $row['forum_id'];
     
     ## get the posters details #########################################################
     $query_poster = "SELECT * FROM `membership` WHERE `id`='$user_id'";
     $result_poster = mysql_query($query_poster) or die (mysql_error()); 
     $rows = mysql_fetch_array($result_poster) or die (mysql_error()); 
     $poster_id = $rows['id'];
     $poster_name = $rows['username'];
     $avatar = $rows['avatar'];
     $user_class = $rows['user_class'];
     
     ## more info date etc ##############################################################
     $query_top = "SELECT * FROM `forum_posts` WHERE `topic_id`='$the_topic_id'";
     $result_top = mysql_query($query_top) or die (mysql_error());
     $top = mysql_fetch_array($result_top);
     $post_topic_id = $top['id'];
     $post_body = $top['post_body'];
     $post_date = $top['date'];
     
     ## 1 more ##########################################################################
     $q = "SELECT * FROM `forum_sections` WHERE `id`='$forum_id'";
     $r = mysql_query($q) or die (mysql_error());
     $row3 = mysql_fetch_array($r) or die (mysql_error()); 
     $f_name = $row3['forum_name'];
     
     //$query_again_top = "SELECT * FROM `forum_posts` WHERE `topic_id`='$post_topic_id'";
     //$result_again_top = mysql_query($query_again_top) or die (mysql_error());
     //$t = mysql_fetch_array($result_again_top) or die (mysql_error()); 
     //$t_date = $t['date'];
     
     echo "<br /><b><a href=\"viewforum.php?id=$forum_id\">$f_name</a> >>> $subject</b><br /><br />";
          
     ####################################################################################
     $query_replies = "SELECT * FROM `forum_posts` WHERE `topic_id`='$the_topic_id'";
     $result_replies = mysql_query($query_replies) or die (mysql_error());
     
     while($row_replies = mysql_fetch_array($result_replies)) {
     
     ## VARS
     
     $fp_id = $row_replies['id'];
     $ft_uid = $row_replies['user_id'];
     $ft_date = $row_replies['date'];
     $post_body = $row_replies['post_body'];
     
     $q3 = "SELECT * FROM `membership` WHERE `id`='$ft_uid'";
     $r3 = mysql_query($q3) or die (mysql_error());
     $qr = mysql_fetch_array($r3) or die (mysql_error());
     $replier_id = $qr['id'];
     $replier_name = $qr['username'];
     $replier_user_class = $qr['user_class'];
     $avatar = $qr['avatar'];
     
     ####################################################################################
     echo '<table width="750" border="20" bordercolor="#8f0002" cellpadding="5" cellspacing="0"> 
           <tr> 
           <td bgcolor="#0055A4" class="details" valign="top"><table width="100%" border="1" cellpadding="5" cellspacing="0"> 
           </tr> 
           <tr>
           <td bgcolor="#EEEEEE" class="forum_border" class="forum_border" colspan="2" align="left" class="details">By <a href="userdetails.php?id='.$replier_id.'">'.$replier_name.'</a> (<b>'.$replier_user_class.'</b>)<br />Posted on '.$ft_date.'</td><td bgcolor="#EEEEEE" class="forum_border" align="right">[<a href="">EDIT</a>]-[<a href="">DELETE</a>]</td>
           </tr>
           <tr>
           <td class="forum_border" colspan="2" width="30%" align="center" valign="top">';
           if(empty($avatar)) {
           
           echo "<img src=\"images/default_avatar.gif\" />";  
           
           } else {
           
           echo "<img src=\"avatars/$avatar\" />";
           
           } 
     echo '</td><td class="forum_border" width="70%" align="left" valign="top">'.$post_body.'</td>
           </tr>
           </table></table><br />';
                
     ####################################################################################
     }
     
     ## reply button ####################################################################
     echo '<form method="get" action="?">
           <input type="hidden" name="action" value="reply">
           <input type="hidden" name="topicid" value='.$the_topic_id.'>
           <input type="submit" value=\'Add Reply\' class=btn>
           </form>';
           
     ## update this topic as the last post read #########################################
     mysql_query("UPDATE `forum_topics` SET `last_post`='$the_topic_id' WHERE `id`='$the_topic_id'");
     
     ####################################################################################
     # we really need the latest post id so while loop it
     ####################################################################################
     $query_the_post_id = "SELECT `id` FROM `forum_posts`";
     $result_the_post_id = mysql_query($query_the_post_id) or die (mysql_error());
     while($rowid = mysql_fetch_array($result_the_post_id)) {
     
     $new_id = $rowid['id'];
     
     }
     
     ## last read post ##################################################################
     mysql_query("INSERT INTO `forum_last_post` (`id`,`user_id`,`topic_id`,`last_read_post`) values ('','$replier_id','$the_topic_id','$new_id')");

     include("includes/footer.php");
     exit; 
     
     }
     
     if ($_GET['action'] == "post") {
     
     $subject = CleanPosts($_POST['subject'], 1);
     $body = CleanPosts($_POST['body'], 1);
     $poster = $_POST['poster'];
     $for_id = $_POST['forums_id'];
     $topical_id = $_POST['topic_id'];
     
     ## error check #####################################################################
     if(empty($subject)) {
     
     stderr("Error", "You never entered a subject.");
     include("includes/footer.php");
     exit;
     
     }
     
     if(empty($body)) {
     
     stderr("Error", "You never entered anything in the message body.");
     include("includes/footer.php");
     exit;
     
     }
     
     ## cap the subject length ##########################################################
     if(strlen($subject) > 50) {
     
     stderr("Error", "Sorry, the subject can't be any longer that 50 characters.");
     include("includes/footer.php");
     exit;     
     
     }
     
     ####################################################################################
     # insertions
     ####################################################################################
     
     ## more insertion ##################################################################
     
     $query_insert = "INSERT INTO `forum_topics` (`id`,`user_id`,`subject`,`forum_id`) VALUES ('','$poster','$subject','$for_id')";
     $result_insert = mysql_query($query_insert) or die (mysql_error());
     
     ## need the topic id  ############################################################
     $query = "SELECT `id` FROM `forum_topics`";
     $result = mysql_query($query) or die (mysql_error());
     
     while($row = mysql_fetch_array($result)) {
     
     $id2 = $row['id']; 
     
     }
     
     $query_insert_2 = "INSERT INTO `forum_posts` (`id`,`topic_id`,`user_id`,`date`,`post_body`) VALUES ('','$id2','$poster',now(),'$body')";
     $result_insert_2 = mysql_query($query_insert_2) or die (mysql_error());
     
     $q_sections = "UPDATE `forum_sections` SET `topic_count`=topic_count+1 WHERE `id`='$for_id'";
     $r_sections = mysql_query($q_sections) or die (mysql_error());
     
     ## all good then do the insertion ##################################################
     
     ####################################################################################
     # insertions
     ####################################################################################
     stderr("Success", "Stand by....");
     echo "<meta http-equiv=\"refresh\" content=\"0;URL=viewforum.php?id=$for_id\">";
     include("includes/footer.php");
     exit;
     
     }
     
     ## the gets ########################################################################
     if ($_GET['action'] == "newtopic") {
     
     ## forum id ########################################################################
     $forumid = $_GET['forumid'];
     $topicid = $_GET['topicid'];
     
     ## ERROR ###########################################################################
     if(empty($forumid)) {
     
     stderr("Error", "Bad ID ");
     include("includes/footer.php");
     exit;
     
     }
     
     ## get the forum name ##############################################################
     get_forum_name($forumid);
     
     ## get the posters id ##############################################################
     $query_poster = "SELECT `id` FROM `membership` WHERE `username`='$member'";
     $result_poster = mysql_query($query_poster) or die (mysql_error());
     $user_id = mysql_fetch_array($result_poster) or die (mysql_error());
     $poster_id = $user_id['id'];
     
     ## make the new topic box ##########################################################
     echo '<br />';
     echo '<b>Adding a new topic in the forum: <a href="viewforum.php?id='.$forumid.'">'.get_forum_name($forumid).'</a></b>';
     echo '<br /><br />
           <table width="80%" border="1" cellspacing="0" cellpadding="10"><tr><td align="center">
           <form method="POST" action="?action=post">
           <input type="hidden" name="forumid" value='.$forumid.'>
           <br />
           <table class="forum_border" border=1 cellspacing=0 cellpadding=5>
           <tr>
           <td class="forum_border"><b>Subject</b></td><td class="forum_border" align=left style=\'padding: 0px\'><input type=text size="100" maxlength="60" name="subject" style=\'border: 0px; height: 19px\'></td>
           </tr>
           <tr>
           <td class="forum_border" align="right" valign="top"><b>Body</b></td><td class="forum_border" align=left style=\'padding: 0px\'><textarea name="body" cols="100" rows="20" style=\'border: 0px\'></textarea></td>
           </tr>
           <tr>
           <td class="forum_border" colspan="2" align="center"><input type="submit" class="btn" value=\'Submit Topic\'>
                                                               <input type="hidden" name="poster" value="'.$poster_id.'">
                                                               <input type="hidden" name="forums_id" value="'.$forumid.'">
           </td>
           </tr>
           </td>
           </tr>
           </table>
           </form>    
           </table><br />';
     
     include("includes/footer.php");
     exit;
     
     }
     
     ## forum name ######################################################################
     $forum_query = "SELECT * FROM `forum_sections` WHERE `id`='$forum_id'";
     $forum_result = mysql_query($forum_query) or die (mysql_error());
     $row = mysql_fetch_array($forum_result) or die (mysql_error());
     
     ## vars ############################################################################
     $forum_name = $row['forum_name'];
     
     ## echo ############################################################################
     echo "<br /><h4>$forum_name</h4>";
     
        ## Pagination start #############################################################
        echo "<center>";
	// If current page number, use it 
	// if not, set one! 

	if(!isset($_GET['page'])){ 
		$page = 1; 
	} else { 
		$page = $_GET['page']; 
	} 

	// Define the number of results per page 
	$max_results = 25; 		

	// Figure out the limit for the query based 
	// on the current page number. 
	$from = (($page * $max_results) - $max_results);  

	// Perform MySQL query on only the current page number's results 

	$sql = mysql_query("SELECT * FROM `forum_topics` WHERE `forum_id`='$forum_id' ORDER BY `id` DESC LIMIT $from, $max_results"); 

        ## Pagination start #############################################################
     
     ####################################################################################
     echo '<table border="1" cellspacing="0" cellpadding="5">
           <tr>
           <td colspan="2" class="colhead" align="left">Topic</td><td class="colhead">Replies</td><td class="colhead">Views</td><td class="colhead" align="center">Author</td><td class="colhead" align="center">Date Posted</td>
           </tr>';
                    
     while($row = mysql_fetch_array($sql)) {
     
     ## vars 
     $for_subj = $row['subject'];
     $for_ti = $row['id'];
     $for_views = $row['views'];
     $for_user_id = $row['user_id'];
     
     ## post user details ###############################################################
     $q6 = "SELECT * FROM `membership` WHERE `id`='$for_user_id'";
     $r6 = mysql_query($q6) or die (mysql_error());
     $r2 = mysql_fetch_array($r6) or die (mysql_error());
     $postid = $r2['id'];
     $postname = $r2['username'];
     
     ## post count ######################################################################
     $q5 = "SELECT * FROM `forum_posts` WHERE `topic_id`='$for_ti'";
     $r5 = mysql_query($q5) or die (mysql_error());
     $date = mysql_fetch_array($r5);
     $postdate = $date['date'];
     $postcount = mysql_num_rows($r5);
     
     ## grab the user_id ################################################################
     $q8 = "SELECT * FROM `membership` WHERE `id`='$need_user_id'";
     $r8 = mysql_query($q8) or die (mysql_error());
     $r4 = mysql_fetch_array($r8);
     $last_post_id = $r4['id'];
     $last_post_name = $r4['username'];
     
     ## replies #########################################################################
     $q12 = "SELECT `replies` FROM `forum_topics` WHERE `id`='$for_ti'";
     $r12 = mysql_query($q12) or die (mysql_error());
     $rep_count = mysql_fetch_array($r12);
     $replies = $rep_count['replies'];
     
     echo '<tr><td class="forum_border" align="center"><img src=images/forum_1337.gif></td><td align="left" class="forum_border"><a href=?action=viewtopic&topicid='.$for_ti.'><b>'.$for_subj.'</b></a></td><td align="center" class="forum_border">'.$replies.'</td><td align="center" class="forum_border">'.$for_views.'</td><td align="center" class="forum_border"><a href="userdetails.php?id='.$postid.'"><b>'.$postname.'</b></a></td><td align="center" class="forum_border"><font size="1">'.$postdate.'</font></td></tr>';
     
     }     
     echo '</table>';
     ####################################################################################
     echo '<br />
           <table class="details" border="0" cellspacing="0" cellpadding="5">
           <tr>
           <td><img src=images/1337(b).gif style=\'margin-right: 5px\'></td><td>New Posts</td><td><img src=images/locked.gif style=\'margin-left: 10px; margin-right: 5px\'></td><td>Locked Topic</td>
           </tr>
           </table><br />';
     
         ## Pagination end ###############################################################

	// Figure out the total number of results in DB: 
	$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM `forum_topics` WHERE `forum_id`='$forum_id'"),0); 

	// Figure out the total number of pages. Always round up using ceil() 
	$total_pages = ceil($total_results / $max_results); 

	// Build Previous Link 
	if($page > 1){ 
		$prev = ($page - 1); 
		echo " <a href=\"".$_SERVER['PHP_SELF']."?id=$forum_id&page=$prev\"><<< </a> "; 
	} 

	for($i = 1; $i <= $total_pages; $i++){ 
		if(($page) == $i){ 
			echo "[<b>$i</b>] "; 
			} else { 
				echo "<a href=\"".$_SERVER['PHP_SELF']."?id=$forum_id&page=$i\">$i</a> "; 
		} 
	} 

	// Build Next Link 
	if($page < $total_pages){ 
		$next = ($page + 1); 
		echo "<a href=\"".$_SERVER['PHP_SELF']."?id=$forum_id&page=$next\"> >>></a>"; 
	} 
	echo "<br /><br />"; 

        ## Pagination end ###############################################################
     ## buttons #########################################################################
     echo '<form method="get" action=?><input type="hidden" name="action" value="newtopic"><input type="hidden" name="forumid" value="'.$forum_id.'"><input type=submit value=\'New Topic\' class=btn style=\'margin-left: 10px\'></form>';
?>

 

the VERY bottom part of the code echoes out the forum tables etc im not sure what i should be doing to make the posted in topic jump to the top, any ideas would be great

 

cheers

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/66233-moving-forum-posts-to-the-top/
Share on other sites

Your topic is dramatically long, so im talking about the concept. U could use the posted date for sorting and that would work greatly, but as long as the word simple is involved u can sort topics by id. U know that the last id is the last created row. So:

 

$query = "SELECT topics FROM category ORDER BY id DESC";

 

"ORDER BY id" sorts rows by id and "DESC" makes the sort descending, so the last is on top.

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.