Building a forum in PHP and Mysql

by Pablo Rigazzi - 22 April 2008

The Templates

Before the next step of our tutorial, I want to make some comments in regards to the “simple” template system that we will use in the forum. First, for those who don’t know what a Template is, I recommend to read Nok‘s and Luigi‘s articles in out Templates section in Zonaphp.com, to at least have an idea of what they are and their use.

Now, my idea when I use templates is that users can modify the aspect of the forum to their needs, without having to touch the code of the application. This is something that the previous tutorial didn’t consider. It included all the HTML output code inside the forum itself, which is not always advisable. What we will do in this case is use .html files that will only contain design (that means only HTML), and in the places in where we want to add dynamic content we will use the simple format to show the variables’ content, that is:

<?=$variable?>.

To “interpret” a template, first we read it in memory using the function file() and then we use a simple function to which we pass like parameter the template and the variables that are to be replaced. For example:

Sample of the page ejemplo.html

Nombre: <b><?=$nombre?></b><br>
Edad : <b><?=$edad?></b><br>
Domicilio : <?=$domicilio?> <hr>

Sample of the page ejemplo.php

<?php
function mostrarTemplate($tema, $variables)
{
//var_dump($variables);
extract($variables);
eval(“?>”.$tema.”<?”);
}


$agenda = array(
“0” => array(“nombre”=>”Marcelo”, “edad”=>”25”, “domicilio”=>”VeraCRuz 342”),
“1” => array(“nombre”=>”Alejandra”, “edad”=>”18”, “domicilio”=>”Los Olmos 67”),
“2” => array(“nombre”=>”Micaela”, “edad”=>”23”, “domicilio”=>”Prof. Mariño 8”) );

$tpl = implode(“”, file(“ejemplo.html”));

foreach($agenda as $registro)
{
mostrarTemplate($tpl, $registro);
}
?>

Here we see how, starting from the data that we have in an array, we show information based on a simple template. The function mostrarTemplate uses the content from the template (like parameters) and an associative array with the values to replace. The PHP function extract () is in charge of passing these values to the environment in which the function is called, in this case, the environment of the function. This is the reason why if we have an array like $a = Array (” key” => “value”), when executing extract($a), then we will have a variable available of name $variable and with “value” like content. The eval() function is in charge of the rest, that is, to execute all the code (the one of the Template) that is passed to it as parameter.

Some will wonder, why don’t we simply use an “include” function? The answer is very simple. Efficiency. The function include implies access to disc, which is quite “expensive” in efficiency terms. So reading template a single time in memory, and then using it several times, we make a better use of the resources. Taking these issues into consideration, we can now follow with the development the Forum.

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