Page 1 of 1

Message board problem

Posted: Tue Mar 06, 2012 7:56 pm
by Php Beginner
Hi there,

I have written codes for a message board to be added into my website by using php and mysql. I have some problem here, the 'Numbers of Reply', 'Last Replied Time' and 'Last Replied by who' could not being updated when there are replies.

Here's the code (elec_forum.php):

Code: Select all

<p ><h3 align="center">Department of Electronic Engineering</h3> 
<?php 

include "connect.php"; //mysql db connection here 

print "<link rel='stylesheet' href='style.css' type='text/css'>"; 

print "<A href='elec_post.php'>New Topic</a><br>"; 

print "<table class='maintable'>"; 

print "<tr class='headline'><td width=50%>Discussion Topic</td><td width=20%>Author</td><td>Replies</td><td>Last replied time</td></tr>"; 

$getthreads="SELECT * from elec_forum where parentid='0' order by lastrepliedto DESC"; 

$getthreads2=mysql_query($getthreads) or die("Could not get threads"); 

while($getthreads3=mysql_fetch_array($getthreads2)) 

{ 

  $getthreads3['title']=strip_tags($getthreads3['title']); 

  $getthreads3['author']=strip_tags($getthreads3['author']); 

  print "<tr class='mainrow'><td><a href='elec_message.php?id=$getthreads3[postid]'>$getthreads3[title]</a></td><td>$getthreads3[author]</td><td>$getthreads3[numreplies]</td><td>$getthreads3[showtime]<br>Last post by <b>$getthreads3[lastposter]</b></td></tr>"; 

} 

print "</table>"; 



?>
Here's the reply code (elec_reply.php):

Code: Select all

<?php 

include "connect.php"; //connection string 

print "<link rel='stylesheet' href='style.css' type='text/css'>"; 

print "<table class='maintables'>"; 

print "<tr class='headline'><td>Reply</td></tr>"; 

print "<tr class='maintables'><td>"; 

if(isset($_POST['submit'])) 

{ 

   $name=$_POST['name']; 

   $yourpost=$_POST['yourpost']; 

   $subject=(isset($_POST['subject']))?$_POST['subject']:null;; 

   $id=$_POST['id']; 

   if(strlen($name)<1) 

   { 

      print "You did not type in a name."; //no name entered 

   } 

   else if(strlen($yourpost)<1) 

   { 

      print "You did not type in a post."; //no post entered 

   } 

   else 

   { 
      date_default_timezone_set('Asia/Brunei'); 
      $thedate=date("U"); //get unix timestamp 

      $displaytime=date("F j, Y, g:i a"); 

      //we now strip HTML injections 

      $subject=strip_tags($subject); 

      $name=strip_tags($name); 

      $yourpost=strip_tags($yourpost); 

      $insertpost="INSERT INTO elec_forum(author,title,post,showtime,realtime,lastposter,parentid) values('$name','$subject','$yourpost','$displaytime','$thedate','$name','$id')"; 

      mysql_query($insertpost) or die("Could not insert post"); //insert post 


      print "Message updated, go back to <A href='elec_message.php?id=$id'>Message</a>."; 

   } 



} 

else 

{ 

   $id=$_GET['id']; 

   print "<form action='elec_reply.php' method='post'>"; 

   print "<input type='hidden' name='id' value='$id'>"; 

   print "Your name:<br>"; 

   print "<input type='text' name='name' size='20'><br>"; 

   print "Your message:<br>"; 

   print "<textarea name='yourpost' rows='5' cols='40'></textarea><br>"; 

   print "<input type='submit' name='submit' value='submit'></form>"; 



} 

print "</td></tr></table>"; 

?>
I am not sure where's the problem occurred. Please advice.

Re: Message board problem

Posted: Wed Mar 07, 2012 5:41 am
by social_experiment
When you want the total amount of replies you need to count replies that have something unique to a specific message; say a id value unique.
Currently you are only adding values to the database and selecting a field called 'numreplies'. Do you have any way of calculating the number replies?

Re: Message board problem

Posted: Wed Mar 07, 2012 6:58 am
by Php Beginner
Hi, I have solved my problem regarding the number of replies. Now the 'last replied time' in the message page work perfectly but in the forum index page, still showing the older time where the 1st message posted.

Here's the code for the message displayed page

Code: Select all

<?php 

include "connect.php"; //mysql db connection here

$id=$_GET['id'];

print "<link rel='stylesheet' href='style.css' type='text/css'>";

print "<A href='elec_forum.php'>Back to main forum</a>-<A href='elec_post.php'>New Topic</a>-<A href='elec_reply.php?id=$id'>Reply<br>";

print "<table class='maintable'>";

print "<tr class='headline'><td width=20%>Author</td><td width=80%>Post</td></tr>";

$gettopic="SELECT * from elec_forum where postid='$id'";

$gettopic2=mysql_query($gettopic) or die("Could not get topic");

$gettopic3=mysql_fetch_array($gettopic2);

print "<tr class='mainrow'><td valign='top'>$gettopic3[author]</td><td valign='top'>Last replied to at $gettopic3[showtime]<br><hr>";

$message=strip_tags($gettopic3['post']);

$message=nl2br($message);

print "$message<hr><br>";

print "</td></tr>";

$getreplies="Select * from elec_forum where parentid='$id' order by postid DESC"; //getting replies

$getreplies2=mysql_query($getreplies) or die("Could not get replies");

while($getreplies3=mysql_fetch_array($getreplies2))

{

   print "<tr class='mainrow'><td valign='top'>$getreplies3[author]</td><td valign='top'>Last replied to at $getreplies3[showtime]<br><hr>";

   $message=strip_tags($getreplies3['post']);

   $message=nl2br($message);

   print "$message<hr><br>";

   print "</td></tr>";

}

print "</table>";



?>  
I could not get to know when is the actual time for the last post until I click into the topic and check on each message. Just like any other forum board out there, we can know who was the last poster and when is the time of that last post without need to access into the topic.