Zymic Forums

Webmaster resources

Zymic IRC Server

Chat in real time at irc.zymic.com - Learn More

Welcome

Welcome to the Zymic webmaster forums. Our forums are here to provide people the free ability to discuss a range of websites related topics such as design, development coding and marketing.

In order to post you will need to register for a zymic account or if you already have one simply login by using the form on the left.

left Zymic Webmaster ForumsZymic Free Web HostingTutorials right
left right
ipsauron
post Feb 23 2008, 02:25 PM
Post #1


Newbie
*

Group: Members
Posts: 26
Joined: 29-October 07
Member No.: 2,078



Step 1: Setup your database
Im going to use MySQL for this tutorial.

First create a database. Im going to use one i call "Test_Forum".

Second we're gonna create the tables:

SQL: (for execution in PHPMyAdmin)

CODE
CREATE TABLE `topics` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`title` VARCHAR( 255 ) NOT NULL ,
`post` VARCHAR( 255 ) NOT NULL ,
`author` VARCHAR( 255 ) NOT NULL ,
`mail` VARCHAR( 255 ) NOT NULL ,
`date` VARCHAR( 255 ) NOT NULL
) ENGINE = MYISAM;

CREATE TABLE `replies` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`topic_id` INT( 11 ) NOT NULL ,
`post` VARCHAR( 255 ) NOT NULL ,
`author` VARCHAR( 255 ) NOT NULL ,
`mail` VARCHAR( 255 ) NOT NULL ,
`date` VARCHAR( 255 ) NOT NULL
) ENGINE = MYISAM;


Step 2: index.php
Now we're gonna make the index.php to show our topics.

CODE
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "Test_Forum";

$con = mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);

echo "<a href='create_topic.php'>New topic</a>";
echo "<table border='0' width='100%'>
<tr><td>#</td><td>Title</td><td>Replies</td><td>Post Date</td></tr>";

$sql = mysql_query("SELECT * FROM topics ORDER BY id DESC");
while($row = mysql_fetch_array($sql)) {
$replies = mysql_num_rows(mysql_query("SELECT * FROM replies WHERE topic_id='".$row['id']."'"));

echo "<tr><td>".$row['id']."</td><td><a href='show_topic.php?id=".$row['id']."'>".htmlentities($row['title'])."</a></td><td>".$replies."</td><td>".date("j/n - y",$row['date'])."</td></tr>";
}

echo "</table>";
mysql_close($con);
?>


Explanation:
First we connect to the host and database using the functions mysql_connect() and mysql_select_db().
Then we make the link to create_topic.php.
Then we make the start of a table where we will inside of it save our info.
The $sql variable contains the mysql_query and SQL to use in our while loop.
Inside the while loop we got a $row variable with a function called mysql_fetch_array() which uses the $sql variable.
The $replies variable counts the number of rows inside the replies table using the functions mysql_num_rows().
Then we post the HTML with the $row array.
And lastly we close the connection to the MySQL host using mysql_close().

Step 3: create_topic.php

This is gonna exist of a form which leads us to add_topic.php.

CODE
<table>
<form action='add_topic.php' method='post'>
<tr><td>Name</td><td>:</td><td><input type='text' id='name' name='name' /></td></tr>
<tr><td>Email</td><td>:</td><td><input type='text' id='mail' name='mail' /></td></tr>
<tr><td>Title</td><td>:</td><td><input type='text' id='title' name='title' /></td></tr>
<tr><td valign='top'>Post</td><td valign='top'>:</td><td><textarea id='post' name='post' rows='7'></textarea></td></tr>
<tr><td> </td><td> </td><td><input type='submit' value='Post Topic' /> <input type='reset' value='Reset Fields' /></td></tr>
</form>
</table>


This form is self-explanatory.

Step 4: add_topic.php

CODE
<?php
if (!empty($_POST)) {
$host = "localhost";
$user = "root";
$pass = "";
$db = "Test_Forum";

$con = mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);

if (mysql_query("INSERT INTO topics (title,post,author,mail,date) VALUES ('".$_POST['title']."','".$_POST['post']."','".$_POST['name']."','".$_POST['mail']."','".time()."')")) {
header("location:index.php");
} else {
echo "Error!";
}

mysql_close($con);
}
?>


First we connect to the host and database using the functions mysql_connect() and mysql_select_db().
Then we use mysql_query to insert the data into our table.
If its a succes it will return you to index.php.
If not... Well it will say Error...

Step 5: show_topic.php

CODE
<?php
if (isset($_GET['id'])){
$host = "localhost";
$user = "root";
$pass = "";
$db = "Test_Forum";

$con = mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);

$id = $_GET['id'];

echo "<table border='0' width='100%'>";

$sql = mysql_query("SELECT * FROM topics WHERE id='".$id."'");
while($row = mysql_fetch_array($sql)) {
echo "<tr><td>".htmlentities($row['title'])."</td></tr>
<tr><td>".str_replace("\n","<br />",htmlentities($row['post']))."</td></tr>
<tr><td><a href='mailto:".$row['mail']."'>".htmlentities($row['author'])."</a></td></tr>
<tr><td>".date("j/n - y",$row['date'])."</td></tr>";
}

echo "<tr><td> </td></tr>";

$sql2 = mysql_query("SELECT * FROM replies WHERE topic_id='".$id."' ORDER BY id ASC");
while($row2 = mysql_fetch_array($sql2)) {
echo "<tr><td>".str_replace("\n","<br />",htmlentities($row2['post']))."</td></tr>
<tr><td><a href='mailto:".$row2['mail']."'>".htmlentities($row2['author'])."</a></td></tr>
<tr><td>".date("j/n - y",$row2['date'])."</td></tr>";
}

echo "<tr><td> </td></tr>";

echo "<tr><td>
<table border='0' width='100%'>
<form action='add_reply.php' method='post'>
<input type='hidden' id='topic_id' name='topic_id' value='".$id."' />
<tr><td>Name</td><td>:</td><td><input type='text' id='name' name='name' /></td></tr>
<tr><td>Mail</td><td>:</td><td><input type='text' id='mail' name='mail' /></td></tr>
<tr><td valign='top'>Post</td><td>:</td><td><textarea id='post' name='post' rows='7'></textarea></td></tr>
<tr><td> </td><td> </td><td><input type='submit' value='Post Reply' /> <input type='reset' value='Reset Fields' /></td></tr>
</form>
</table>
</td></tr>";

echo "</table>";

mysql_close($con);
} else {
echo "invalid usage!";
}
?>


First we check if ID is set.
If it is we will connect to the host and database using the functions mysql_connect() and mysql_select_db().
And then we set the $id variable to $_GET['id'] from the address bar.
And then we set the $sql variable to contain the mysql_query and SQL to use in our while loop.
And then inside the while loop we got a $row variable with a function called mysql_fetch_array() which uses the $sql variable.
This is the Topic.
Then we make a seperation cell outside of the loop.
And then we set the $sql2 variable to contain the mysql_query and SQL to use in our while loop.
And then inside the while loop we got a $row2 variable with a function called mysql_fetch_array() which uses the $sql2 variable.
These are the replies if there are any.
Then we make the form to submit replies.
It leads to add_reply.php.
Then we close table and mysql connection.

Step 6: add_reply.php

CODE
<?php
if (!empty($_POST)) {
$host = "localhost";
$user = "root";
$pass = "";
$db = "Test_Forum";

$con = mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);

if (mysql_query("INSERT INTO replies (topic_id,post,author,mail,date) VALUES ('".$_POST['topic_id']."','".$_POST['post']."','".$_POST['name']."','".$_POST['mail']."','".time()."')")) {
header("location:showtopic.php?id=".$_POST['topic_id']);
} else {
echo "Error!";
}

mysql_close($con);
} else {
echo "Invalid usage!";
}
?>


First we connect to the host and database using the functions mysql_connect() and mysql_select_db() if $_POST is not empty.
Then we use mysql_query to insert the data into our table.
If its a succes it will return you to the topic.
If not... Well it will say Error...

Step 7: Finish!

This concludes the simple forum tutorial.
Hope you find it useful.
I have not made any edit or delete because its a simple forum.

This has been tested on a PHP server and it works.

Online Demo
Go to the top of the page 
 
  + Quote Post

Posts in this topic
ipsauron   Php Simple Forum   Feb 23 2008, 02:25 PM
Conmiro   Nice!   Mar 1 2008, 10:51 PM
Paradoks   Simple is an OVERSTATEment. But its raw, I has ALO...   Mar 2 2008, 07:55 AM
ipsauron   Thank you :P   Mar 2 2008, 06:29 PM
Soul Of Me   Edited, found out what the error was, BUT everytim...   Mar 8 2008, 08:51 PM
wozzym   is it supposed to be all white and super simple? ...   Mar 16 2008, 03:44 AM
Soul Of Me   I am so freaking dumb! i forgot to change the...   Mar 16 2008, 11:00 PM
HBF   To be honest, it is good. But the fact that its so...   Apr 3 2008, 07:30 PM
Valency   Very nice tutorial, Nice in depth and in detail. K...   Apr 3 2008, 09:49 PM
evolution815   hmmm. nice I guess i'll try out the code at ...   Apr 4 2008, 11:21 AM
eshoppe   Is zymic webhosting for PHP supports mysqli functi...   Apr 12 2008, 09:18 AM
Crown   Is zymic webhosting for PHP supports mysqli functi...   Apr 12 2008, 09:33 AM
ipsauron   Thanks for all replies guys. I will soon write a ...   Apr 15 2008, 09:22 PM
Creactive Online   Already up and running on mine - http://www.creact...   Apr 16 2008, 08:11 PM
uncled1023   i used your tutorial for my forum. it works great...   Apr 29 2008, 05:18 AM
ipsauron   Ok... Really i dont suggest using this exact forum...   Apr 29 2008, 09:10 PM
ipsauron   I have decided to disallow posting for the Online ...   May 2 2008, 09:45 PM
wizzyfox   Hi! I couldnt help but notice that this is t...   Jan 30 2010, 03:23 PM
phpprogrammer   can anyone provide a link to same kind of this tut...   Mar 18 2010, 06:20 AM
flaviusmihaiu   This is really great and I have tried this myself....   Apr 13 2010, 09:15 AM
ckjafer   thanks   Jul 22 2010, 09:59 AM
benhaider   appears to be that i facing errors with it......th...   May 4 2010, 11:32 AM
halwits   so thanks its a very nice code.   Aug 18 2011, 05:54 AM
Datumama   Neatly working on my site, also added some stuff i...   Sep 7 2010, 12:48 AM
jim253   Thanks sharing a nice post..   Dec 20 2010, 06:46 AM
safeguard   thank for sharing nice script   Apr 1 2011, 08:18 PM
oneinamillion   wow. this is nothing like html at all........ i wi...   Apr 25 2011, 03:22 AM
Harriesta   Nice code...   May 2 2011, 02:49 PM
july   O.M.G, as a beginer, i don't think it...   May 17 2011, 08:09 AM
imdprince   Wow Great buddy Superb   Jul 4 2011, 06:56 AM
johnco   Nice one ! thanks for sharing . I like it ..   Jul 5 2011, 06:52 AM
halwits   I think its a very good code so thanks.   Aug 19 2011, 06:07 AM
halwits   so thanks for share with me its a amazing code.   Aug 30 2011, 08:16 AM
kayakalp   Nice post thanks for sharing.   Sep 2 2011, 05:16 AM
hipreplacement   I'm still learning PHP, I've always notice...   Sep 14 2011, 10:02 PM
dick123   Its a really good.   Sep 27 2011, 06:43 AM
soniasyril   it's good but simple. you should add html tabl...   Oct 28 2011, 07:35 AM
PSNcallofduty   it's good but simple. you should add html tabl...   Oct 29 2011, 02:37 AM
dick123   thanks for share these post its a amazing.   Nov 17 2011, 06:16 AM
jimmycarter   Your information is very useful for human beings. ...   Feb 18 2012, 11:08 AM
elbabemed   thank you sharing this code :)   Jun 4 2012, 10:29 AM
neo_rony   Thank you very much. I was forget some code but no...   Aug 2 2012, 11:07 AM

 Reply to this topic Start new topic
left right
0 Members:
left right
 


Lo-Fi Version Time is now: 23rd May 2013 - 03:23 PM