|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
daoho Smarty n00b
Joined: 12 Aug 2003 Posts: 4
|
Posted: Tue Aug 12, 2003 1:06 pm Post subject: prob to view the result of a select.... |
|
|
Hi everybody...
I'm actually working in a research center with smarty and i've got a problem :
here is the php code for doing an array with the result of my SQL request
Code: |
$sql_affich="SELECT * FROM ".$table ;
$affich = mysql_query($sql_affich) or die(mysql_error());
$i = 0;
$ligne = 0;
while ($rangee=mysql_fetch_row($affich)) {
for($i=0 ; $i < mysql_num_fields($affich) ; $i++) {
$champ=mysql_fetch_field($affich,$i);
$smarty->assign('tableau',array("lig".$ligne => array ($champ->name => $rangee[$i])));
}
$ligne++;
}
|
I don't know how to write correctly the template to have a view of the data of $smarty
(perhaps should I modify my uilding of the array...it's not a problem...)
Thanks[/code] |
|
Back to top |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Tue Aug 12, 2003 1:31 pm Post subject: |
|
|
There your just overwritting the same variable over and over which isn't much use. I suggest you use an array of fields something like[php:1:0bd13602f4]$sql_affich="SELECT * FROM ".$table ;
$affich = mysql_query($sql_affich) or die(mysql_error());
$tableau = array();
while ($rangee=mysql_fetch_assoc($affich)) {
$tableau[] = $rangee;
}
$smarty->assign('tableau', $tableau);[/php:1:0bd13602f4]then in your smarty template you can use a foreach loop Code: | {foreach from=$tableau item=$foo}
<b>{$foo.name}</b><br />
{/foreach} |
_________________ "Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane |
|
Back to top |
|
daoho Smarty n00b
Joined: 12 Aug 2003 Posts: 4
|
Posted: Tue Aug 12, 2003 1:43 pm Post subject: |
|
|
it could be nice but there is a problem with your code :
Parse error: parse error, unexpected T_STRING, expecting ']' in c:\site\smarty_dir\templates_c\%%-58\%%-583032944\tab.tpl.php on line 11
first time i've got an error like that....
I paste your code without changing anything... |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Aug 12, 2003 2:33 pm Post subject: |
|
|
i think in the foreach it should read "item=foo" and not "item=$foo". a very common mistake |
|
Back to top |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Tue Aug 12, 2003 3:07 pm Post subject: |
|
|
Heh well I often just post code without testing _________________ "Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Aug 12, 2003 3:35 pm Post subject: |
|
|
me too
[EDIT: and BTW i consider it a bug in smarty that it produces invalid php in this case instead of raising a syntax error, but it's far too hot over here in germany right now to tamper in the guts of smarty_compiler.class.php for such minor issues ] |
|
Back to top |
|
|
|
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
|