Php Simple Forum |
||
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.
Zymic Webmaster Forums Zymic Free Web Hosting Tutorials |
||
3 Pages
1 2 3 >
|
![]() |
Php Simple Forum |
||
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 |
|
|
Mar 1 2008, 10:51 PM
Post
#2
|
|
|
Newbie ![]() Group: Members Posts: 6 Joined: 1-March 08 Member No.: 12,424 |
Nice!
|
|
|
Mar 2 2008, 07:55 AM
Post
#3
|
|
![]() Super Ninja ![]() ![]() ![]() ![]() Group: Members Posts: 313 Joined: 12-December 07 From: Glendale CA Member No.: 4,714 |
Simple is an OVERSTATEment. But its raw, I has ALOT of potential ;-)
|
|
|
Mar 2 2008, 06:29 PM
Post
#4
|
|
![]() Newbie ![]() Group: Members Posts: 26 Joined: 29-October 07 Member No.: 2,078 |
Thank you
|
|
|
Mar 8 2008, 08:51 PM
Post
#5
|
|
|
Newbie ![]() Group: Members Posts: 24 Joined: 18-January 08 Member No.: 7,304 |
Edited, found out what the error was, BUT everytime i try to post, it comes to the error page.
|
|
|
Mar 16 2008, 03:44 AM
Post
#6
|
|
|
Outrageously Uber Ninja ![]() Group: Community Helper Posts: 2,198 Joined: 2-March 08 From: Australia Member No.: 12,578 |
is it supposed to be all white and super simple? or am is that cuz of something else? Nice work thou anyways
|
|
|
Mar 16 2008, 11:00 PM
Post
#7
|
|
|
Newbie ![]() Group: Members Posts: 24 Joined: 18-January 08 Member No.: 7,304 |
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 Thanks for the forum |
|
|
Apr 3 2008, 07:30 PM
Post
#8
|
|
|
Newbie ![]() Group: Members Posts: 5 Joined: 19-February 08 Member No.: 10,950 |
To be honest, it is good. But the fact that its so basic and has no design...
|
|
|
Apr 3 2008, 09:49 PM
Post
#9
|
|
![]() Super Duper Ninja ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 445 Joined: 24-January 08 From: Scotland! Member No.: 7,936 |
Very nice tutorial, Nice in depth and in detail. Keep up the good work Scorp.
|
|
|
Apr 4 2008, 11:21 AM
Post
#10
|
|
|
Newbie ![]() Group: Members Posts: 22 Joined: 1-October 07 Member No.: 787 |
hmmm. nice
I guess i'll try out the code at home |
|
|
Apr 12 2008, 09:18 AM
Post
#11
|
|
|
Newbie ![]() Group: Members Posts: 1 Joined: 12-April 08 Member No.: 18,766 |
Is zymic webhosting for PHP supports mysqli function for database connection
|
|
|
Apr 12 2008, 09:33 AM
Post
#12
|
|
![]() Outrageously Uber Ninja ![]() Group: Community Helper Posts: 1,756 Joined: 11-November 07 From: HouseMaybe? Member No.: 2,689 |
|
|
|
Apr 15 2008, 09:22 PM
Post
#13
|
|
![]() Newbie ![]() Group: Members Posts: 26 Joined: 29-October 07 Member No.: 2,078 |
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 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...). |
|
|
Apr 16 2008, 08:11 PM
Post
#14
|
|
![]() PHP Programmer ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 895 Joined: 9-March 08 From: Naples, FL Member No.: 13,296 |
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.
|
|
|
Apr 29 2008, 05:18 AM
Post
#15
|
|
![]() Super Duper Ninja ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 423 Joined: 24-February 08 Member No.: 11,718 |
i used your tutorial for my forum. it works great! added some stuff of coarse...
http://www.profilewars.net/forum.php |
|
|
Apr 29 2008, 09:10 PM
Post
#16
|
|
![]() Newbie ![]() Group: Members Posts: 26 Joined: 29-October 07 Member No.: 2,078 |
Ok... Really i dont suggest using this exact forum because it got no flood control and no moderating possibilities.
|
|
|
May 2 2008, 09:45 PM
Post
#17
|
|
![]() Newbie ![]() Group: Members Posts: 26 Joined: 29-October 07 Member No.: 2,078 |
I have decided to disallow posting for the Online Demo of this forum because of abuse and spam.
|
|
|
Jan 30 2010, 03:23 PM
Post
#18
|
|
|
Newbie ![]() Group: Members Posts: 5 Joined: 7-December 08 Member No.: 72,023 |
Hi!
I couldnt help but notice that this is the only tutorial on the internet (that I have found at least) That shows you how to make your own forum. I suggest that you keep developing it into a bit more advanced. |
|
|
Mar 18 2010, 06:20 AM
Post
#19
|
|
|
Newbie ![]() Group: Members Posts: 4 Joined: 13-March 10 Member No.: 134,349 |
can anyone provide a link to same kind of this tutorial in video format, maybe in youtube?
|
|
|
Apr 13 2010, 09:15 AM
Post
#20
|
|
|
Newbie ![]() Group: Members Posts: 6 Joined: 13-April 10 Member No.: 138,251 |
This is really great and I have tried this myself. It is pretty good.
Only thing I would suggest is having the details in a separate file rather than having to retype them into every file. |
|
|
![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users) |
||
| 0 Members: | ||
Forum Jump |
||
| Lo-Fi Version | Time is now: 22nd May 2013 - 01:51 AM |