考虑数组是:
Array
(
[Page-1] => Array
(
[0] => Array
(
[0] => Cat-1
[1] => Item-1
)
)
[Page-2] => Array
(
[0] => Array
(
[0] => Cat-2
[1] => Item-2
)
[1] => Array
(
[0] => Cat-3
[1] => Item-3
)
[2] => Array
(
[0] => Cat-4
[1] => Item-4
)
)
[Page-3] => Array
(
[0] => Array
(
[0] => Cat-5
[1] => Item-5
)
)
[Page-4] => Array
(
[0] => Array
(
[0] => Cat-6
[1] => Item-6
)
)
[Page-5] => Array
(
[0] => Array
(
[0] => Cat-7
[1] => Item-7
)
[1] => Array
(
[0] => Cat-9
[1] => Item-9
)
)
[Page-6] => Array
(
[0] => Array
(
[0] => Cat-8
[1] => Item-8
)
)
)
其中,数组中的第一个键[Page-x]将是导航菜单中的 Main-Links 。
一些主要链接可能有子链接,有些则没有
子链接是第3个子阵列的键[0]的值
最后,每个链接的URL将是第3个子阵列的key [1]的值
只有包含多个类别的页面才会将其类别显示为子链接
我想要的导航栏:
1. <a href="Item-1">Page-1</a>
2. <a href="#">Page-2</a>
<a href="Item-2">Cat-2</a>
<a href="Item-3">Cat-3</a>
<a href="Item-4">Cat-4</a>
3. <a href="Item-5">Page-3</a>
4. <a href="Item-6">Page-4</a>
5. <a href="#">Page-5</a>
<a href="Item-7">Cat-7</a>
<a href="Item-9">Cat-9</a>
6. <a href="Item-8">Page-6</a>
PHP代码
$records = $p->main_links();
foreach ($records as $key => $value) {
$return[$value['page']][] = array($value['child'], $value['item']);
}
foreach ($return as $key2 => $value2) {
$count = 0;
/* Select a specific value within the Array */
$main_links = $value2[$count][1]; /* URL of the main Pages */
$count = count($return[$key2]);
if($count > 1) {
foreach ($value2 as $key3 => $value3)
{
$link_name = $value3[0]; /* Child Link Names */
$link_url = $value3[1]; /* URL of Child Links */
/* addedd htmlspecialchars() function to $variables that will be echoed into HTML. It provides some XSS protection */
$cat_link .= '<li><a href="'.htmlspecialchars($filter1.$p->seoUrl($key2).$filter2.$p->seoUrl($link_url)).'">'.htmlspecialchars($link_name).'</a></li>';
}
$result .= '
<li '.htmlspecialchars($li_class).'><a href="#"><span>'.htmlspecialchars($key2).'</span></a>
<ul>
'.$cat_link.'
</ul>
</li>';
}else {
$result .= '
<li><a href="'.htmlspecialchars($filter1.$p->seoUrl($main_links)).'"><span>'.htmlspecialchars($key2).'</span></a></li>';
}
}
不幸的是我不能让它工作......输出不是我所期待的:(
电流输出(错误的一个):
1. <a href="Item-1">Page-1</a>
2. <a href="#">Page-2</a>
<a href="Item-2">Cat-2</a>
<a href="Item-3">Cat-3</a>
<a href="Item-4">Cat-4</a>
3. <a href="Item-5">Page-3</a>
4. <a href="Item-6">Page-4</a>
5. <a href="#">Page-5</a>
<a href="Item-2">Cat-2</a>
<a href="Item-3">Cat-3</a>
<a href="Item-4">Cat-4</a>
<a href="Item-7">Cat-7</a>
<a href="Item-9">Cat-9</a>
6. <a href="Item-8">Page-6</a>
任何帮助将不胜感激!
答案 0 :(得分:0)
您当前的代码已接近正常工作。这一行总是会产生1的计数。
$count = count($value);
我相信,你在那里寻找的是:
$count = count($return[$key]);
答案 1 :(得分:0)
我发现另一种方式比我试图做的更好。这解决了我的问题。
http://wizardinternetsolutions.com/articles/web-programming/single-query-dynamic-multi-level-menu
感谢您的支持!