Building a forum in PHP and Mysql

by Pablo Rigazzi - 22 April 2008

One topic at a time

Already having the base of our forum, the design of the table in the database and knowing how our system of templates works, we can begin to create a front page, in which we will show all the topics of the forum:

Sample of the page index.php

<?php
require(‘configuracion.php’);
require(‘funciones.php’);
include(‘header.html’);
/* Pedimos todos los temas iniciales (identificador==0)
* y los ordenamos por ult_respuesta */
$sql = “SELECT id, autor, titulo, fecha, respuestas, ult_respuesta “;
$sql.= “FROM foro WHERE identificador=0 ORDER BY ult_respuesta DESC”;
$rs = mysql_query($sql, $con);
if(mysql_num_rows($rs)>0)
{
// Leemos el contenido de la plantilla de temas
$template = implode(“”, file(“temas.html”));
include(‘titulos.html’);
while($row = mysql_fetch_assoc($rs))
{
$color=($color==””?”#5b69a6″:””);
$row[“color”] = $color;
mostrarTemplate($template, $row);
}
}
include(‘footer.html’);
?>

Is that all? Yes, that is everything. Now we will review step by step what the script does. First we have a series of requires and includes.

The first of them includes the first Script that we wrote, which makes the connection to the data base. The second script includes archivo.php that contains important functions for the forum like, for example, mostrarTemplate.

The third script includes a generic header which we will use to give all our pages a similar design.There we will be able to place a logo for the site, important links, banners, etc.



$sql = “SELECT id, autor, titulo, fecha, respuestas, ult_respuesta “;
$sql.= “FROM foro WHERE identificador=0 ORDER BY ult_respuesta DESC”;
$rs = mysql_query($sql, $con);
if(mysql_num_rows($rs)>0)
{

Here we execute a query in the database, which will bring all the messages that are initiators of a subject, that is, those whose identifier is in zero. The rest of the messages, that are answers to a subject in particular, will have the field identifier with the value of the message to which they respond. We asked the database to order these topics by the date of the last answer, in descending order. Therefore, in our forum, the most recent messages will appear first. Also we carried out a decision by which we only show the topics of our Forum if the amount of rows recovered from the database is greater than 0.

Then, within the main While of the program, we generate all the “magic”:

// Leemos el contenido de la plantilla de temas
$template = implode(“”, file(“temas.html”));
include(‘titulos.html’);
while($row = mysql_fetch_assoc($rs))
{
$color=($color==””?”#5b69a6″:””);
$row[“color”] = $color;
mostrarTemplate($template, $row);
}

Here we begin to work for the first time with the templates. First we read the content of the template in memory and we kept within the variable $template. Also we included a file that contains a row of the Topics table (Temas), with the cells’ titles. We saw the rest previously; by calling the function mostrarTemplate we show the data of each topic.

Finally we only add another HTML file, with the code to close the page and to show some Copyright message (or whatever we want to write). With this we already have our initial page of the Forum, showing the existing topics. Now, we are going to create the form that is needed to enter new topics or to respond existing ones.

Previous page Next page

4 Comments on “Building a forum in PHP and Mysql”

  1. Codespanish » Blog Archive » Why you shouldn’t code in Spanish Says:

    […] Take for example this PHP code from the post “Building a forum in PHP and Mysql”. […]

  2. Jaime recuerdo Says:

    Hola, gracias para estos consejos que me han servido para crear mi foro gratis en el sitio foroactivo. Ahí, tienen varias versiones de foros phpbb y sobre todo la posibilidad pasar de la versión phpbb2 a 3 o invision sin problema como un bueno webmaster. Además, esta nueva versión tiene muchas opciones gratis como mas de 3000 skin, la posibilidad de cambiar lo CSS y templates, crear perfiles personalizados, chatbox, sistema de reputación…y la ultima es cambiar los foros en blog y viceversa ! saludos

  3. Pablo Matamoros Says:

    No problems, as long as you link back or mention us.

  4. Abrir Correo Says:

    Es interesante lo que dices. Ya te agregué a mi lector de feeds RSS, adios

Leave a Reply