Help - Search - Members - Calendar
Full Version: Php Simple Forum
Zymic Webmaster Forums > Zymic Free Web Hosting > Tutorials
ipsauron
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
Conmiro
Nice!
Paradoks
Simple is an OVERSTATEment. But its raw, I has ALOT of potential ;-)
ipsauron
Thank you tongue.gif
Soul Of Me
Edited, found out what the error was, BUT everytime i try to post, it comes to the error page.
wozzym
is it supposed to be all white and super simple? or am is that cuz of something else? Nice work thou anyways smile.gif
Soul Of Me
I am so freaking dumb!

i forgot to change the

$db = "Test_Forum";

to my own DB!!! z0mg, i just have to fix some issues to make it work like i want wink.gif

Thanks for the forum ohmy.gif
HBF
To be honest, it is good. But the fact that its so basic and has no design...
Valency
Very nice tutorial, Nice in depth and in detail. Keep up the good work Scorp.
evolution815
hmmm. nice

I guess i'll try out the code at home
eshoppe
Is zymic webhosting for PHP supports mysqli function for database connection
Crown
QUOTE(eshoppe @ Apr 12 2008, 09:18 AM) *
Is zymic webhosting for PHP supports mysqli function for database connection

This is good smile.gif but simple smile.gif
ipsauron
Thanks for all replies guys.

I will soon write a tutorial on how to make a more advanced forum with php. Though not as advanced as IPB Forums. I will simply call it Extended Simple Forum tongue.gif

Anyway you are free to upload this tutorial to your site aslong as you credit me. Just credit Electricity (I dont think i can change my display name on these forums...).
Jetteh22
Already up and running on mine - http://www.creactiveonline.com I credited electricity. If you get your own website, let me know so I can add it to the tutorial.
uncled1023
i used your tutorial for my forum. it works great! added some stuff of coarse...

http://www.profilewars.net/forum.php
ipsauron
Ok... Really i dont suggest using this exact forum because it got no flood control and no moderating possibilities.
ipsauron
I have decided to disallow posting for the Online Demo of this forum because of abuse and spam.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.