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

using foreach for mysql..

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


Joined: 24 Oct 2003
Posts: 5

PostPosted: Fri Oct 24, 2003 7:22 pm    Post subject: using foreach for mysql.. Reply with quote

i have been gettin errors... when i tried to produce the results into a tpl .. using foreach..

the below is the coding.. what's wrong with it?
please help...

----news.tpl-----
{foreach item="news" from=$news_list}
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" width="100%">
<tr><td>No: {$news.id} </td>
<td>Title: {$news.title} </td></tr><tr>
<td colspan="2">Posted by: {$news.author} on {$news.timestamp}</td>
</tr><tr><td colspan="2"><br>{$news.body}</td></tr></table>
{/foreach}

---news.php----
global $tpl;
$news = "SELECT * FROM news LIMIT 5";
$news = mysql_query($news);
$tpl->assign("news_list","$news");

ps: i only copied part of the code that consist of the news... so dont worry about the "$tpl = new smarty" and stuff like that.. as i am very sure it worked.. Smile
the results would come out as "r"
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Oct 24, 2003 11:03 pm    Post subject: Reply with quote

I think the problem is the way you assign the array:

$tpl->assign("news_list", $news);

in your code, $news is in double quotes, so what gets assigned to news_list (by php) is actually the literal string "Array".

You may want to use the Smarty debug console which will help you verify the values of variables passed to the template.
Back to top
View user's profile Send private message
xavior
Smarty Rookie


Joined: 24 Oct 2003
Posts: 5

PostPosted: Fri Oct 24, 2003 11:55 pm    Post subject: Reply with quote

hi boots,
i tried removing the quotes..
but it does not work..
and now.. it does not even have the "r"
LOL
anyways.. how do you use the debugging console??
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat Oct 25, 2003 12:33 am    Post subject: Reply with quote

{debug}

or

$smarty->debugging = true;

See: Chapter 10. Debugging Console

---

The other thing is that you aren't actually building a result array when you use mysql_query() -- you also need to fetch the results in an array (say with mysql_fetch_assoc() for example) but I wasn't sure if that is a detail that you are omitting.
Back to top
View user's profile Send private message
xavior
Smarty Rookie


Joined: 24 Oct 2003
Posts: 5

PostPosted: Sat Oct 25, 2003 11:43 am    Post subject: Reply with quote

i got an error running the debugging console thou..
somehow... i think i missed a step or two.. not sure... would be readin the guide again..

in any case... in your manual there's this file called mysql.class.php.
may i know where i could download it? (well... that file would solve this problem that i am having quickly.. heh.. *grins*)
Back to top
View user's profile Send private message
xavior
Smarty Rookie


Joined: 24 Oct 2003
Posts: 5

PostPosted: Sat Oct 25, 2003 2:05 pm    Post subject: Reply with quote

erm.. booty, thanks for your help..
i solved the problem...
added the mysql_fetch_row command and remade the arrays...
Smile
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