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

Outputting Data from a MySql Query

 
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
david63
Smarty n00b


Joined: 06 Jan 2005
Posts: 3
Location: Lancashire - UK

PostPosted: Thu Jan 06, 2005 9:23 am    Post subject: Outputting Data from a MySql Query Reply with quote

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


Joined: 29 Oct 2003
Posts: 57

PostPosted: Mon Jan 10, 2005 3:23 am    Post subject: a big mistake in your code Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
kilburn
Smarty Rookie


Joined: 08 Oct 2004
Posts: 21

PostPosted: Mon Jan 10, 2005 11:54 am    Post subject: Reply with quote

Code:
while ($row = myqsl_fetch_assoc($result))
    $rows[] = $row;
$smarty->assign('rows',$rows);


and then (I prefer foreach over section Razz)

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