|
||||
| Register--Login--Top 20 Posters--Search Topics |
Forum Main>>Tutorials>>Simple forum in php/mysql | ||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 PM [Chipmunk] View Member Photo | Last replied to on Sat May 05, 2012 04:49:13 Edit Post|Quote If you've ever wondered how to build a simple forum in PHP/Mysql, this simple tutorial will give you the basics of how to post and reply to threads and display messages. First we need to create the SQL tables which will store the data for our threads and posts for this forum:
This is pretty basic. It creates the fields for the id of the post(this is how each post is identified), the author, the title, the time of post, the number of replies, if it was a reply, the thread it is a reply of, the last person to reply, and when the thread was last replied to. Next we have the basic connect.php file to connect to our database:
Simple, a simple connector function where you have to input your username, password, and database name to connect. Not that the function below starting at if(!get_magic_quotes_gpc()) is not actually part of the connector function, it is simply to parse out mySQL injections and may not be needed. If your php to mysql extenson classes are not updated, mysql_escape_string may not work and you will have to take the parser lines out. Next we have a stylesheet to give our forum colors and look:
Simple basic stylesheet to define some table headings and colors we will use, you can change this as you please. Now the actual message board files. We only need 4 files, index.php(the one to display all the threads), post.php(The file to post and start threads), message.php(The file that displays the messages), and reply.php(The file to reply to posts). First index.php:
This is straightforward, it simply selects all threads with a parentid of 0, meaning that all threads that are not replies and it selects them by the last time they were replied to, beginning with the latest first. Then it goes through a loop to display all of them along with the number of replies they have, and the thread starter. It links the thread subject with a message identifier to message.php so people can view the thread by clicking on the link. The strip_tags function parses out SQL injections that user might have put in. Now the message.php file:
This file gets some data passed to it in the form $id in the URL. What this code does is it first selects the topic post in $gettopic, printing it and then selects all the posts in $getreplies and prints them. Strip_tags is there to parse out HTML injectons and nl2br is there to make sure line breaks work right. Now the post.php file
This file has two cases, if submit was pressed, and if submit was not pressed. If submit was not pressed, then it prints a form for you to type in , your name, a message subject, and a message. If you have pressed submit, it checks first if the required fields of name, subject, and post have been entered and spits out error message if the have not. If everything has been entered, then it draws the local time both in Unix format and in readable format and inserts all the data of the post along with the timestamps into the database. Now the reply.php file:
Note this is very similar to the post.php file except it passed a hidden parameter id, which indicates which thread you are reply to. Also after you have pressed submit, it checks if the required fields have been filled out, inserts the message with the timestamps and then it updates the original message which you replied to with new timestamps and adds one to the number of replies that message has while putting you as the last replier to that message. Ok, there you have it, a simple working forum. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Fri Feb 02, 2007 07:49:54 Edit post|Quote instead of order by postid desc order by postid asc should do the trick. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Fri Mar 09, 2007 04:31:42 Edit post|Quote Yeah, but generally these tutorials are not going to be that long. I could write a short book about that. The entire forum consists of many, many files and thousands of lines of actual code. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Sun Mar 25, 2007 16:30:54 Edit post|Quote That means you don't have the database name correct because its not recognizing the name of your database. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Thu Apr 05, 2007 19:07:41 Edit post|Quote Turn debug mode off in PHP and those will go away. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Fri Apr 06, 2007 19:19:28 Edit post|Quote You can't in PHPMYADMIN, it has to be done in php.ini . You may have to talk to your host about this one. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Sat Apr 21, 2007 14:40:16 Edit post|Quote
This worked for the first person that asked. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Sat Apr 21, 2007 16:01:26 Edit post|Quote Do order by lastreplied desc then ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Sat Apr 21, 2007 17:14:32 Edit post|Quote order by lastrepliedto desc will sort the topic in the order that they were last replied to. FOr new topics, I think you may have to insert the current time for lastreplied to. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Sat Apr 21, 2007 18:18:38 Edit post|Quote
That line, you need to insert lastreplied to and give it the $thedate value. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Sat Apr 21, 2007 19:15:23 Edit post|Quote Its in post.php, you need to insert the data for lastrepliedto . ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Sat Apr 21, 2007 20:13:19 Edit post|Quote yeah, you need to add data stored in $thetime to the lastrepliedto field. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Sun Apr 29, 2007 14:22:46 Edit post|Quote what problem? ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Sun May 06, 2007 00:44:17 Edit post|Quote I already answered that question in the above posts. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Sun May 06, 2007 16:12:07 Edit post|Quote You don't have to change that, you just have to insert the current date into that field when posting. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Mon May 07, 2007 18:48:05 Edit post|Quote
I forgot what the actual field name was(not at home right now) and what the actual variable I used for time was, but thats how you do it. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Wed Jun 20, 2007 15:11:30 Edit post|Quote Yeah, read up on the substr() function. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Fri Jun 29, 2007 19:06:34 Edit post|Quote Better searching can be done by indexing the fields you want to search and using the mySQL match() function, even if it is bitchy to use sometimes. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Wed Jul 18, 2007 23:40:03 Edit post|Quote There's various PHP functions for BBCODE out there, just go grab one of those. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Sun Sep 16, 2007 16:36:41 Edit post|Quote You basically need a file that has a form and does a delete query from b_posts. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 [PM Chipmunk] View Member Photo | Posted at Mon Feb 11, 2008 23:34:10 Edit post|Quote Yeah, you just need to modify the value of some of the date() functions. ----------------------------- Chipmunk, Supreme Administrator | |||||||
Page: 1 |