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

prob to view the result of a select....

 
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 -> General
View previous topic :: View next topic  
Author Message
daoho
Smarty n00b


Joined: 12 Aug 2003
Posts: 4

PostPosted: Tue Aug 12, 2003 1:06 pm    Post subject: prob to view the result of a select.... Reply with quote

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
View user's profile Send private message
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Tue Aug 12, 2003 1:31 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
daoho
Smarty n00b


Joined: 12 Aug 2003
Posts: 4

PostPosted: Tue Aug 12, 2003 1:43 pm    Post subject: Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Aug 12, 2003 2:33 pm    Post subject: Reply with quote

i think in the foreach it should read "item=foo" and not "item=$foo". a very common mistake Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Tue Aug 12, 2003 3:07 pm    Post subject: Reply with quote

Heh well I often just post code without testing Cool
_________________
"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
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Aug 12, 2003 3:35 pm    Post subject: Reply with quote

me too Smile

[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 Smile ]
Back to top
View user's profile Send private message Send e-mail 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 -> General 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