Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

viewing multi-dimensional array with nested section loops
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
cellers
Smarty Rookie


Joined: 16 Dec 2003
Posts: 17
Location: UK

PostPosted: Tue Dec 16, 2003 12:10 pm    Post subject: viewing multi-dimensional array with nested section loops Reply with quote

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

Code:
print_r($contents)


Thanks.
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Tue Dec 16, 2003 1:29 pm    Post subject: Reply with quote

maybe the keys in your $contents are not numeric. show us a dump of the array if you like.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
cellers
Smarty Rookie


Joined: 16 Dec 2003
Posts: 17
Location: UK

PostPosted: Tue Dec 16, 2003 2:01 pm    Post subject: Reply with quote

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
View user's profile Send private message
messju
Administrator


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

PostPosted: Tue Dec 16, 2003 2:09 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
cellers
Smarty Rookie


Joined: 16 Dec 2003
Posts: 17
Location: UK

PostPosted: Tue Dec 16, 2003 2:22 pm    Post subject: Reply with quote

Thanks - that works!!!

Although oddly enough the other method has suddenly started working again as well, for no reason I can see.

Perplexed!

Confused
Back to top
View user's profile Send private message
cellers
Smarty Rookie


Joined: 16 Dec 2003
Posts: 17
Location: UK

PostPosted: Tue Dec 16, 2003 3:17 pm    Post subject: its broken again... Reply with quote

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
View user's profile Send private message
messju
Administrator


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

PostPosted: Tue Dec 16, 2003 4:57 pm    Post subject: Reply with quote

first put {$contents|@debug_print_var} in your template to make sure the data you passed looks reasonable.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
cellers
Smarty Rookie


Joined: 16 Dec 2003
Posts: 17
Location: UK

PostPosted: Wed Dec 17, 2003 11:15 am    Post subject: Reply with quote

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
View user's profile Send private message
Duncan
Smarty Pro


Joined: 16 Dec 2003
Posts: 166

PostPosted: Wed Dec 17, 2003 3:43 pm    Post subject: Reply with quote

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
View user's profile Send private message
cellers
Smarty Rookie


Joined: 16 Dec 2003
Posts: 17
Location: UK

PostPosted: Wed Dec 17, 2003 7:11 pm    Post subject: Reply with quote

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
View user's profile Send private message
boots
Administrator


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

PostPosted: Wed Dec 17, 2003 7:23 pm    Post subject: Reply with quote

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
View user's profile Send private message
Duncan
Smarty Pro


Joined: 16 Dec 2003
Posts: 166

PostPosted: Wed Dec 17, 2003 11:36 pm    Post subject: Reply with quote

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
View user's profile Send private message
Duncan
Smarty Pro


Joined: 16 Dec 2003
Posts: 166

PostPosted: Wed Dec 17, 2003 11:39 pm    Post subject: Reply with quote

...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
View user's profile Send private message
cellers
Smarty Rookie


Joined: 16 Dec 2003
Posts: 17
Location: UK

PostPosted: Thu Dec 18, 2003 9:55 am    Post subject: Reply with quote

Thanks a lot for that! Excellent! Very Happy
Back to top
View user's profile Send private message
cellers
Smarty Rookie


Joined: 16 Dec 2003
Posts: 17
Location: UK

PostPosted: Thu Dec 18, 2003 10:18 am    Post subject: Reply with quote

Yup, I can now update the tpl file and the updates show when I click Refresh.

Way hay! Razz

Many thanks.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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