View previous topic :: View next topic |
Author |
Message |
cellers Smarty Rookie
Joined: 16 Dec 2003 Posts: 17 Location: UK
|
Posted: Tue Dec 16, 2003 12:10 pm Post subject: viewing multi-dimensional array with nested section loops |
|
|
Can anyone tell me what's wrong with the following code? It worked up till recently and I can't see what's changed. Although the contents array contains data when its assigned from the php file, only empty spaces are printed in the table.
Code: | {* Display Data *}
{section name=record loop=$contents}
<tr bgcolor='#B9DCFF'>
{section name=entry loop=$contents[record]}
<td align='center'>{$contents[record][entry]}</td>
{/section}
</tr>
{/section}
|
$contents is assigned from my php file using
Code: | $smarty->assign('contents', $contents); |
and shows the correct data when I print the array from my php file using
Thanks. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Dec 16, 2003 1:29 pm Post subject: |
|
|
maybe the keys in your $contents are not numeric. show us a dump of the array if you like. |
|
Back to top |
|
cellers Smarty Rookie
Joined: 16 Dec 2003 Posts: 17 Location: UK
|
Posted: Tue Dec 16, 2003 2:01 pm Post subject: |
|
|
Code: | Array
(
[0] => Array
(
[NAME] => Trevor Eve
[ADDRESS] => The Campsite Manchester
[POSTCODE] => ewrw
[TEL] => 23432424
[TIMESTAMP] => 27-NOV-03
)
[1] => Array
(
[NAME] => a
[ADDRESS] => b
[POSTCODE] => c
[TEL] => 32423
[TIMESTAMP] => 01-DEC-03
)
[2] => Array
(
[NAME] => dsfds
[ADDRESS] => sdfdsfds
[POSTCODE] => sdfds
[TEL] => sdfsd
[TIMESTAMP] => 01-DEC-03
)
[3] => Array
(
[NAME] => Bob
[ADDRESS] => uyuy
[POSTCODE] => uyu
[TEL] => uyyuy
[TIMESTAMP] => 01-DEC-03
)
) |
Cheers. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Dec 16, 2003 2:09 pm Post subject: |
|
|
the outer array works with section (the keys are 0,1,2,3...) but the inner array is a hash-array, you cannot loop with section over that, you need foreach:
Code: |
{* Display Data *}
{section name=record loop=$contents}
<tr bgcolor='#B9DCFF'>
{foreach from=$contents[record] item=entry key=name}
<td align='center'>{$name} is {$entry}</td>
{/foreach}
</tr>
{/section}
|
see http://smarty.php.net/manual/en/language.function.foreach.php for more info |
|
Back to top |
|
cellers Smarty Rookie
Joined: 16 Dec 2003 Posts: 17 Location: UK
|
Posted: Tue Dec 16, 2003 2:22 pm Post subject: |
|
|
Thanks - that works!!!
Although oddly enough the other method has suddenly started working again as well, for no reason I can see.
Perplexed!
|
|
Back to top |
|
cellers Smarty Rookie
Joined: 16 Dec 2003 Posts: 17 Location: UK
|
Posted: Tue Dec 16, 2003 3:17 pm Post subject: its broken again... |
|
|
Now I know why I stopped working before. Even the code you gave me no longer works!
I have replaced the single color table
Code: | {* Display Data *}
{section name=record loop=$contents}
<tr bgcolor='#B9DCFF'>
{foreach from=$contents[record] item=entry}
<td align='center'>{$entry}</td>
{/foreach}
</tr>
{/section} |
with one using cycling colours
Code: | {* Display Data *}
{section name=record loop=$contents}
<tr bgcolor="{cycle values="#eeeeee,#d0d0d0"}"> {* CHANGE HERE *}
{foreach from=$contents[record] item=entry}
<td align='center'>{$entry}</td>
{/foreach}
</tr>
{/section} |
and now I get empty data again, even puting the code back to the way it was above. Something odd here. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Dec 16, 2003 4:57 pm Post subject: |
|
|
first put {$contents|@debug_print_var} in your template to make sure the data you passed looks reasonable. |
|
Back to top |
|
cellers Smarty Rookie
Joined: 16 Dec 2003 Posts: 17 Location: UK
|
Posted: Wed Dec 17, 2003 11:15 am Post subject: |
|
|
I did that, and the data was there yet nothing was being displayed in the table.
Also, the data in the array was as if it had been collected from the Oracle database by column, not by row. I checked contents array from the php file and somehow between being passed to the template, the data in the array had been rearranged.
ie, instead of
Code: | Array (2)
0 => Array (2)
NAME => Trevor Eve
TEL => Bob
1=> Array (2)
NAME => Trevor Eve
TEL => Bob |
it displayed
Code: | Array (2)
NAME => Array (2)
0 => Trevor Eve
1 => Bob
TEL => Array (2)
0 => Trevor Eve
1 => Bob |
However, when I was messing around with the php and tpl files the table suddenly appeared correctly - and the array data displayed from the tpl file was correctly displayed as from the php file.
Great. So I saved the two files in a safe directory.
Then I once again added the
Code: | <tr bgcolor="{cycle values="#eeeeee,#d0d0d0"}"> |
line to the template and lo and behold the table data was no longer being displayed, and the data from contents was again rearranged!!!
Finally, I copied the saved files from the SAFE directory back into my work directories... and the problem is still there!!!
How odd is that?
Hope I've described the problem well enough. Thanks. |
|
Back to top |
|
Duncan Smarty Pro
Joined: 16 Dec 2003 Posts: 166
|
Posted: Wed Dec 17, 2003 3:43 pm Post subject: |
|
|
Actually this sounds like a caching issue. You should try to SHIFT+reload the page to make IE not use the cache or add headers to the page to avoid it from being cached. |
|
Back to top |
|
cellers Smarty Rookie
Joined: 16 Dec 2003 Posts: 17 Location: UK
|
Posted: Wed Dec 17, 2003 7:11 pm Post subject: |
|
|
I was thinking that, but couldn't see what I could do about it. When I copied the files, giving them new names, then added the cycle line to the new file it worked just fine.
How do I shift+Reload with IE? Is that the same as shift and clicking the Refresh button?
Do you know how to add headers to the page to avoid it from being cached? Is that HTML?
Cheers. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Dec 17, 2003 7:23 pm Post subject: |
|
|
There is a client option in IE that defaults to the worse possible choice for the benefit of slow-connections -- Tools/Internet Options/General, Tempory Internet Files/Settings..., choose Never instead of Automatic.
I think there is a cache pragma for this, but off-hand I can't recall, sorry. |
|
Back to top |
|
Duncan Smarty Pro
Joined: 16 Dec 2003 Posts: 166
|
Posted: Wed Dec 17, 2003 11:36 pm Post subject: |
|
|
cellers wrote: | How do I shift+Reload with IE? Is that the same as shift and clicking the Refresh button? |
Ah yeah, that's what I meant. I have a different language installed here, so the term "reload" was more a guess.
cellers wrote: | Do you know how to add headers to the page to avoid it from being cached? Is that HTML? |
Most forum scripts simply add this in the header part of the HTML:
Code: |
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="no-cache">
<meta http-equiv="Expires" content="-1">
<meta http-equiv="Cache-Control" content="no-cache">
|
|
|
Back to top |
|
Duncan Smarty Pro
Joined: 16 Dec 2003 Posts: 166
|
Posted: Wed Dec 17, 2003 11:39 pm Post subject: |
|
|
...and I wouldn't rely on changing the default caching value in IE as it will mean to work properly on your end, but as soon as you put the script online you might get complaints that other users get the cached site presented.
That's why it's usually best to make sure the page itself won't be cached in any major browser - if you check via google on this, then you'll find plenty of info on how to prevent browser caching via HTML or php headers. |
|
Back to top |
|
cellers Smarty Rookie
Joined: 16 Dec 2003 Posts: 17 Location: UK
|
Posted: Thu Dec 18, 2003 9:55 am Post subject: |
|
|
Thanks a lot for that! Excellent! |
|
Back to top |
|
cellers Smarty Rookie
Joined: 16 Dec 2003 Posts: 17 Location: UK
|
Posted: Thu Dec 18, 2003 10:18 am Post subject: |
|
|
Yup, I can now update the tpl file and the updates show when I click Refresh.
Way hay!
Many thanks. |
|
Back to top |
|
|