I have the following array that needs to be displayed as navigation
[items] => Array
(
[Electronics] => Array
(
[level2] => Array
(
[Home Use] => Array
(
[level3] => Array
(
[Laptops] => Array
(
[level4] => Array
(
[0] => HP A very simple laptop
[1] => HP Tablet
[2] => Acer Laptop
)
)
[Desktops] => Array
(
[level4] => Array
(
[0] => Acer Tablet
)
)
[Tablets] => Array
(
[level4] => Array
(
[0] =>
)
)
)
)
[Business] => Array
(
[level3] => Array
(
[Tablets] => Array
(
[level4] => Array
(
[0] => HP A very simple laptop
[1] => HP Tablet
[2] => Touchmate
)
)
[Laptops] => Array
(
[level4] => Array
(
[0] => Compaq
[1] => IBM
)
)
[Desktop] => Array
(
[level4] => Array
(
[0] => Lenovo
)
)
)
)
)
)
[Hotels] => Array
(
[level2] => Array
(
[5 Stars] => Array
(
[level3] => Array
(
[Daily Basis] => Array
(
[level4] => Array
(
[0] => Hilton
)
)
[Montly Basis] => Array
(
[level4] => Array
(
[0] => Hilton
[1] => Intercon
[2] => Marina Hotel
[3] => Galaxy Plaza Hotel
)
)
)
)
[4 Stars] => Array
(
[level3] => Array
(
[Daily Basis] => Array
(
[level4] => Array
(
[0] => Address
)
)
[Montly Basis] => Array
(
[level4] => Array
(
[0] => Armada Al Barsha Hotel
[1] => Everest International Hotel
[2] => New Hotel
[3] => Good Hotel
)
)
)
)
[3 Stars] => Array
(
[level3] => Array
(
[Daily Basis] => Array
(
[level4] => Array
(
[0] => Royal Garden Hotel
)
)
[Montly Basis] => Array
(
[level4] => Array
(
[0] => Galaxy Plaza Hotel
)
)
)
)
)
)
[Entertainment] => Array
(
[level2] => Array
(
[] => Array
(
[level3] => Array
(
[] => Array
(
[level4] => Array
(
[0] =>
)
)
)
)
)
)
[Services] => Array
(
[level2] => Array
(
[] => Array
(
[level3] => Array
(
[] => Array
(
[level4] => Array
(
[0] =>
)
)
)
)
)
)
)
)
NOW... i want to display this in Unordered list. I have the following function.
public function display($sorted_array)
{
$markup = '';
foreach ($sorted_array as $key => $value)
{
if($key === 'Electronics' || $key ==='Hotels' || $key === 'Services' || $key === 'Entertainment')
{
$markup .= '<h1><li>';
}
$markup .= (is_array($value)) ? '<b>'.$key.'</b></li></ul></h1>'.$this->display($value) : '<ul><li>'.$value. '</li></ul>';
}
return '<ul>'.$markup . '</ul>';
}
Electronics
Home Use
Laptops
HP A very simple laptop
HP Tablet
Acer Laptop
Desktops
Acer Tablet
Tablets
Business
Tablets
HP A very simple laptop
HP Tablet
Touchmate
Laptops
Compaq
IBM
Desktop
Lenovo
Hotels
5 Stars
Daily Basis
Hilton
All the categories and subcategories are being displayed in the right order. However, i need to know where i should add an id so that i can add styles to sub categories and place them appropriately.
If you use id then you would use an id multiple times, so use class instead of id. One way is to keep a counter which represents the level. According that your function works properly, you would get something like this:
public function strip($string)
{
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
$string = strtolower($string); //To lower case
return $string;
}
public function display($sorted_array, $level=0)
{
$level++;
$markup = '';
foreach ($sorted_array as $key => $value)
{
if($key === 'Electronics' || $key ==='Hotels' || $key === 'Services' || $key === 'Entertainment')
{
$markup .= '<h1><li id="'.$this->strip($key).'" class="level-'.$level.'">';
}
$markup .= (is_array($value)) ? '<b>'.$key.'</b></li></ul></h1>'.$this->display($value,$level) : '<ul><li id="'.$this->strip($key).'" class="level-'.$level.'">'.$value. '</li></ul>';
}
return '<ul>'.$markup . '</ul>';
}
Now you can use the level classes in your css.
.level-1 {
}
.level-2 {
}
...