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

big array in array problem

 
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
johannes
Smarty Regular


Joined: 10 May 2003
Posts: 85
Location: Malmö, Sweden

PostPosted: Mon Jun 23, 2003 8:20 pm    Post subject: big array in array problem Reply with quote

hello, I still have this array problem... Phuuu. So, could anyone please help me out and explain how too solve this. thank you all!! I cant get Smarty to make an array in two diminsions and then too loop it all out i correct way - sorry for bad spelling!!

This in what I want

Head
- level2
- level2

Head
-level2

my code that wont work...


file: nav_load.php
//Search for the Head 'outmainname'
$sql = "select * from junior_out_main";

$result = $db->query($sql);

$items = array ();
$i = 0;

while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
$itemsTmp = array (
'outmainid' => $row['outmainid'],
'outmainname' => $row['outmainname'],
'outmainurl' => $row['outmainurl']
);

$sql2 = "select * from junior_out_levelone where outmainid=$itemsTmp[outmainid]";
$result2 = $db->query($sql2);
$items2[] = array();
$j = 0;

while ($row2 = $result2->fetchRow(DB_FETCHMODE_ASSOC)) {
$itemslevel2Tmp = array (
'outlevelonename' => $row2['outlevelonename'],
'outleveloneurl' => $row2['outleveloneurl']);
$items2[$i][$j] = $itemslevel2Tmp;
$j++;
}
}

$items[$i] = $itemsTmp;
$i++;

}


//assign the two arrays too Smarty tpl
$smarty->assign("items", $items);
$smarty->assign("items2", $items2);


tpl code (for nav).

{section name=sec1 loop=$items}
{$items[sec1].outmainname}<br>

{section name=sec2 loop=$items2}
{$items2[sec1][sec2].outlevelonename}<br>
{/section}

{/section}
_________________
--------------------------
nice culture on the Internet
www.poeter.se
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 Jul 18, 2003 5:12 pm    Post subject: Reply with quote

can you provide a sample of the results of print_r($items) and print_r($items2)?

Also, as a general note to all posters: when posting code samples, please use the Code, PHP and Quote features of the forum. It would be much easier to read your code if it was properly formatted (ie. using regular indentation) and highlighted. Thanks!
Back to top
View user's profile Send private message
johannes
Smarty Regular


Joined: 10 May 2003
Posts: 85
Location: Malmö, Sweden

PostPosted: Fri Jul 18, 2003 6:45 pm    Post subject: Result of the code Reply with quote

Hi and an bunch of thanks!!!

Well, the thing is that if this is working, one lever under each head.

head
-level2
head
-level2
head

but... this is not working, the second lever2 wont show.

head
-level2
-level2
-level2
head

If I loop one level2 under each head the page will bee ok but if i try to loop out more level2 under one head i wont show out.

question: is it better too make one array? insted of two like, rigth now i assign two different array as you can see. Again thanks alot for taking time too this!!!!
_________________
--------------------------
nice culture on the Internet
www.poeter.se
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: Sat Jul 19, 2003 3:02 am    Post subject: Re: Result of the code Reply with quote

Hi johannes.

johannes wrote:
question: is it better too make one array? insted of two like, rigth now i assign two different array as you can see.


It depends on what you need to do, but from what your example suggests, an associative array of arrays is probably simplest. You would build an array that had a structure like:

[php:1:bf2b2dba46]$test = array('head'=>array('level1', 'level2'), 'body'=>array('level3', 'level4'));
$smarty->assign('test', $test);[/php:1:bf2b2dba46]

In your template, you would use two loops:

Code:
{foreach key=head item=items loop=$test}
  <h2>{$head}</h2>
  {foreach item=label loop=$items}
    <div>{$label}</div>
  {/foreach}
{/foreach}


I didn't test this, but it should work--at least, I hope you can see the general idea behind it Wink

HTH
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Sat Jul 19, 2003 6:45 pm    Post subject: Reply with quote

Also look here for a similar issue and resolution.
Back to top
View user's profile Send private message
johannes
Smarty Regular


Joined: 10 May 2003
Posts: 85
Location: Malmö, Sweden

PostPosted: Wed Jul 23, 2003 4:31 pm    Post subject: Many thanks Reply with quote

Junst need too say - thank you for the awnser, I just sorted all out!! Great.
_________________
--------------------------
nice culture on the Internet
www.poeter.se
Back to top
View user's profile Send private message Send e-mail 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