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

Accessing an array of objects

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


Joined: 23 Jun 2003
Posts: 3
Location: Scotland

PostPosted: Mon Jun 23, 2003 2:39 pm    Post subject: Accessing an array of objects Reply with quote

How do I pass an array of objects to smarty? At the moment I'm having to go through the array of objects and create an array of arrays in php like so:

[php:1:65032243d3]
// Returns an array of Film objects
$films = $objectStore->objectsWithRawSQLFilter( 'Film', $sql_query );

$films_array = array();
$total = count( $films );
if ( $total > 0 ) {
for ( $i = 0; $i < $total; $i++ ) {
$films_array[$i] = array(
'id' => $films[$i]->id(),
'title' => $films[$i]->title(),
[...Very Large Snip...]
'year' => $films[$i]->year()
);
}
}

$smarty->assign( 'films', $films_array );
[/php:1:65032243d3]
_________________
"These aren't stolen, they just haven't been payed for."
Bacon (Lock Stock and Two Smokin Barrels)
Back to top
View user's profile Send private message
mischa
Smarty Rookie


Joined: 30 Apr 2003
Posts: 19
Location: Suhr, Switzerland

PostPosted: Mon Jun 23, 2003 4:30 pm    Post subject: Reply with quote

Hi

You've access to objects within Smarty:

[php:1:c7b565a5de]
$smarty->assign('films',$films); // $films ->array of objects
[/php:1:c7b565a5de]

Template:
Code:

{foreach from=$films item=film}
  {$film->id()},{$film->title()},{$film->year()}
{/foreach}


I hope that helps!
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Jun 23, 2003 4:34 pm    Post subject: Reply with quote

btw, it's a good habit to assign object by reference.

[php:1:bce2423865]$smarty->assign_by_ref('films', $films);[/php:1:bce2423865]

Monte
Back to top
View user's profile Send private message Visit poster's website
fyfe
Smarty n00b


Joined: 23 Jun 2003
Posts: 3
Location: Scotland

PostPosted: Mon Jun 23, 2003 10:07 pm    Post subject: Reply with quote

Thanks Smile

I don't know what I was trying to do before but it just wouldn't work.
_________________
"These aren't stolen, they just haven't been payed for."
Bacon (Lock Stock and Two Smokin Barrels)
Back to top
View user's profile Send private message
Enquest
Smarty Regular


Joined: 14 May 2003
Posts: 79

PostPosted: Wed Jun 25, 2003 3:03 pm    Post subject: Reply with quote

Mohrt can you explain why its a good idee to asign by ref.

thanxs
_________________
-----------------------
Learning my self by trying to help others
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Jun 25, 2003 3:56 pm    Post subject: Reply with quote

If you assign an object normally, PHP4 makes an in-memory copy of the entire object. If you assign_by_ref(), then a reference to the original object is made which saves a substantial processing and memory overhead.

Monte
Back to top
View user's profile Send private message Visit poster's website
fyfe
Smarty n00b


Joined: 23 Jun 2003
Posts: 3
Location: Scotland

PostPosted: Wed Jun 25, 2003 4:44 pm    Post subject: Reply with quote

And in PHP5 it always asigns objects by ref.
_________________
"These aren't stolen, they just haven't been payed for."
Bacon (Lock Stock and Two Smokin Barrels)
Back to top
View user's profile Send private message
mpk
Smarty Rookie


Joined: 26 Jun 2003
Posts: 9
Location: Amsterdam, Netherlands

PostPosted: Wed Jul 02, 2003 8:43 am    Post subject: Reply with quote

May I make this a bit more difficult: I have an array of arrays of objects...

So my data structure is like this

Array ( [0] => Array ( [0] => film Object ( [filmID] => 001200306230815)
[1] => film Object ( [filmID] => 001200306230830 ) ) )
Array ( [1] => Array ( [0] => film Object ( [filmID] => 001200306230845 ) ))
Array ( )

I'm trying to print in template with something like this :
{foreach from=$allfilms item=filmcategory}
{foreach from=$filmcategory item=film}
{$film->filmID}
{/foreach}
{/foreach}

But it won't work.
Any ideas?
Back to top
View user's profile Send private message Send e-mail
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