Sie sind auf Seite 1von 6

@INTE ACADEMIA

Curso de PHP

Sesin 34. Foro


34.1. Foro: la base de datos Vamos a crear un foro donde los usuarios puedan ir dejando noticias y comentarios. A continuacin tiene el archivo de base de datos. Ejecute esta consulta entrando en la ventana SQL.
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) ) TYPE=MyISAM;

1/6

AINTE INFORMTICA S.L.

@INTE ACADEMIA

Curso de PHP

Ejecute tambin estas dos consultas para aadir algunos datos a la base de datos. INSERT INTO foro VALUES (1,'pablo','probando','Este es un mensaje de prueba','0000-00-00 00:00:00',2,0,'0000-00-00 00:00:00'); INSERT INTO foro VALUES (2,'federico','Otra prueba bedelesca','SEguimos metiendo mensajes que no sirven para nada,'2003-05-02 00:14:47',3,0,'2003-05-02 00:14:47');

34.2. El archivo configuracion.php Este archivo contiene la conexin a la base de datos. <?php $bd_host = "localhost"; $bd_usuario = "root"; $bd_password = ""; $bd_base = "foro"; $con = mysql_connect($bd_host, $bd_usuario, $bd_password); mysql_select_db($bd_base, $con); ?>

2/6

AINTE INFORMTICA S.L.

@INTE ACADEMIA

Curso de PHP

34.3. El archivo funciones.php Este archivo contiene un par de funciones tiles. La funcin mostrarTemplate extrae las variables e imprime el tema. La funcin parsearTags busca el tag CITAR y lo sustituye por un texto en formato de cita (quote). <?php function mostrarTemplate($tema, $variables) { //var_dump($variables); extract($variables); eval("?>".$tema."<?"); } function parsearTags($mensaje) { $mensaje = str_replace("[citar]", "<blockquote><hr width='100%' size='2'>", $mensaje); $mensaje = str_replace("[/citar]", "<hr width='100%' size='2'></blockquote>", $mensaje); return $mensaje; } ?>

3/6

AINTE INFORMTICA S.L.

@INTE ACADEMIA

Curso de PHP

34.4. El archivo header.html Este archivo es la cabecera de todas las pginas.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>El Foro de ZonaPHP</title> <meta http-equiv="Content-Type" content="text/html; charset=iso8859-1"> //ah un estilo css embebido en la cabecera <style type="text/css"> <!-body { font-family: Arial, Helvetica, sans-serif; color: #FFFFFF; } a{ text-decoration: none; } --> </style> </head> <body bgcolor="#9999cc" text="#FFFFFF" link="#FFFFFF"> <table width="90%" border="0" align="center" cellpadding="2" cellspacing="2"> <tr> <td bgcolor="#4b557d"><h2>El Foro de ZonaPHP</h2> <div align="right">[ <a href="index.php">Inicio</a> ] [ <a href="respuesta.php">Nuevo 4/6 AINTE INFORMTICA S.L.

@INTE ACADEMIA Tema</a> ]</div></td> </tr> </table> 34.5. El archivo footer.html Este es el archivo que va al final de cada pgina.

Curso de PHP

<table width="90%" border="0" align="center" cellpadding="2" cellspacing="2" bgcolor="#4b557d"> <tr> <td align="center">Foro de Jos Luis - Bajo licencia GPL</td> </tr> </table> </body> </html> 34.6. El archivo index.php Este es el archivo principal. Lista las noticias del foro. <?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) 5/6 AINTE INFORMTICA S.L.

@INTE ACADEMIA { // 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'); ?>

Curso de PHP

6/6

AINTE INFORMTICA S.L.

Das könnte Ihnen auch gefallen