|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
johannes Smarty Regular
Joined: 10 May 2003 Posts: 85 Location: Malmö, Sweden
|
Posted: Mon Jun 23, 2003 8:20 pm Post subject: big array in array problem |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Jul 18, 2003 5:12 pm Post subject: |
|
|
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 |
|
johannes Smarty Regular
Joined: 10 May 2003 Posts: 85 Location: Malmö, Sweden
|
Posted: Fri Jul 18, 2003 6:45 pm Post subject: Result of the code |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Jul 19, 2003 3:02 am Post subject: Re: Result of the code |
|
|
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
HTH |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Jul 19, 2003 6:45 pm Post subject: |
|
|
Also look here for a similar issue and resolution. |
|
Back to top |
|
johannes Smarty Regular
Joined: 10 May 2003 Posts: 85 Location: Malmö, Sweden
|
Posted: Wed Jul 23, 2003 4:31 pm Post subject: Many thanks |
|
|
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 |
|
|
|
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
|