我有多维数组,如下所示:
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)
{.....}
然后,聪明地,我得到了:
{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}
,它也无法正常工作。我坚持了,你能帮助我吗?
答案 0 :(得分:1)
您对循环元素使用了错误的访问权限,而$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}
答案 1 :(得分:0)
currentCategoryId
可能被视为字符串而不是变量,在它之前添加一个美元符号。