View previous topic :: View next topic |
Author |
Message |
fyfe Smarty n00b
Joined: 23 Jun 2003 Posts: 3 Location: Scotland
|
Posted: Mon Jun 23, 2003 2:39 pm Post subject: Accessing an array of objects |
|
|
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 |
|
mischa Smarty Rookie
Joined: 30 Apr 2003 Posts: 19 Location: Suhr, Switzerland
|
Posted: Mon Jun 23, 2003 4:30 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Jun 23, 2003 4:34 pm Post subject: |
|
|
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 |
|
fyfe Smarty n00b
Joined: 23 Jun 2003 Posts: 3 Location: Scotland
|
Posted: Mon Jun 23, 2003 10:07 pm Post subject: |
|
|
Thanks
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 |
|
Enquest Smarty Regular
Joined: 14 May 2003 Posts: 79
|
Posted: Wed Jun 25, 2003 3:03 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Jun 25, 2003 3:56 pm Post subject: |
|
|
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 |
|
fyfe Smarty n00b
Joined: 23 Jun 2003 Posts: 3 Location: Scotland
|
Posted: Wed Jun 25, 2003 4:44 pm Post subject: |
|
|
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 |
|
mpk Smarty Rookie
Joined: 26 Jun 2003 Posts: 9 Location: Amsterdam, Netherlands
|
Posted: Wed Jul 02, 2003 8:43 am Post subject: |
|
|
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 |
|
|