[eluser]skunkbad[/eluser]
I've been going around and around with this for hours. I've got an array that I'm trying to build a menu out of.Any help is appreciated:
This is the code:
Code: <div id="category-menu">
<h3><a href="product.php">Products</a></h3>
<div style="float:left; width:100%;">
<?php
$category_menu = array(
'Examples' => array(
'category_id' => 1,
'sub' => array(
'Widgets' => array(
'category_id' => 2,
'sub' => array(
'Hardcore' => array(
'category_id' => 3
),
'Whatever' => array(
'category_id' => 4
)
)
),
'Little' => array(
'category_id' => 5,
'sub' => array(
'Papers' => array(
'category_id' => 9
)
)
),
'Yaknow' => array(
'category_id' => 7
)
)
),
'Something' => array(
'category_id' => 8
)
);
function create_menu_markup($menu_array, $back_out_point, $back_out_index, $parents, $is_sub)
{
// if a submenu, put into an unordered list
if(!$is_sub)
{
$menu = '';
}
else
{
$menu = '<ul>'; // Open the menu container
}
// loop through current array to extract element values
$x = 0;
foreach($menu_array as $id => $properties)
{
// set a place to come back to if more than one category in this level
if(count($menu_array) > 1)
{
if(count($back_out_point) > 0)
{
$back_out_index++;
if(isset($back_out_point[$back_out_index]))
{
$back_out_point[$back_out_index] += 1;
}
else
{
$back_out_point[$back_out_index] = 1;
}
}
else
{
$back_out_point[$back_out_index] = 1;
}
}
if($x == 0)
{
// only add parent once
$parents[] = '/' . $id;
}
$x++;
foreach($parents as $parent)
{
if(!isset($this_parent)){
$this_parent = $parent;
}
else
{
$this_parent .= $parent;
}
}
$y = 0;
foreach($properties as $key => $val)
{
if(is_array($val))
{
$back_out_point[$back_out_index] += 1;
$sub = create_menu_markup($val, $back_out_point, $back_out_index, $parents, TRUE);
}
else
{
$sub = NULL;
if(count($properties) == $y)
{
//array pop the parents array by the number of the last back out point key => value and the pop that key => value
if(count($back_out_point) > 0)
{
for($i=0;$i<=end($back_out_point)-1;$i++)
{
array_pop($parents);
}
array_pop($back_out_point);
$back_out_index--;
}
}
}
$y++;
}
unset($y);
if(!$is_sub)
{
if($menu != '')
{
$menu .= '</div></div>';
}
$menu .= '<h4><a href="http://localhost/' . strtolower($this_parent) . '.php">' . preg_replace('/[_]+/', ' ', $id) . '</a></h4><div class="submenu-div"><div class="submenu-listbox">' . $sub;
}
else
{
$menu .= '<li><a href="http://localhost' . strtolower($this_parent) . '.php">' . preg_replace('/[_]+/', ' ', $id) . '</a>' . $sub . '</li>';
}
unset($url, $sub);
}
unset($x);
$end_tag = (!$is_sub) ? '</div></div>' : '</ul>';
return $menu . $end_tag;
}
$back_out_point = array();
$back_out_index = 0;
$parents = array();
$is_sub = FALSE;
echo create_menu_markup($category_menu, $back_out_point, $back_out_index, $parents, $is_sub);
?>
</div>
</div>
[eluser]skunkbad[/eluser]
This is the expected HTML:
Code: <div id="category-menu">
<h3>
<a href="product.php">Products</a>
</h3>
<div style="float:left; width:100%;">
<h4>
<a href="http://localhost//examples.php">Examples</a>
</h4>
<div class="submenu-div">
<div class="submenu-listbox">
<ul>
<li>
<a href="http://localhost/examples/widgets.php">Widgets</a>
<ul>
<li>
<a href="http://localhost/examples/widgets/hardcore.php">Hardcore</a>
</li>
<li>
<a href="http://localhost/examples/widgets/whatever.php">Whatever</a>
</li>
</ul>
</li>
<li>
<a href="http://localhost/examples/little.php">Little</a>
<ul>
<li>
<a href="http://localhost/examples/little/papers.php">Papers</a>
</li>
</ul>
</li>
<li>
<a href="http://localhost/examples/yaknow.php">Yaknow</a>
</li>
</ul>
</div>
</div>
<h4>
<a href="http://localhost//something.php">Something</a>
</h4>
<div class="submenu-div">
<div class="submenu-listbox"></div>
</div>
</div>
</div>
[eluser]Tominator[/eluser]
Hello,
I made this one, I am sure it helps you .. it's much easier:
Code: // OUR ARRAY
$category_menu = array(
'Examples' => array(
'category_id' => 1,
'sub' => array(
'Widgets' => array(
'category_id' => 2,
'sub' => array(
'Hardcore' => array(
'category_id' => 3
),
'Whatever' => array(
'category_id' => 4
)
)
),
'Little' => array(
'category_id' => 5,
'sub' => array(
'Papers' => array(
'category_id' => 9
)
)
),
'Yaknow' => array(
'category_id' => 7
)
)
),
'Something' => array(
'category_id' => 8
)
);
// FOR MAKING CHILDS
function make_list($childs, $parents)
{
echo '<ul>';
foreach($childs as $name => $array)
{
echo '<li>';
echo '<a href="/'.$parents.'/'. strtolower($name) .'">'.$name.'</a>';
if(!empty($array['sub']) && count($array['sub']) > 0)
{
$new_parents = $parents . "/" . $name;
make_list($array['sub'], $new_parents);
}
echo '</li>';
}
echo '</ul>';
}
// HERE WE GO
foreach($category_menu as $name => $array)
{
echo '<h4>';
echo '<a href="/'. strtolower($name) .'">'. $name .'</a>';
echo '</h4>';
echo '<div class="submenu-div">';
echo '<div class="submenu-listbox">';
if(!empty($array['sub']) && count($array['sub']) > 0)
{
$new_parents = strtolower($name);
make_list($array['sub'], $new_parents);
}
echo '</div>';
echo '</div>';
}
?>
Tom
[eluser]skunkbad[/eluser]
You are my hero! That was fast too! Thank you, thank you, thank you!
[eluser]skunkbad[/eluser]
[quote author="jayrulez" date="1271696254"]have a look here http://code.google.com/p/zii/source/brow.../CMenu.php
You can render unlimited descendants[/quote]
Tominator's code is perfect for what I need. I saw the discussion going on here about SEO friendly URLs, so I decided to work a bit on the way my URLs are appearing in Community Cart. I haven't made a commit yet, because there are still other thing to work out, plus I have to work on customer's stuff.
|