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

Worried about memory and other things... Is there a solution

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


Joined: 26 Aug 2003
Posts: 32

PostPosted: Mon Sep 08, 2003 10:15 am    Post subject: Worried about memory and other things... Is there a solution Reply with quote

Hello,

I am a bit concerned abotu one aspect of Smarty and I'm not really sure what to do here...

Basically I am concerned that for large database queries that return hundreds/thousands of results that having to build that array in memory then have Smarty iterate over the array will consume substantially more memory then something like this:

Code:

$res=db_quer("SELECT SOME BIG RESULTS");
while($rec=db_fetch_assoc($res))
{
  print $rec['somefield'];
  print $rec['somefield2'];

}


Is there any intuitive way around this that does involve writing PHP code in the Smarty template?... I really want as much of my code (particularly database code) to sit outside of the template...

Thanks!

- Greg
Back to top
View user's profile Send private message Visit poster's website
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Mon Sep 08, 2003 10:45 am    Post subject: Reply with quote

Perhaps you could write a custom block plugin which is able to iterate over you database result... Confused
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Mon Sep 08, 2003 10:50 am    Post subject: Reply with quote

this is an issue if you really want to show hundreds/thousands of results on one webpage. but this is unlikely, not?

i think in nearly every case you can limit the results of the database to a subset of reasonable size that is needed for the template to display.

for example if you have 1000 elements to be paged over n-per-page with a template you don't need to assign all 1000 elements to the template, but just the number of the "active" page and the n elements for the "active" page.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


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

PostPosted: Mon Sep 08, 2003 12:49 pm    Post subject: Reply with quote

Yes, you WILL loop over the data at least twice; once to collect it, once to display it; however, you have additional opportunities now. For example, if the query results are shared by many templates, you can serialize or otherwise cache them.

Also, when assigning to smarty, make sure you use assign_by_ref(), at least for large objects/arrays.

Besides, the fetch and print paradigm is exactly what Smarty is there to defeat/replace since it ties your application's logic to your display logic which is often not what you really want anyhow.
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Mon Sep 08, 2003 5:41 pm    Post subject: Reply with quote

It occurs to me that you can use an assigned object that is used to retrieve the current record of interest to avoid the double iteration issue. Perhaps in conjunction with the fast recursion technique.

In other words, you application would set the object up, but it would be left to smarty to actually iterate the object.


Last edited by boots on Tue Sep 09, 2003 12:44 am; edited 1 time in total
Back to top
View user's profile Send private message
devnul
Smarty Rookie


Joined: 26 Aug 2003
Posts: 32

PostPosted: Mon Sep 08, 2003 11:39 pm    Post subject: Reply with quote

Of all of these i think the "LIMIT" method works best for me on this project (it seems to be the one which keeps the most code out of the template)....

Thanks guys and/or gals!

- Greg
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