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
3 Pages V  < 1 2 3 >   Reply to this topic Start new topic
left right
benhaider
post May 4 2010, 11:32 AM
Post #21


Newbie
*

Group: Members
Posts: 1
Joined: 4-May 10
Member No.: 140,789



appears to be that i facing errors with it......this one has bug in it.....installed three times...but screen shows blank.....
QUOTE(ipsauron @ Feb 23 2008, 02:25 PM) *
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
ckjafer
post Jul 22 2010, 09:59 AM
Post #22


Newbie
*

Group: Members
Posts: 2
Joined: 22-July 10
Member No.: 150,076



thanks
Go to the top of the page 
 
  + Quote Post
Datumama
post Sep 7 2010, 12:48 AM
Post #23


Newbie
*

Group: Members
Posts: 8
Joined: 21-June 09
Member No.: 100,373



Neatly working on my site, also added some stuff in it such as images and a login/registration area. How about adding a bbc or html codes support and emoticons as well? Can somebody help me do that? http://www.datuwap.tk/forum
Go to the top of the page 
 
  + Quote Post
jim253
post Dec 20 2010, 06:46 AM
Post #24


Newbie
*

Group: Members
Posts: 13
Joined: 10-December 10
Member No.: 168,537



Thanks sharing a nice post..
Go to the top of the page 
 
  + Quote Post
safeguard
post Apr 1 2011, 08:18 PM
Post #25


Member
**

Group: Members
Posts: 49
Joined: 23-March 11
Member No.: 183,301



thank for sharing nice script
Go to the top of the page 
 
  + Quote Post
oneinamillion
post Apr 25 2011, 03:22 AM
Post #26


Newbie
*

Group: Members
Posts: 17
Joined: 25-April 11
Member No.: 187,832



wow. this is nothing like html at all........ i will have to learn a lot to customize it.
Go to the top of the page 
 
  + Quote Post
Harriesta
post May 2 2011, 02:49 PM
Post #27


Newbie
*

Group: Members
Posts: 4
Joined: 24-July 10
Member No.: 150,374



Nice code...
Go to the top of the page 
 
  + Quote Post
july
post May 17 2011, 08:09 AM
Post #28


Newbie
*

Group: Members
Posts: 6
Joined: 10-January 11
Member No.: 172,294



O.M.G, as a beginer, i don't think it's simple~~that's good
Go to the top of the page 
 
  + Quote Post
imdprince
post Jul 4 2011, 06:56 AM
Post #29


Newbie
*

Group: Members
Posts: 3
Joined: 4-July 11
Member No.: 197,065



Wow Great buddy

Superb
Go to the top of the page 
 
  + Quote Post
johnco
post Jul 5 2011, 06:52 AM
Post #30


Newbie
*

Group: Members
Posts: 1
Joined: 4-July 11
From: Paranaque, Manila PH.
Member No.: 197,058



Nice one ! thanks for sharing . I like it ..
Go to the top of the page 
 
  + Quote Post
halwits
post Aug 18 2011, 05:54 AM
Post #31


Member
**

Group: Members
Posts: 42
Joined: 29-July 11
Member No.: 200,941



so thanks its a very nice code.
Go to the top of the page 
 
  + Quote Post
halwits
post Aug 19 2011, 06:07 AM
Post #32


Member
**

Group: Members
Posts: 42
Joined: 29-July 11
Member No.: 200,941



I think its a very good code so thanks.
Go to the top of the page 
 
  + Quote Post
halwits
post Aug 30 2011, 08:16 AM
Post #33


Member
**

Group: Members
Posts: 42
Joined: 29-July 11
Member No.: 200,941



so thanks for share with me its a amazing code.
Go to the top of the page 
 
  + Quote Post
kayakalp
post Sep 2 2011, 05:16 AM
Post #34


Newbie
*

Group: Members
Posts: 1
Joined: 11-July 11
Member No.: 198,131



Nice post thanks for sharing.
Go to the top of the page 
 
  + Quote Post
hipreplacement
post Sep 14 2011, 10:02 PM
Post #35


Newbie
*

Group: Members
Posts: 2
Joined: 19-July 11
Member No.: 199,399



I'm still learning PHP, I've always noticed that these are good along with YouTube videos. It all depends on what works best for yourself though.
Go to the top of the page 
 
  + Quote Post
dick123
post Sep 27 2011, 06:43 AM
Post #36


Newbie
*

Group: Members
Posts: 13
Joined: 21-September 11
Member No.: 209,728



Its a really good.
Go to the top of the page 
 
  + Quote Post
soniasyril
post Oct 28 2011, 07:35 AM
Post #37


Newbie
*

Group: Members
Posts: 1
Joined: 28-October 11
Member No.: 215,399



it's good but simple. you should add html tables with 2 columns.
Go to the top of the page 
 
  + Quote Post
PSNcallofduty
post Oct 29 2011, 02:37 AM
Post #38


Newbie
*

Group: Members
Posts: 14
Joined: 9-October 11
Member No.: 212,631



QUOTE(soniasyril @ Oct 28 2011, 07:35 AM) *
it's good but simple. you should add html tables with 2 columns.


There are tables used, he tends to use 'echo' to output his HTML instead of closing the php tag and putting the code and the starting the next php section. I must admit in some cases it looks better and is easier to edit and others its not. depends on your style, its always nice to stay consistent in your files though.
Go to the top of the page 
 
  + Quote Post
dick123
post Nov 17 2011, 06:16 AM
Post #39


Newbie
*

Group: Members
Posts: 13
Joined: 21-September 11
Member No.: 209,728



thanks for share these post its a amazing.
Go to the top of the page 
 
  + Quote Post
jimmycarter
post Feb 18 2012, 11:08 AM
Post #40


Newbie
*

Group: Members
Posts: 19
Joined: 14-February 12
From: United Kingdom
Member No.: 230,315



Your information is very useful for human beings. Thanks for sharing me.
Go to the top of the page 
 
  + Quote Post
3 Pages V  < 1 2 3 >
 Reply to this topic Start new topic
left right
0 Members:
left right
 


Lo-Fi Version Time is now: 20th June 2013 - 03:32 AM