Friday, June 1, 2012

Forum Scripts PHP


You may have written some thing to forum or may have seen forums discussions. Basically Forum contains topic,details and answers.Those thing should be stored in a mysql data base.create below two tables in your previous my_database.
Table QuestionTable Answer
CREATE TABLE IF NOT EXISTS `question` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `topic` varchar(255) NOT NULL DEFAULT '',
  `description` longtext NOT NULL,
  `name` varchar(65) NOT NULL DEFAULT '',
  `e_mail` varchar(65) NOT NULL,
  `datetime` varchar(25) NOT NULL DEFAULT '',
  `view` int(4) NOT NULL DEFAULT '0',
  `reply` int(4) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `answer` (
  `question_id` int(4) NOT NULL DEFAULT '0',
  `a_id` int(4) NOT NULL DEFAULT '0',
  `a_name` varchar(65) NOT NULL DEFAULT '',
  `a_email` varchar(65) NOT NULL DEFAULT '',
  `a_answer` longtext NOT NULL,
  `a_datetime` varchar(25) NOT NULL DEFAULT '',
  KEY `a_id` (`a_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Then you have to create the form from which you submit your question to the forum.layout of such form is as bellow.

Post Topic
Topic:-
Description:-
Name:-
E-mail:-

Codes of above above form is as follows named as "topic.html".

<form id="form1" name="form1" method="post" action="topic_enter.php">
<td>
<table bgcolor="#1b84a2" align="center">
<tr>
<th colspan="3" bgcolor="#dbeff5">Post Topic</th>
</tr>
<tr   bgcolor="#FFFFFF">
<th >Topic:-</th>
<td><input name="topic" type="text" id="topic" size="50" /></td>
</tr>
<tr   bgcolor="#FFFFFF">
<th>Discription:-</strong></th>
<td><textarea name="description" cols="50" rows="3" id="description"></textarea></td>
</tr>
<tr   bgcolor="#FFFFFF">
<th>Name:-</th>
<td><input name="name" type="text" id="name" size="50" /></td>
</tr>
<tr   bgcolor="#FFFFFF">
<th>E-mail:-</th>
<td><input name="e_mail" type="text" id="e_mail" size="50" /></td>
</tr>
<tr   bgcolor="#FFFFFF">
<td colspan=2 align="right" bgcolor="#dbeff5"><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>

Next create topic_enter.php 

<?php
$host="localhost";
$username="root";
$password="";
$db_name="my_database";
$tbl_name="question";


mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$topic=$_POST['topic'];
$description=$_POST['description'];
$name=$_POST['name'];
$e_mail=$_POST['e_mail'];

$datetime=date("d/m/y h:i:s"); //create date time

$sql="INSERT INTO $tbl_name(topic, description, name, e_mail, datetime)VALUES('$topic', '$description', '$name', '$e_mail', '$datetime')";
$result=mysql_query($sql);

if($result){
echo "Successful<BR>";
echo "<a href=main_display.php>View your topic added</a>";
}
else {
echo "ERROR";
}
mysql_close();
?>

Next create main_display.php as bellow


<?php
$host="localhost";
$username="root";
$password="";
$db_name="my_database";
$tbl_name="question";


mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";

$result=mysql_query($sql);
?>
<table width="1000"  align="center" cellpadding="3" cellspacing="1" bgcolor="#15475c">
<tr bgcolor="#ddeef5">
<td align="center" ><strong>Question Number</strong></td>
<td align="center" "><strong>Topic</strong></td>
<td  align="center" "><strong>Views</strong></td>
<td align="center" "><strong>Replies</strong></td>
<td " align="center" "><strong>Date and Time</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>
<tr bgcolor="#ddeef5">
<td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><a href="topic_view.php?id=<?php echo $rows['id']; ?>"><?php echo $rows['topic']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['reply']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['datetime']; ?></td>
</tr>

<?php

}
mysql_close();
?>
<tr bgcolor="#ddeef5">
<td colspan="5" align="right" ><a href="topic.html"><strong>Create New Topic</strong> </a></td>
</tr>
</table>

Next create topic_view.php as bellow.


<?php
$host="localhost";
$username="root";
$password="";
$db_name="my_database";
$tbl_name="question";

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['id'];

$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#c5e7f5">
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" bgcolor="#FFFFFF">
<tr bgcolor=#f1f9fa>
<td ><strong><?php echo $rows['topic']; ?></strong></td>
</tr>

<tr bgcolor=#f1f9fa>
<td><?php echo $rows['description']; ?></td>
</tr>

<tr bgcolor=#f1f9fa>
<td ><strong>By :</strong> <?php echo $rows['name']; ?> <strong>Email : </strong><?php echo $rows['e_mail'];?></td>
</tr>

<tr bgcolor=#f1f9fa>
<td ><strong>Date/time : </strong><?php echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<BR>
<?php
$tbl_name2="answer";

$sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id'";
$result2=mysql_query($sql2);

while($rows=mysql_fetch_array($result2)){
?>
<table  align="center"  bgcolor="#CCCCCC">
<tr>
<td><table  bgcolor="#FFFFFF">
<tr bgcolor=#f1f9fa>
<td ><strong>ID</strong></td>
<td>:</td>
<td><?php echo $rows['a_id']; ?></td>
</tr>
<tr bgcolor=#f1f9fa>
<td ><strong>Name</strong></td>
<td>:</td>
<td ><?php echo $rows['a_name']; ?></td>
</tr>
<tr bgcolor=#f1f9fa>
<td><strong>Email</strong></td>
<td>:</td>
<td><?php echo $rows['a_email']; ?></td>
</tr>
<tr bgcolor=#f1f9fa>
<td><strong>Answer</strong></td>
<td >:</td>
<td ><?php echo $rows['a_answer']; ?></td>
</tr>
<tr bgcolor=#f1f9fa>
<td ><strong>Date/Time</strong></td>
<td>:</td>
<td ><?php echo $rows['a_datetime']; ?></td>
</tr>
</table></td>
</tr>
</table><br>

<?php
}

$sql3="SELECT view FROM $tbl_name WHERE id='$id'";
$result3=mysql_query($sql3);

$rows=mysql_fetch_array($result3);
$view=$rows['view'];

// if have no counter value set counter = 1
if(empty($view)){
$view=1;
$sql4="INSERT INTO $tbl_name(view) VALUES('$view') WHERE id='$id'";
$result4=mysql_query($sql4);
}

// count more value
$addview=$view+1;
$sql5="update $tbl_name set view='$addview' WHERE id='$id'";
$result5=mysql_query($sql5);

mysql_close();
?>
<BR>
<table  bgcolor="#CCCCCC" align="center">
<tr>
<form name="form1" method="post" action="enter_answer.php">
<td>
<table bgcolor="#FFFFFF" align="center">
<tr>
<td width="18%"><strong>Name</strong></td>
<td width="3%">:</td>
<td width="79%"><input name="a_name" type="text" id="a_name" size="45"></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>:</td>
<td><input name="a_email" type="text" id="a_email" size="45"></td>
</tr>
<tr>
<td valign="top"><strong>Answer</strong></td>
<td valign="top">:</td>
<td><textarea name="a_answer" cols="45" rows="3" id="a_answer"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="id" type="hidden" value="<?php echo $id; ?>"></td>
<td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

Finally create enter_answer.php as bellow.


<?php
$host="localhost";
$username="root";
$password="";
$db_name="my_database";
$tbl_name="answer";


mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$id=$_POST['id'];


$sql="SELECT MAX(a_id) AS Maxa_id FROM $tbl_name WHERE question_id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);

// add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1
if ($rows) {
$Max_id = $rows['Maxa_id']+1;
}
else {
$Max_id = 1;
}


$a_name=$_POST['a_name'];
$a_email=$_POST['a_email'];
$a_answer=$_POST['a_answer'];

$datetime=date("d/m/y H:i:s");

// Insert answer
$sql2="INSERT INTO $tbl_name(question_id, a_id, a_name, a_email, a_answer, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_email', '$a_answer', '$datetime')";
$result2=mysql_query($sql2);

if($result2){
echo "Successful<BR>";
echo "<a href='topic_view.php?id=".$id."'>View your answer</a>";


$tbl_name2="question";
$sql3="UPDATE $tbl_name2 SET reply='$Max_id' WHERE id='$id'";
$result3=mysql_query($sql3);

}
else {
echo "ERROR";
}

mysql_close();
?>



19 comments: