Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

MySQL y Foreach

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: Spanish
View previous topic :: View next topic  
Author Message
punk-devil
Smarty n00b


Joined: 02 May 2008
Posts: 1

PostPosted: Fri May 02, 2008 7:47 am    Post subject: MySQL y Foreach Reply with quote

Pues empecemos... soy un poco inexperto en php, pero pues he hecho algunos proyectos a prueba & error y todo ha resultado kedando yo muy conforme, el punto es que ando en un nuevo proyecto y pues aca muestro lo que tengo

Index.php
Code:
<?
require ("Smarty.class.php");
require('funcion.php');
require('db.php');

$smarty = new Smarty;

// Se hace la consulta  para "paginas"
$result = mysql_query("SELECT * FROM evil_static ORDER BY id");
while($show = mysql_fetch_array($result)){
$id_pag = $show['id'];
$titulo_pag= $show['titulo'];
$fecha_pag= $show['fecha'];
$contenido_pag = $show['contenido'];

$smarty->assign("id_pag",$id_pag);
$smarty->assign("titulo_pag",$titulo_pag);

}

$smarty->display("album.tpl");


y mi album.tpl


Code:

<html>
<head>
<LINK REL="stylesheet" TYPE="text/css" HREF="estilos.css">
<title>{$titulo}</title>

</head>



<div id='cuerpo'>
<div id='paginas'>
{foreach item=paginas key=id from=paginas }
         <a href="pag.php?id={$id}">{$titulo}</a>
{/foreach}
</div>
   



</body>
</html>


Trabajo sobre MySQL, tengo la base de datos CMS con las tablas evil_content, evil_static.

En evil_static tengo ID, titulo, contenido y fecha
Ahora, lo que deseo hacer es que, la primer consulta, muestre enlaces, siendo la url el ID y el titulo del link el titulo en la DB.

Realmente no se bien donde esta el error, puesto que, a la forma en que esta el codigo en estos momentos, solo me muestra el primer registro varias veces, y no todos los registros una sola vez cada uno.
Back to top
View user's profile Send private message
mrtinez
Smarty Rookie


Joined: 31 Jan 2005
Posts: 26
Location: Mexico

PostPosted: Fri May 02, 2008 6:07 pm    Post subject: MySQL y Foreach Reply with quote

bien, veamos la primera parte.

Code:

<?
require ("Smarty.class.php");
require('funcion.php');
require('db.php');

$smarty = new Smarty;

// Se hace la consulta  para "paginas"
$result = mysql_query("SELECT * FROM evil_static ORDER BY id");
$i=0;
while($show = mysql_fetch_array($result)){
    $id_pag[$i] = $show['id'];
    $titulo_pag[$i] = $show['titulo'];
    $fecha_pag[$i] = $show['fecha'];
    $contenido_pag[$i] = $show['contenido'];
    $i++;
}

$smarty->assign(array("id_pag"=>$id_pag,
                                  "titulo_pag"=>$titulo_pag));
$smarty->display("album.tpl");


prueba poniendo el codigo de esta manera, lo que hicimos fue sacar la asignacion de la variables a smarty fuera del while, y mandarla como arrelo no como variable escalar.
a las variables del while se les agrego un indice para poderlas manejar como arreglo. prueba haciendo de esta menera.

saludos
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: Spanish All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP