hi,
I have tried out the logic using two files.
I have added some more fields like author, date and subject.I have put the code of those files below.They would allow u to post a topic and also reply to a topic. Just change the user name and password for mysql_connect.
Check them out.
Save this file as posts.php:
<?php
$connection=mysql_connect("localhost","root","rootmysql");
$db=mysql_select_db("test",$connection);
//If submitted a new topic
if ($insert){
//If contains all neccessary data
if ($username && $content && $subject){
$sql="insert into posts
(postid,parentpostid,username,content,subject,date)values ('','$parentpostid','$username',
'$content','$subject',NOW())";
mysql_query($sql);
header("Location:posts.php");
exit;
}
//If data missing
else{
header("Location:newposts.php?value=true&username=$username&content=$content&subject=$subject");
exit;
}
}?>
<head><title> Forum </title></head>
<body><form method='post'><br>
<p align='center'>
<font face='arial' size='4'>Forum</font></p>
<br><p align='center'>
<font face='arial' size='4'>
<a href='newposts.php'>New topic</a></font>
</p><br>
<table border='1' width='100%' cellspacing='0' cellpadding='0'>
<tr bgcolor='#A3A3A3'>
<td width='33%' align='left'>
<font face='arial' size='2'>Topic</font></td>
<td width='33%' align='left'>
<font face='arial' size='2'>Author</font>
</td><td width='33%' align='left'>
<font face='arial' size='2'>Date</font></td>
</tr><tr><td width='100%' colspan='3'>
<table border='0' width='100%' cellspacing='0' cellpadding='0'>
<?php
$sql="select * from posts where parentpostid='0' order by date";
$result=mysql_query($sql);
$i=0;
while ($row=mysql_fetch_array($result))
{
$i=++$i;
if ($i%2==0){
$bgcolor="#FFCCFF";
}
else{
$bgcolor="#99CCFF";
}
echo "
<tr bgcolor='$bgcolor'>
<td width='33%' align='left'>
<a href='newposts.php?postid=$row[postid]'>$row[subject]</a>
</td>
<td width='33%' align='left'>$row[username]</td>
<td width='33%' align='left'>$row[date]</td>
</tr>";
/ Call to the function to get the
replies to a particular message /
$flag=replies($row[postid],$i);
/ Call to the function to get all the
replies to a particular message /
while ($flag!="null"){
$i=++$i;
$flag=replies($flag,$i);
}
}
?>
</table>
</td>
</tr>
</table>
</form>
</body>
<?php
function replies($postid,$i)
{
$sql1="select * from posts where
parentpostid='$postid' order by date";
$result1=mysql_query($sql1);
$record="null";
while ($row=mysql_fetch_array($result1))
{
$i=++$i;
if ($i%2==0)
{
$bgcolor="#FFCCFF";
}
else
{
$bgcolor="#99CCFF";
}
echo "
<tr bgcolor='$bgcolor'>
<td width='33%' align='left'>
<a href='newposts.php?postid=$row[postid]'>$row[subject]</a></td>
<td width='33%' align='left'>$row[username]</td>
<td width='33%' align='left'>$row[date]</td>
</tr>";
$record=$row[postid];
}
return $record;
}
?>
Save this file as newposts.php:
<?php
$connection=mysql_connect("localhost","root","rootmysql");
$db=mysql_select_db("test",$connection);
echo "
<head><title> Posts </title></head>
<body>
<form action='posts.php?insert=true' method='post'>
<font face='arial' size='4'>
<a href='posts.php'>Topics</a></font>";
//If reply to a old topic
if ($postid){
$sql="select * from posts where postid='$postid'";
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result)){
echo "
<center>
<table border='0' width='79%' cellspacing='0' cellpadding='0'>
<tr>
<td width='100%' bgcolor='#C0C0C0'>
<font face='arial' size='2'><b>
Subject : </b>$row[subject]</font>
</td></tr>
<tr>
<td width='100%' bgcolor='#99CCFF'>
<font face='arial' size='2'><b>
Author : </b>$row[username]</font>
<br><font face='arial' size='2'><b>
Date : </b>$row[date]</font><br>
<br><font face='arial' size='2'>$row[content]</font></td></tr></table></center>";
}
echo "<p align='center'><font face='arial' size='4'>Reply to the topic</font></p>";
}
//If a new topic
else{
echo "<p align='center'><font face='arial' size='4'>Post a topic</font></p>";
}
//If null values have been submitted
if ($value){
echo "<p align='center'>
<font face='arial' size='4' color='red'>Null Values Posted</font></p>";
}
echo "
<table border='0' align='center' cellspacing='0' cellpadding='0'>
<tr><td align='right'>User name :
</td><td align='left'>
<input type='text' name='username' size='25' value='$username'>
</td></tr>
<tr><td colspan='2'> </td></tr>
<tr><td align='right'>Subject :
</td><td align='left'>
<input type='text' name='subject' size='25' value='$subject'></td></tr>
<tr><td colspan='2'> </td></tr>
<tr><td align='right'>Content :
</td><td align='left'>
<textarea rows='10' cols='25' name='content'>$content</textarea></td></tr>
<tr><td colspan='2'> </td></tr></table>
<center><input type='submit' name='btnsubmit' value='Submit'>
<input type='reset' name='btnreset' value='Reset'></center>
<input type='hidden' name='parentpostid' size='25' value='$postid'>
</form>
</body>
";
?>
with regards,
preeth