Consider the array is:
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
)
)
)
Where, the first keys [Page-x] from array will be Main-Links in the navigation menu.
Some of the main links may have Sub-Links, some not.
Sub-links are the values of the key [0] of the 3rd sub-array.
And finally the URL for each and every link will be the value of key [1] of the 3rd sub-Array.
Only Pages that have more than one category will show its categories as sub-links
The navigation bar i would like to have:
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>
the PHP code
$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>';
}
}
Unfortunately i can't get it work... the output is not what i am expecting :(
current Output (wrong one):
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>
Any help would be appreciated!