Note1: This article was first published in June of 2003 by “Maestros del Web“. The functions used in the code might need changes for newer versions of PHP and MySQL. We will be happy to receive any comments on this matter.
Note 2: Although the content of this post has been translated to English, the original PHP code is in Spanish.
In this tutorial we will create a forum from scratch by learning to separate code from design and modularising our application so that it is simple to setup and adapt
First of all, we must prepare the table in the database that will contain all the discussions of our forum. What this table will have is messages information, their authors, and other data that will serve to identify what discussion the message belongs to. This is the proposed structure:
CREATE TABLE `foro` (
`id` int(7) NOT NULL auto_increment,
`autor` varchar(200) NOT NULL default ”,
`titulo` varchar(200) NOT NULL default ”,
`mensaje` text NOT NULL,
`fecha` datetime NOT NULL default ’0000-00-00 00:00:00′,
`respuestas` int(11) NOT NULL default ’0′,
`identificador` int(7) NOT NULL default ’0′,
`ult_respuesta` datetime default NULL,
KEY `id` (`id`)
Lets see in detail what fields our Forum’s table will have:
- id:will be the main identifier of the table. It serves to differentiate each message.
- autor: the message’s author
- titulo: the title that the message will have. If this message is the initiator of a discussion, it will be the one that will show in the forum’s home.
- mensaje: the message itself.
- fecha: a field DATETIME that will indicate the date each message has been published.
- respuestas:if this message is the initiator of a discussion, the number of answers that it receives will be accumulated here.
- identificador:this field keeps the id value of the message that is being answered. If the message is initiator of a discussion, then this field will be in zero
- ult_respuesta: if the message is a discussion initiator, the value will be updated with the date of the last answer that has received. It is useful to be able to maintain an order in the forums.
The above SQL command can be kept in a .sql file and run in your database from phpMyAdmin or, if you prefer it, it can be entered line by line in a text editor of mySQL (like mySQL monitor). Once we have created the table in your database, we can then advance to the following step. Before coding anything for the forum, we are going to create a small script that carries out a task to be repeated a lot, and that is to connect us to the database. This script will be included in each page where we have to access the database:
Sample of the page configuracion.php
$bd_host = “localhost”;
color=”#008000″>$bd_usuario = “user”;
$bd_password = “password”;
$bd_base = “nuestra_bd”;
$con = mysql_connect($bd_host, $bd_usuario, $bd_password);
As we can see, there is not rocket science in this script, only variables containing the configuration of our database, and the minimum and necessary code to connect and to keep the resource from the connection in a variable $con that soon we will use when we make our queries.
Stumble it! Bookmark it! Digg it! Menéame! Fresqui Suscribe Suscribe