|
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 |
david63 Smarty n00b
Joined: 06 Jan 2005 Posts: 3 Location: Lancashire - UK
|
Posted: Thu Jan 06, 2005 9:23 am Post subject: Outputting Data from a MySql Query |
|
|
I am new to Smarty and I am just trying to get to grips with it - so forive me if this question has been saked and answered before (I have tried serching but I am still unsure of the terminology of Smarty)
. So far I have been able to create a "static" template with no problems but now I want to be a little more adventurous.
I have a mySql table from which I want to extract and display data row by row. From what I can find in the documentation I think I need to use sections but I cannot seem to be able to figure it out.
The code I have in my php file is Code: | while($row = mysql_fetch_array($result))
{
$smarty -> assign('SITE_NAME', $row['site_name']);
} | and the code in the tpl file is Code: | {section name=sites loop=$SITE_SECTION}
<tr>
<td width="90%" colspan="2"><span class="sitename">{$SITE_SECTION[sites].$SITE_NAME}</span></td>
</tr>
{/section} | Can anyone point me in the right direction please? |
|
Back to top |
|
joey Smarty Regular
Joined: 29 Oct 2003 Posts: 57
|
Posted: Mon Jan 10, 2005 3:23 am Post subject: a big mistake in your code |
|
|
you haven't assign the "$SITE_SECTION" to the tpl , how to get the right result in your tpl.
I think the correct answer is :
Code: |
$row = mysql_fetch_array($result);
$smarty->assign('SITE_SECTION',$row);
|
_________________ Joey
Senior Developer & Designer
-------------------------------------------
http://www.propagator.net
[SourceForge Project] Formcat with Smarty!
http://www.propagator.net/formcat/ |
|
Back to top |
|
kilburn Smarty Rookie
Joined: 08 Oct 2004 Posts: 21
|
Posted: Mon Jan 10, 2005 11:54 am Post subject: |
|
|
Code: | while ($row = myqsl_fetch_assoc($result))
$rows[] = $row;
$smarty->assign('rows',$rows); |
and then (I prefer foreach over section )
Code: | {foreach from=$rows item=row}
<tr>
<td width="90%" colspan="2"><span class="sitename">{$row.site_name}</span></td>
</tr>
{/foreach} |
Wow! |
|
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
|