www.webdeveloper.com
Results 1 to 5 of 5

Thread: PHP mega menu

  1. #1
    Join Date
    Jul 2014
    Posts
    3

    PHP mega menu

    I want to create a mega menu like
    http://www.libertyeaglearms.com/dev/
    my code goes here:

    PHP Code:
    <div id="menu">
      <ul>
        <?php foreach ($categories as $category) { ?>
        <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
          <?php if ($category['children']) { ?>
          <div>
            <?php for ($i 0$i count($category['children']);) { ?>
            <ul>
              <?php $j $i ceil(count($category['children']) / $category['column']); ?>
              <?php for (; $i $j$i++) { ?>
              <?php if (isset($category['children'][$i])) { ?>
              <li><a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?></a></li>
              <?php ?>
              <?php ?>
            </ul>
            <?php ?>
          </div>
          <?php ?>
        </li>
        <?php ?>
      </ul>
    </div>
    And my database is as follows:
    category_id name
    63 Decor
    64 kitchen
    65 bar

    category_id path_id
    65 65
    64 63
    63 63
    65 63
    65 64
    64 64

  2. #2
    Join Date
    Jul 2014
    Posts
    3
    category_id parent_id
    63 0
    64 63
    65 64

  3. #3
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    785
    ?? What's stopping you? IE, what problem are you having?
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  4. #4
    Join Date
    Jul 2014
    Posts
    3
    Quote Originally Posted by ginerjm View Post
    ?? What's stopping you? IE, what problem are you having?
    with this code im getting only one level subcategory, where as i have two level subcategory. i am new and im not been able to figure out how to add the third subcategory below second subcategory.

    for example:

    Decor
    Kitchen
    bar

  5. #5
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    785
    For one thing - your code is flawed. Your first for loop has no increment clause in it. What happens there? Is there a default there that I'm not aware of? Even if there is, it would be very good practice to put one in there just to make it easier to read.

    For another thing your code is a mess. Any time you are using <? & ?> CONSTANTLY you are writing poor code. Separate your html (presentation code) from your logic (php) as much as possible. Learn how to use php's features to make your code more readable.

    I have restructured your code. See if you can find why you are not getting what you expect. Plus - why do you have div tags in the middle of a list like this? I could see it if they helped you format the outcome a little differently, but you have no styling attached to these divs so what's the point??

    PHP Code:
    echo '<div id="menu">'
    echo 
    "<ul>"
    foreach (
    $categories as $category
    {
        echo 
    "<li><a href='" $category['href'] . "'>" $category['name'] . "</a>"
        if (
    $category['children']) 
        {  
            echo 
    "<div>"
            for (
    $i 0$i count($category['children']);$i++) // I ADDED INCREMENT HERE
            

        echo 
    "<ul>"
        
    $j $i ceil(count($category['children']) / $category['column']); 
        for (; 
    $i $j$i++)  // YOU HAVE NO STARTING VALUE FOR THIS LOOP
        // YOU ARE USING $I AGAIN HERE WHILE IT IS BEING USED IN THE FIRST LOOP
        
    {
                      if (isset(
    $category['children'][$i]))    
                      {
                          echo 
    "<li><a href='" .$category['children'][$i]['href']."'>" .$category['children'][$i]['name'] ."</a></li>"
                       }
        } 
        echo 
    "</ul>"
           }
           echo 
    "</div>"
       } 
       echo 
    "</li>"

    echo 
    "</ul>"
    echo 
    "</div>"
    (Sorry about the appearance. It looked a lot neater in my editor, but didn't transfer into this site as well. oh, well....
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles