Array-Built Bullet Lists

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NinjaBot
Forum Newbie
Posts: 7
Joined: Sat Apr 29, 2006 11:54 am

Array-Built Bullet Lists

Post by NinjaBot »

Hi there,

Being one for jumping in the deep end, I (being a php novice) have taken it upon myself to build a php menu script. Thus far, it isn't going to badly, but I'm really struggling now.

This is the format I'm trying to build:

Code: Select all

<ul>
	<li><h2><a href="page1.html">Products</a></h2>
	<ul>
		<li><a href="page1.html">Product 1</a>
		<ul>
			<li><a href="page1.html">Page 1</a></li>
			<li><a href="page2.html">Page 2</a></li>
			<li><a href="page3.html">Page 3</a></li>
		</ul>
		</li>
		<li><a href="page4.html">Product 2</a></li>
		<li><a href="page5.html">Product 3</a></li>
	</ul>
	</li>
</ul>
<ul>
	<li><h2><a href="page6.html">Applications</a></h2>
	</li>
</ul>
<ul>
	<li><h2><a href="page7.html">News</a></h2>
	<ul>
		<li><a href="page7.html">Latest News</a></li>
		<li><a href="page8.html">Older News</a></li>
	</ul>
	</li>
</ul>

It didn't seem too much to take on at first, and I'm getting there ... but I'm really stuck. Arrays are new to me, and I'm sure it will come with time, but even though I'm almost there, I can't help wondering if there'd be a better way to build this in order to get the format I'm after. Anyways, here is what I have so far:

Code: Select all

<?
$menu = array(
		'Menu One'=>array(
			'Product 1'=>array(
				'Page 1'=>'page1.html',
				'Page 2'=>'page2.html',
				'Page 3'=>'page3.html'),
			'Product 2'=>'page4.html',
			'Product 3'=>'page5.html'),
		'Menu Two'=>array(),
		'News'=>array(
			'Latest News'=>'page7.html',
			'Older News'=>'page8.html'));

function array_search_recursive($needle, $haystack) {
    $pos = null;
    $keys = array_keys($haystack);
    while(!$pos && (list($garbage, $value)=each($keys))) {
        if(is_scalar($haystack[$value])) {
            if($haystack[$value] === $needle)
                $pos[] = $value;
        } elseif(is_array($haystack[$value])) {
            if($pos = array_search_recursive($needle, $haystack[$value]))
                array_unshift($pos, $value);
        }
    }
    return $pos;
}
function draw_menu($menu, $preserve, &$id) {
    if($id == 0)
        echo "<ul id=\"$id\">\r\n";
    else
        echo "\t<ul id=\"$id\">\r\n";
    $id += 1;
    foreach($menu as $key=>$value) {
        if(is_array($value)) {
            if(@in_array($key, $preserve))
            echo "\t<li><h2><a href=\"$value\">$key</a></h2></li>\r\n";
            draw_menu($value, $preserve, $id);
        }
        else {
            echo "\t\t<li class=\"file\">";
            if(@in_array($key, $preserve))
                echo "<a class=\"live\" href=\"$value\">$key</a>";
            else
                echo "<a href=\"$value\">$key</a>";
            echo "</li>\r\n";
        }
    }
    if($id == 0)
        echo "p\n</ul>\r\n";
    else
        echo "r\n</ul>\r\n";
}
$id = 0;
$base = basename($_SERVER['PHP_SELF']);
$self = isset($_SERVER['QUERY_STRING']) ? $base.'?'.$_SERVER['QUERY_STRING'] : $base;
$preserve = array_search_recursive($self, $menu);
draw_menu($menu, $preserve, $id);
?>

Some help and guidance would be much appreciated.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

if you are trying to accomplish a modular approach to this, then I could see the use for functions to do what you're after.

however, given that what you're trying to print out seems pretty static, you might try something procedurally.

you could accomplish this with a few nested foreach() loops.

ex:

Code: Select all

$someArray = array("group 1"=>array("page1","page2"),"group2"=>"page1");
foreach($someArray as $key => $val)
{
   if(is_array($val))
      foreach(...)
   //etc.
}
Post Reply