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

Iterate over result set without using an 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
CJNE
Smarty n00b


Joined: 30 Jan 2004
Posts: 3
Location: Sweden

PostPosted: Fri Jan 30, 2004 8:22 am    Post subject: Iterate over result set without using an array Reply with quote

Hello,
I'm pretty new with Smarty and i really like it's features. There is one thing though that i can't figure out how to do with smarty.

I want to iterate over a result set directly in a template, something like:
{foreach from=$resultSet item="row"}
{$row.col1} ...
{/foreach}

where $resultSet is a Pear DB result set.
I looked at doing a block plugin but i don't think that would work..?

Why do i want to do this? Well i just figured it would be more efficient to iterate the result set directly, it would eliminiate the creating of array and filling it up step.

Regards,
Johan Isacsson
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Jan 30, 2004 9:18 am    Post subject: Reply with quote

you can do it with a block-function and the $repeat-parameter.

but i don't think that it's much more efficient than assigning to an array and loop over that.

did you really measure the difference, or is it just a feeling that it's more efficient to loop in the template?
Back to top
View user's profile Send private message Send e-mail Visit poster's website
CJNE
Smarty n00b


Joined: 30 Jan 2004
Posts: 3
Location: Sweden

PostPosted: Fri Jan 30, 2004 9:58 am    Post subject: Reply with quote

I haven't measured performance of it but without considering any overhead added by doing it as a block-function (if any) it must be more efficient considering you don't have to do the array copy operation.
Don't know wether there is a noticeable difference in speed doing so but i imagine it might make some difference on hight traffic sites.

I got the impression that $content passed in to a block function is already parsed by smarty, so my {$col1} inside the block would not work, maybe i should read that section again...

Is it possible to make a block function be as efficient as or more efficient than section/foreach ?
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Jan 30, 2004 10:10 am    Post subject: Reply with quote

you save the array-copy operation, but you may occupy the database-result for a longer time, if you do it in the template (especially if you don't have output-buffering enabled). it really depends on the particular situation.

the $content is already parsed, yes. the trick is to assign the result and to let the contents be displayed on the next iteration of the block.
like this:
[php:1:856b68138b]
function smarty_block_db_foreach($params, $content, &$smarty, &$repeat) {
$smarty->assign($params['assign'], $row = next_row_from_db());
$repeat = (bool)$row; /* set to true if there was another row */
return $content; /* return contents of the previous iteration */
}
----
template:
{db_foreach assign=row} {* pass the variablename as assign-attribute *}
..{$row|@debug_print_var}
{/db_foreach}
[/php:1:856b68138b]
Back to top
View user's profile Send private message Send e-mail Visit poster's website
CJNE
Smarty n00b


Joined: 30 Jan 2004
Posts: 3
Location: Sweden

PostPosted: Fri Jan 30, 2004 10:56 am    Post subject: Reply with quote

thanks, i'll give it a try once i got apache working again (php5 on windows is a bit unstable Razz)
Back to top
View user's profile Send private message Visit poster's website
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