create Sub menu

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
Surbakti
Forum Newbie
Posts: 3
Joined: Sun Aug 09, 2009 10:21 pm

create Sub menu

Post by Surbakti »

I'm sorry if this topic re-post.
I've problem showing menu with sub menu in website.
i want show sub menu when menu click.
like this one :

*home
*Tools
*about
*site map
when i click tools menu it's will be like this

*home
*tools
-pen
-pencil
-eraser
*about
*site map

what must i do?
thanks.
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: create Sub menu

Post by AlanG »

You would need to use JavaScript if you wish to make it more fluent, as PHP will cause the page to load. But if you want to do it with PHP, you could do something like this.

Code: Select all

 
<?php
    // This function will read in an array, if the keys are arrays they will be treated as categories, otherwise they will be treated as hyperlinks
    function build_links_menu($links,$cat) {
        $menu = '';
 
        foreach($links as $key=>$value) {
            if(is_array($links[$key])) {
                $menu .= '* <a href="?cat='.urlencode($key).'">'.$key.'</a><br />'."\n";
        if($key == $cat) {
            foreach($links[$key] as $key2=>$value2)
                    $menu .= '- <a href="'.$value2.'">'.$key2.'</a><br />'."\n";
        }
            }
            else
                $menu .= '&nbsp; <a href="'.$value.'">'.$key.'</a><br />'."\n";
        }
 
        return $menu;
    }
 
    $cat = (isset($_GET['cat']) && !empty($_GET['cat'])) ? urldecode($_GET['cat']) : null; // Fetch the category
 
    // Map out the categories
    $links = array(
                            'home' => array('example'=>'example.html','example2'=>'example2.html'),
                            'example3' => 'example3.html',
                            'tools' => array('pen'=>'example4.html','pencil'=>'example5.html','eraser'=>'eraser.html'),
                            'about' => array(),
                            'site map' => array(),
                        );
 
    $links_menu = build_links_menu($links,$cat);
?>
<html>
    <!-- You can place your HTML here -->
    <body>
        <?php echo $links_menu; ?>
    </body>
</html>
 
You should be able to figure that out. :) The $links array seems obvious enough to modify.
Surbakti
Forum Newbie
Posts: 3
Joined: Sun Aug 09, 2009 10:21 pm

Re: create Sub menu

Post by Surbakti »

thanks AlanG.
it's that code can read data from databases?
i want link read from database.
where i put query for read from databases?
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: create Sub menu

Post by AlanG »

Yeah, you will need to modify the links array. What's the layout of your database table?
Surbakti
Forum Newbie
Posts: 3
Joined: Sun Aug 09, 2009 10:21 pm

Re: create Sub menu

Post by Surbakti »

I'm new one.
what is the database layout?
if you mean database table and field,here they are;
database name "Project" with table "Cars" and field are: carCode,careName,carsType,detail,inputUser,inputtime
Post Reply