Seich
01-05-2008, 03:52 AM
please remember this post was made by Seich for the Photoshopstar forums don't post it anywhere else :)because i don't want it to spread exaggeratedly.:p
Hi everyone! as my first useful post i wanted to post something you can use in the php section so i will show everyone how to make a forum using php, mysql and a little of html so lets start.:p
ok our forum will be divided into Six parts:
index.php
create_topic.php
add_answer.php
add_topic.php
view_topic.php
db.php
all code is explained further with comments so be sure to read them.:o
Lets start, first we create the necessary Db* entries i will be using the following scheme so please feel free to use the tables as you which.
First Table forum_answer this is where all answers to posts will be stored.
you can use a program to execute this query or phpmyadmin what ever is fine with you.
CREATE TABLE `forum_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`)
) TYPE=MyISAM;
second table is forum_questions where topic info will be stored.
CREATE TABLE `forum_question` (
`id` int(4) NOT NULL auto_increment,
`topic` varchar(255) NOT NULL default '',
`detail` longtext NOT NULL,
`name` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
`datetime` varchar(25) NOT NULL default '',
`view` int(4) NOT NULL default '0',
`reply` int(4) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
ok now lets work with the php
the first file we are going to create is a standard dbconnector which will connect the forum to the db containing your forum's info.
make sure to replace the info with yours.
db.php
<?php
$host="localhost"; // Host name
$username="db_user"; // Mysql username
$password="db_password"; // Mysql password
$db_name="db_name"; // Database name
<?
// Connect to server and select databse. make sure not to edit under this line
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
?>
ok no that we are able to connect to the db we need a forum.
ok we start by making a index.php file you can name this anything you want.
index.php
<?php/*we start by adding a link to our db.php file.*/ require_once('db.php')?>
<?php
$tbl_name="forum_question"; // we tell the forum what table to connect to.
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending
$result=mysql_query($sql);
?>
<!--time for some html we write and configure the look and feel of our forum in this case is just a simple table with some text-->
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></a><BR></td><!-- we echo db info so it is displayed on the forum-->
<td align="center" bgcolor="#FFFFFF"><? echo $rows['view']; ?></td><!--we continue echoing information from the db-->
<td align="center" bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td><!--we continue echoing information from the db-->
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td><!--we continue echoing information from the db-->
</tr>
<?php
// Exit looping and close connection
}
mysql_close();
?>
<tr>
<td colspan="-5" align="right" bgcolor="#E6E6E6"></td>
<td colspan="5" align="right" bgcolor="#E6E6E6"><a href="create_topic.php">New topic</a><!--we add a link to our create_topic.php file we are going to do later on-->
</td>
</tr>
</table>
now we need a create_topic.php to create new topics
this is a simple form with a post method called add_topic.php
create_topic.php
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="add_topic.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="-5" align="right" bgcolor="#E6E6E6"></td>
<td colspan="3" bgcolor="#E6E6E6"><strong>Create New Topic</strong> </td>
</tr>
<tr>
<td width="14%"><strong>Topic</strong></td>
<td width="2%">:</td>
<td width="84%"><input name="topic" type="text" id="topic" size="50" /></td>
</tr>
<tr>
<td valign="top"><strong>Detail</strong></td>
<td valign="top">:</td>
<td><textarea name="detail" cols="50" rows="3" id="detail"></textarea></td>
</tr>
<tr>
<td><strong>Name</strong></td>
<td>:</td>
<td><input name="name" type="text" id="name" size="50" /></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>:</td>
<td><input name="email" type="text" id="email" size="50" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
now we need the post method add_topic.php so lets create it.
this method is basically a file that will put the data in the db
add_topic
<?php/*we include db.php*/ require_once('db.php')?>
<?php
$tbl_name="forum_question"; // Table name
// get data that sent from form at create_topic.php
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name'];
$email=$_POST['email'];
$datetime=date("d/m/y h:i:s"); //create date time
$sql="INSERT INTO $tbl_name(topic, detail, name, email, datetime)VALUES('$topic', '$detail', '$name', '$email', '$datetime')";
$result=mysql_query($sql);
if($result){
echo "Successful<BR>";
echo "<a href=index.php>View your topic</a>";
}
else {
echo "ERROR";
}
mysql_close();
?>
Hi everyone! as my first useful post i wanted to post something you can use in the php section so i will show everyone how to make a forum using php, mysql and a little of html so lets start.:p
ok our forum will be divided into Six parts:
index.php
create_topic.php
add_answer.php
add_topic.php
view_topic.php
db.php
all code is explained further with comments so be sure to read them.:o
Lets start, first we create the necessary Db* entries i will be using the following scheme so please feel free to use the tables as you which.
First Table forum_answer this is where all answers to posts will be stored.
you can use a program to execute this query or phpmyadmin what ever is fine with you.
CREATE TABLE `forum_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`)
) TYPE=MyISAM;
second table is forum_questions where topic info will be stored.
CREATE TABLE `forum_question` (
`id` int(4) NOT NULL auto_increment,
`topic` varchar(255) NOT NULL default '',
`detail` longtext NOT NULL,
`name` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
`datetime` varchar(25) NOT NULL default '',
`view` int(4) NOT NULL default '0',
`reply` int(4) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
ok now lets work with the php
the first file we are going to create is a standard dbconnector which will connect the forum to the db containing your forum's info.
make sure to replace the info with yours.
db.php
<?php
$host="localhost"; // Host name
$username="db_user"; // Mysql username
$password="db_password"; // Mysql password
$db_name="db_name"; // Database name
<?
// Connect to server and select databse. make sure not to edit under this line
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
?>
ok no that we are able to connect to the db we need a forum.
ok we start by making a index.php file you can name this anything you want.
index.php
<?php/*we start by adding a link to our db.php file.*/ require_once('db.php')?>
<?php
$tbl_name="forum_question"; // we tell the forum what table to connect to.
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending
$result=mysql_query($sql);
?>
<!--time for some html we write and configure the look and feel of our forum in this case is just a simple table with some text-->
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></a><BR></td><!-- we echo db info so it is displayed on the forum-->
<td align="center" bgcolor="#FFFFFF"><? echo $rows['view']; ?></td><!--we continue echoing information from the db-->
<td align="center" bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td><!--we continue echoing information from the db-->
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td><!--we continue echoing information from the db-->
</tr>
<?php
// Exit looping and close connection
}
mysql_close();
?>
<tr>
<td colspan="-5" align="right" bgcolor="#E6E6E6"></td>
<td colspan="5" align="right" bgcolor="#E6E6E6"><a href="create_topic.php">New topic</a><!--we add a link to our create_topic.php file we are going to do later on-->
</td>
</tr>
</table>
now we need a create_topic.php to create new topics
this is a simple form with a post method called add_topic.php
create_topic.php
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="add_topic.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="-5" align="right" bgcolor="#E6E6E6"></td>
<td colspan="3" bgcolor="#E6E6E6"><strong>Create New Topic</strong> </td>
</tr>
<tr>
<td width="14%"><strong>Topic</strong></td>
<td width="2%">:</td>
<td width="84%"><input name="topic" type="text" id="topic" size="50" /></td>
</tr>
<tr>
<td valign="top"><strong>Detail</strong></td>
<td valign="top">:</td>
<td><textarea name="detail" cols="50" rows="3" id="detail"></textarea></td>
</tr>
<tr>
<td><strong>Name</strong></td>
<td>:</td>
<td><input name="name" type="text" id="name" size="50" /></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>:</td>
<td><input name="email" type="text" id="email" size="50" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
now we need the post method add_topic.php so lets create it.
this method is basically a file that will put the data in the db
add_topic
<?php/*we include db.php*/ require_once('db.php')?>
<?php
$tbl_name="forum_question"; // Table name
// get data that sent from form at create_topic.php
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name'];
$email=$_POST['email'];
$datetime=date("d/m/y h:i:s"); //create date time
$sql="INSERT INTO $tbl_name(topic, detail, name, email, datetime)VALUES('$topic', '$detail', '$name', '$email', '$datetime')";
$result=mysql_query($sql);
if($result){
echo "Successful<BR>";
echo "<a href=index.php>View your topic</a>";
}
else {
echo "ERROR";
}
mysql_close();
?>