|
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 |
rico Smarty n00b
Joined: 06 May 2008 Posts: 1
|
Posted: Tue May 06, 2008 11:06 am Post subject: problem with multi dimensional array |
|
|
Hi All,
I'm a newbie and finding a solution to display a multi dim. array.
Witch way is the best to display this?
here's the array:
Code: | $data = array (
0=> array
(
'name' => 'Rico',
'test' => 'test',
'rooms' => array( 0 => array('name' => 'triple room1','id' => '322'),
1 => array('name' => 'doddf room2','id' => '312344'),
)
),
1=> array
(
'name' => 'Rico2',
'test' => 'test2',
'rooms' => array( 0 => array('name' => 'single room1','id' => '23'),
1 => array('name' => 'do room2','id' => '344'),
)
)
); | [/code]
Thanks! |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue May 06, 2008 7:39 pm Post subject: |
|
|
Use a nested {foreach...}
Code: | {foreach item=thisitem from=$data}
{$thisitem.name} {$thisitem.test}
{foreach item=thisroom from=$thisitem.rooms}
{$thisroom.name} {$thisroom.id}
{/foreach}
{/foreach}
|
as an example |
|
Back to top |
|
JonZenor Smarty Rookie
Joined: 03 Jun 2008 Posts: 5
|
Posted: Tue Jun 03, 2008 9:14 am Post subject: |
|
|
I don't know why I can't get this...
What I'm trying to do is load multiple links under each category that i have. Here is the array setup:
php
Code: |
$rs = mysql_query("SELECT * FROM ".$db_prefix."links_cat ORDER BY cat_pos");
while($row = mysql_fetch_array($rs))
{
$cat_name[] = $row['cat_name'];
$cat_id[] = $row['cat_id'];
$thisid = $row['cat_id']; // for testing purposes only
$rs2 = mysql_query("SELECT * FROM ".$db_prefix."links WHERE link_cat = '$cat_id' ORDER BY link_name");
while($row2 = mysql_fetch_array($rs2))
{
$link_addr[$row['cat_id']][] = $row2['link_addr'];
$link_name[$row['cat_id']][] = $row2['link_name'];
};
// Some test info to make sure the values are populated
$link_addr[$thisid][] = "ZenorSoft";
$link_name[$thisid][] = "ZenorSoft Web Design";
};
$smarty->assign("catid", $cat_id);
$smarty->assign("catname", $cat_name);
$smarty->assign("linkname", $link_name);
$smarty->assign("linkaddr", $link_addr);
|
tpl
Code: |
{section name=Category loop=$catid}
<font size="+1">{$catname[Category]}</font><br />
{section name=Link loop=$linkname[Category]}
hi<a href="{$linkaddr[Category][Link]}" TARGET="_BLANK">{$linkname[Category][Link]}</a><br />
{/section}
{/section}
|
Now I can get the category name to display ($catname[Category]) but no links display.
Can you please help? Show me what I'm doing wrong!
Thanks,
-Jon |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Jun 03, 2008 5:29 pm Post subject: |
|
|
Your index of $link_addr and $link_name is wrong. You took $row['cat_id'] from the database whatever that is, while $cat_name and cat_id have a counting index of 0,1,2.....
Code: | $rs = mysql_query("SELECT * FROM ".$db_prefix."links_cat ORDER BY cat_pos");
$i=0;
while($row = mysql_fetch_array($rs))
{
$cat_name[$i] = $row['cat_name'];
$cat_id[$i] = $row['cat_id'];
$rs2 = mysql_query("SELECT * FROM ".$db_prefix."links WHERE link_cat = '$cat_id' ORDER BY link_name");
while($row2 = mysql_fetch_array($rs2))
{
$link_addr[$i][] = $row2['link_addr'];
$link_name[$i][] = $row2['link_name'];
};
$i++;
};
$smarty->assign("catid", $cat_id);
$smarty->assign("catname", $cat_name);
$smarty->assign("linkname", $link_name);
$smarty->assign("linkaddr", $link_addr);
|
|
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Jun 03, 2008 5:52 pm Post subject: |
|
|
As you can get an associative array from your database queries I personally prefere to create on nested array and use {foreach} in the template.
Code: | $rs = mysql_query("SELECT * FROM ".$db_prefix."links_cat ORDER BY cat_pos");
while($row = mysql_fetch_assoc($rs))
{
$rs2 = mysql_query("SELECT * FROM ".$db_prefix."links WHERE link_cat = '$cat_id' ORDER BY link_name");
while($row2 = mysql_fetch_assoc($rs2))
{
$row['links'][] = $row2;
};
$cats[]=$row;
};
$smarty->assign("cats", $cats);
|
template
Code: |
{foreach item=thiscat from=$cats}
<font size="+1">{$thiscat.cat_name}</font><br />
{foreach item=thislink from=$thiscat.links}
hi<a href="{$thislink.link_addr}" TARGET="_BLANK">{$thislink.link_name}</a><br />
{/foreach}
{/foreach}
|
But this is personal flavor.... |
|
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
|