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

Assoc arrays: Why doesn´t this work?

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


Joined: 23 Aug 2004
Posts: 7

PostPosted: Tue Aug 24, 2004 3:16 pm    Post subject: Assoc arrays: Why doesn´t this work? Reply with quote

php file:
[php:1:ac9c9e4f7d]
$menus = array("menu1" => array("News", "terms"), "menu2" => array("information", "rules"), "menu3" => array("Links"));

$smarty->assign('menus', $menus);
[/php:1:ac9c9e4f7d]

tpl file:
Code:

{foreach key=k item=value from=$menus}
<b>{$value[menu1]}</b>
{/foreach}


Why doesn´t this work?
It outputs only:
Quote:
array
array
array


Instead this I wanna get:
Quote:
News
terms
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Aug 24, 2004 3:57 pm    Post subject: Reply with quote

inside your foreach loop, $k will be the key 'menu1' and $value will be the array ('News','terms')

So one way to access the data:

Code:
{foreach key=k item=value from=$menus}
   key is {$k}
   array item 1 is {$value[0]}
   array item 2 is {$value[1]}
{/foreach}


Or, you could loop through your value array:

Code:
{foreach key=k item=value from=$menus}
   key is {$k}
   {foreach item="foobar" from=$value}
      item is {$foobar}
   {/foreach}
{/foreach}
Back to top
View user's profile Send private message Visit poster's website
macfreakz
Smarty Rookie


Joined: 23 Aug 2004
Posts: 7

PostPosted: Wed Aug 25, 2004 7:00 am    Post subject: Reply with quote

Thats not the solution!
But I found the right solution:

Code:
{foreach key=k item=value from=$menus[menu1]}
<b>{$value}</b>
{/foreach}


And I forgot to delete the compiled file which was used as cache file to open the page faster! I dont want to have this option. How can I disable the cache?
Back to top
View user's profile Send private message
dthought
Smarty Regular


Joined: 21 Apr 2003
Posts: 55
Location: Melbourne, Australia

PostPosted: Wed Aug 25, 2004 7:02 am    Post subject: Reply with quote

Normally if you just edit and resave a TPL file, Smarty picks up on it and recompiles it... only if something odd is going on will it not recompile.
Back to top
View user's profile Send private message Visit poster's website
macfreakz
Smarty Rookie


Joined: 23 Aug 2004
Posts: 7

PostPosted: Wed Aug 25, 2004 7:15 am    Post subject: Reply with quote

@dtought: oh yeah! I now understand why my files are not recompiled after changing the content: the templates are stored on the database.

I implemented this function:

[php:1:992921ce88]
function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
{
return true;
}
[/php:1:992921ce88]

Thats wrong! This function should return the update time of the content. I have to plan how to implement this function now ... Help anyone?
Back to top
View user's profile Send private message
macfreakz
Smarty Rookie


Joined: 23 Aug 2004
Posts: 7

PostPosted: Wed Aug 25, 2004 8:15 am    Post subject: Reply with quote

Uh, I have the solution with timestamp:

[php:1:5d69eca04b]
function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
{
global $db; // default db id

$sqlquery = "SHOW TABLE STATUS LIKE 'tbl_template'";
$tbl_info_result = mysql_query($sqlquery, $db) or die ("Error: ".mysql_error($db));
$tbl_info_data = mysql_fetch_array($tbl_info_result);

$tpl_timestamp = strtotime($tbl_info_data['Update_time']);

return true;
}
[/php:1:5d69eca04b]
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