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

3-dimensional 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
unknewn
Smarty n00b


Joined: 21 Nov 2003
Posts: 3

PostPosted: Fri Nov 21, 2003 9:05 am    Post subject: 3-dimensional array Reply with quote

What i am trying to do is a news system where the two last days are shown and all the articles in that day. ie:


Friday 21st November 2003
- title of post
content in this post
- title of second post
content in the second post

Thursday 20th November 2003
- title of post
content in this post
- title of second post
content in the second post

What i had as an array was the following:

[php:1:a486ccf24b]<?php
$smarty->assign("news", array(array("date" => "tstamp", "data" => array(array("title" => "Title 1", "content" => "Content Data 1", "author" => "John"),
array("title" => "Title 2", "content" => "Content Data 2", "author" => "John Doe"),
array("title" => "Title 3", "content" => "Content Data 3", "author" => "John Smith"))
)));
?>[/php:1:a486ccf24b]

Not sure if this is right, i could not make a section loop that would do this properly. So what i need is the right smarty code to do this, any help would be appreciated
Back to top
View user's profile Send private message
Enquest
Smarty Regular


Joined: 14 May 2003
Posts: 79

PostPosted: Fri Nov 21, 2003 9:53 am    Post subject: Reply with quote

First of all Boots and Messju ... Will say use $smarty->assign_by_ref(...) this doesn't copy the variable and gives you some edge on speed.

Second. How I do this.

I pull my data out of the database. (MySql) In that mysql wich is in a lib. function I limit the amount of data. You can do that dynamicaly passing some date's to your funciton... What ever pull the data out of your mysql

$query = ...
$result = ...

then I make a while loop and in that while I assign each cycle to a array. Like this
while ($res = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
$ret = $res
}
return $ret;

the result is that the two last dates are in an array

now I only have to assign it to smarty!
$smarty->assign_by_ref('data', $data);

In my tpl I make a make then
{section name=dat loop=data}
{$data[dat].title}<br>
{$data[dat].text} ...

{/section}

Volà hoped it helped!
_________________
-----------------------
Learning my self by trying to help others
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Fri Nov 21, 2003 11:28 am    Post subject: Reply with quote

Enquest wrote:
First of all Boots and Messju ... Will say use $smarty->assign_by_ref(...) this doesn't copy the variable and gives you some edge on speed.


nope, it won't. php does copy on write. so it gives you only speed if you modifiy the assigned array in the template, which is unlikely.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Enquest
Smarty Regular


Joined: 14 May 2003
Posts: 79

PostPosted: Fri Nov 21, 2003 12:09 pm    Post subject: Reply with quote

Can you explain me then exactly when to use $smarty->assign_by_ref

Because now I'm confused!

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


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

PostPosted: Fri Nov 21, 2003 12:23 pm    Post subject: Reply with quote

php's references are confusing. they are *very* confusing, IMHO.
that's why they suck.

you assign by ref if you want any modifications that are done to the variable be done on the original variable. you rarely need it because you normaly don't (should not) do any modifications of the data in your templates.

an example where i almost always use assign_by_ref() are objects. object's methods *may* be called during display. if so they *may* change the object's internal state and if so it's likely i want these changes be done on the object i have after display() returned.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Enquest
Smarty Regular


Joined: 14 May 2003
Posts: 79

PostPosted: Fri Nov 21, 2003 12:48 pm    Post subject: Reply with quote

I could swear I read some where on this forum that its beter to use assign_by_ref then assign because then the var isn't copyied and you had some speed advancment. It did take less memory of the server also.

Small question then. Because I made now my site wit assign_by_ref should I change it back? to just assign?
_________________
-----------------------
Learning my self by trying to help others
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Fri Nov 21, 2003 1:20 pm    Post subject: Reply with quote

Enquest wrote:
I could swear I read some where on this forum that its beter to use assign_by_ref then assign because then the var isn't copyied and you had some speed advancment. It did take less memory of the server also.


that's possible. i didn't know that myself for too long Smile

Quote:
Small question then. Because I made now my site wit assign_by_ref should I change it back? to just assign?


if it works good i wouldn't bother.
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: Fri Nov 21, 2003 6:20 pm    Post subject: Reply with quote

FWIW, my understanding is that assign_by_ref will indeed be faster for most objects. Mainly since it means that the object doesn't need to be copied during the assign. This is not always true for arrays, but often is if the array is not small and particularly if it contains objects or other arrays. Simple types are definately faster using a normal assign.

YMMV
Back to top
View user's profile Send private message
unknewn
Smarty n00b


Joined: 21 Nov 2003
Posts: 3

PostPosted: Sat Nov 22, 2003 2:46 am    Post subject: Reply with quote

ok.. well all that conversation.. and it didnt really help me much on my way..
Back to top
View user's profile Send private message
unknewn
Smarty n00b


Joined: 21 Nov 2003
Posts: 3

PostPosted: Sat Nov 22, 2003 7:03 am    Post subject: Reply with quote

Ok, i managed to finally get it (yay) i used the following to do so for later reference if need be.

Code:

{section name=outer loop=$news}
{$news[outer].date}<br>
   {section name=inner loop=$news[outer].data}
      {$news[outer].data[inner].title}, {$news[outer].data[inner].content}, {$news[outer].data[inner].author}<br>
   {/section}
{/section}


and the following assignment.

[php:1:928569b1f5]<?php
$smarty->assign("news", array(array("date" => "tstamp", "data" => array(array("title" => "Title 1", "content" => "Content Data 1", "author" => "John"),
array("title" => "Title 2", "content" => "Content Data 2", "author" => "John Doe"),
array("title" => "Title 3", "content" => "Content Data 3", "author" => "John Smith"))
)));
?>[/php:1:928569b1f5]

From this you should be able to conclude the rest..
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 -> 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