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_fetch_array

 
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
bobbonew
Smarty Rookie


Joined: 28 Sep 2003
Posts: 8

PostPosted: Sun Sep 28, 2003 2:26 pm    Post subject: mysql_fetch_array Reply with quote

Hey everyone, i'm extremely new to smarty (installed it yesterday) and I know alot about php so i'm following the smarty syntax pretty well. One question I have, is how would I use the mysql_fetch_array like an example below where I want to have three rows displayed. I only know of assigning "stable" smarty variables like

$smarty->assign('name','booboo');

Please help, thx.

$sql = "SELECT * FROM posts ORDER BY post_id desc LIMIT 3 ";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
print $row['body';
}
Back to top
View user's profile Send private message
purephase
Smarty Rookie


Joined: 29 Sep 2003
Posts: 8

PostPosted: Tue Sep 30, 2003 8:38 pm    Post subject: Re: mysql_fetch_array Reply with quote

bobbonew wrote:

$sql = "SELECT * FROM posts ORDER BY post_id desc LIMIT 3 ";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
print $row['body';
}


I've come close to figuring this out, but not quite. This sucessfully retrieves the correct data, but you cannot append to existing arrays (at least as far I've seen so far) so my $smarty->assign in the while loop is incorrect. The $vars ($surname etc.) are created when the extract($row) is run. There are corresponding columns in the table.

Here's what I have:

Code:

query.php

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
extract($row);

// create associative array.
$smarty->assign('results', array($n => array('Surname' => $Surname,'FirstName' => $FirstName,'Position' => $Position,'Extension' => Extension,'Room' => $Room,'Current_Email_Address' => $Current_Email_Address)));

$n = $n + 1;
}

$smarty->display('query.tpl');

query.tpl

{foreach name=outer item=row from=$results}
{foreach name=inner key=key item=item from=$row}
<td><div>{$item}</div></td>
{/foreach}
{/foreach}



This returns results, just the last record. So I'm assuming it just overwrites "results" each time with a different nested array $n.

I guess my question is, is there a way to grow associative arrays? Or append to them? I've only been using Smarty since yesterday, and I did try to find other posts about this, but to no avail. Any help would be appreciated.
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