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

Associative Arrays and Sections

 
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 -> Smarty Development
View previous topic :: View next topic  
Author Message
sgcet
Smarty Rookie


Joined: 09 Feb 2005
Posts: 13

PostPosted: Wed Feb 09, 2005 12:21 pm    Post subject: Associative Arrays and Sections Reply with quote

This is my first post.
it is a huge forum.Many thanks to all Moderators and contributors
Hi All,
This is my section

[’releases’] => Array
(
[0] => Array
(
[ausprice] => 149.95
[author] => James Gonzalez
[category] => web,
[date_released] => 2004-10-26
[book] => searchengineoptimization
[new] => Yes
[numbermovies] => 92
[price] => 99.95


)
[0] => Array
(
[ausprice] => 149.95
[author] => James Gonzalez
[category] => web,
[date_released] => 2004-10-26
[book] => searchengineoptimization
[new] => Yes
[numbermovies] => 92
[price] => 99.95


) and so on


This is my section

PHP

$smarty->assign(’releases’, $releases);

TPL

How can i loop thru to print only author and foldername

{section name=’releases’ loop=$’releases’}
Author:{$releases[?].author }<br>
Book: {$releases[?].book}<br>
<p>
{/section}

I am outputting nothing. Do i hav to go one level deeper inside the array?

What to put in the index section?

Pls help me on this

Thanx for all
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Wed Feb 09, 2005 12:34 pm    Post subject: Re: Associative Arrays and Sections Reply with quote

sgcet wrote:

Code:

{section name=’releases’ loop=$’releases’}
Author:{$releases[?].author }<br>
Book:  {$releases[?].book}<br>
<p>
{/section}



I think that your $loop parameter is incorrect -- try $releases (no quotes). Within your section, you use the name of the section to refer to the current index, eg: {$releases[releases].author}

That said, you are likely going to find it much easier to use a foreach loop:

Code:

{foreach from $releases item=release}
Author:{$release.author}<br>
Book:  {$release.book}<br>
<p>
{/foreach}


HTH
Back to top
View user's profile Send private message
sgcet
Smarty Rookie


Joined: 09 Feb 2005
Posts: 13

PostPosted: Wed Feb 09, 2005 12:56 pm    Post subject: Reply with quote

Hi Boots,


Thanks for uer prompt reply. I mispalced the quotes in the post only.
The code that u put will not go down How can i go the required no of levels to fetch the author and book.

{foreach from= $releases item=release}
{foreach from= $releases item=release}
Author:{$release.author}<br>
Book: {$release.book}<br>
<p>
{/foreach}
{/foreach}

I am new to smarty.
Pls post your thoughts

Thanks
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Wed Feb 09, 2005 1:46 pm    Post subject: Reply with quote

Well, for one thing you should only need one foreach loop for the array you suggest. I am assuming that you have an array ($releases) which is an array of records -- that means that each item in $releases is itself an array and in those record arrays -- ie in PHP you should be able to do the following:

echo $releases[0]['author']; // should be 'James Gonzalez'
echo $releases[1]['author']; // should be the next author...

If not, then your array is quite what I was expecting...
Back to top
View user's profile Send private message
sgcet
Smarty Rookie


Joined: 09 Feb 2005
Posts: 13

PostPosted: Thu Feb 10, 2005 8:54 am    Post subject: Found out- simple Reply with quote

Hi boots,

as i had to just select the result set as ->
[php:1:be05e88624]<?php
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC))
$results[] = $row;
return $results;
?>[/php:1:be05e88624]
and then just pass this to smarty and then thier i had to loop thru the smarty's foreach fn to display I used foreach as u suggested thanks

[php:1:be05e88624]<?php
<table bgcolor="#ffffff" cellpadding="4" cellspacing="1" width="100%">{foreach from= $releases item=release}
{foreach key=key from=$release item=rel}
{if $key eq "doorwaypage"}
<tr>
<td valign=top bgcolor='#eeeeee'>-</td>
<td valign=top bgcolor='#eeeeee'>
<a href="{$rel}">
{elseif $key eq "onlinetitle"}
<u>{$rel}</u></a></td>
</tr>
{/if}
{/foreach}
{/foreach}
</td>
</table>
?>[/php:1:be05e88624]

Thanks
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 -> Smarty Development 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