我有一个多维数组,像这样:
Array (7)
0 => Array (6)
id => "31"
link => "http://site.loc/index.php?id_categor..."
name => "Gotowe"
desc => ""
id_parent => "28"
children => Array (5)
0 => Array (6)
id => "152"
link => "http://site.loc/index.php?id_categor..."
name => ""
desc => ""
id_parent => "31"
children => Array (0)
1 => Array (6)
id => "153"
link => "http://site.loc/index.php?id_categor..."
name => ""
desc => ""
id_parent => "31"
children => Array (0)
2 => Array (6)
id => "154"
link => "http://site.loc/index.php?id_categor..."
name => ""
desc => ""
id_parent => "31"
children => Array (0)
3 => Array (6)
id => "155"
link => "http://site.loc/index.php?id_categor..."
name => ""
desc => ""
id_parent => "31"
children => Array (0)
4 => Array (6)
id => "156"
link => "http://site.loc/index.php?id_categor..."
name => ""
desc => ""
id_parent => "31"
children => Array (0)
1 => Array (6)
id => "42"
link => "http://site.loc/index.php?id_categor..."
name => "Moda"
desc => ""
id_parent => "28"
children => Array (6)
0 => Array (6)
id => "89"
link => "http://site.loc/index.php?id_categor..."
name => "SUKIENKI"
desc => ""
id_parent => "42"
children => Array (0)
{.....}
然后,在smarty中,我有:
{section name = "foo" loop = $node.children}
{if $node.children[id_parent] == currentCategoryId}
<li><p><span><a href = "{$node.children.children.link|escape:'htmlall':'UTF-8'}" title="{$child.children.desc|escape:'htmlall':'UTF-8'}">{$node.children.children.name|escape:'htmlall':'UTF-8'}</a></span></p></li>
{/if}
{/section}
关键是,我需要从数组中获得所有链接,其中id_parent等于currentCategoryId。更准确地说,当我在与id = 42(它在数组代码的底部)类别的页面上,我需要得到所有的链接,从它并放在页面上。我尝试了一些{foreach}
,但它不起作用,所以现在我尝试{section}
,它也不能正常工作。我被卡住了,你能帮我吗?
您使用了错误的访问循环元素,$currentCategoriId
应该在$
的开头。
循环应该像这样:
{section name = "foo" loop = $node.children}
{if $node.children[foo].id_parent == $currentCategoryId}
<li><p><span><a href = "{$node.children[foo].link|escape:'htmlall':'UTF-8'}" title="{$node.children[foo].desc|escape:'htmlall':'UTF-8'}">{$node.children[foo].name|escape:'htmlall':'UTF-8'}</a></span></p></li>
{/if}
{/section}
对于这样的PHP数据:
$data = Array (
'children' => array(
0 => array (
'id' => "152",
'link' => "http://site.loc/index.php?id_categor...",
'name' => "aaa",
'desc' => "a desc",
'id_parent' => "31",
),
1 => array (
'id' => "22152",
'link' => "22http://site.loc/index.php?id_categor...",
'name' => "bbb",
'desc' => "b desc",
'id_parent' => "3122",
),
1 => array (
'id' => "3322152",
'link' => "3322http://site.loc/index.php?id_categor...",
'name' => "ccc",
'desc' => "c desc",
'id_parent' => "31",
)
));
$smarty->assign('node',$data);
$smarty->assign('currentCategoryId',31);
我得到想要的输出:
<li><p><span><a href = "http://site.loc/index.php?id_categor..." title="a desc">aaa</a></span></p></li>
<li><p><span><a href = "3322http://site.loc/index.php?id_categor..." title="c desc">ccc</a></span></p></li>
下次请在我的回答中提供PHP格式的数据。这样测试就容易多了。
编辑
因为你没有提供PHP格式的样本数据,所以很难测试它,也不知道你从PHP分配给Smarty的是什么,但我想这应该适合你:
{section name = "foo" loop = $node}
{section name="bar" loop=$node[foo].children}
{if $node[foo].children[bar].id_parent == $currentCategoryId}
<li><p><span><a href = "{$node[foo].children[bar].link|escape:'htmlall':'UTF-8'}" title="{$node[foo].children[bar].desc|escape:'htmlall':'UTF-8'}">{$node[foo].children[bar].name|escape:'htmlall':'UTF-8'}</a></span></p></li>
{/if}
{/section}
{/section}
currentCategoryId
可能被视为字符串而不是变量,在其前面加一个美元符号