Jump to content

PHP fuction not rendering HTML properly


thara

Recommended Posts

I am using this php function to display categories and sub-categories in a list.

    function displayMenuMobile(&$cats, $parent, $level=0) {
        switch ($level) {
            case 0: $class = "menu-phone"; $menuId = "nav"; break;
            case 1: $class = "sub-menu"; $menuId = ""; break;
        }
        if ($parent==0) {
            foreach ($cats[$parent] as $id=>$nm) {
                displayMenuMobile($cats, $id);
            }
        }
        else {
            echo "<ul class='$class' id='$menuId'>\n";
            $classParent = '';
            if ($level == 0) {
                $classParent = "class='parent'";
            }
            foreach ($cats[$parent] as $id=>$nm) {
                echo "<li $classParent><a href='#'><span>$nm</span></a>\n";
                if (isset($cats[$id])) {
                    displayMenuMobile($cats, $id, $level+1);  //increment level
                }
            }
            echo "</li></ul>\n";
        }  
    }

Its displaying the list but it is not rendering HTML properly.

This is rendering HTML:

    <ul class='menu-phone' id='nav'>
        <li class='parent'><a href='#'><span>Hot Deals</span></a>
        <li class='parent'><a href='#'><span>Pantry</span></a>
            <ul class='sub-menu' id=''>
                <li ><a href='#'><span>Biscuits</span></a>
                <li ><a href='#'><span>Canned Foods</span></a>
                <li ><a href='#'><span>Canned Vegetables</span></a>
                <li ><a href='#'><span>Chips, Snacks & Nuts</span></a>
            </ul>
        <li class='parent'><a href='#'><span>Drinks</span></a>
            <ul class='sub-menu' id=''>
                <li ><a href='#'><span>Coffee</span></a>
                <li ><a href='#'><span>Flavoured Milk Drinks</span></a>
            </ul>
        <li class='parent'><a href='#'><span>Confectionery</span></a>
            <ul class='sub-menu' id=''>
                <li ><a href='#'><span>Chocolate</span></a>
                <li ><a href='#'><span>Mints &Bathroom & Toilet Gums  </span></a></li>
            </ul>
        <li class='parent'><a href='#'><span>Household</span></a>
            <ul class='sub-menu' id=''>
                <li ><a href='#'><span>Air Fresheners</span></a>
                <li ><a href='#'><span>Bathroom & Toilet</span></a>
                <li ><a href='#'><span>Cleaning</span></a>
                <li ><a href='#'><span>Electrical</span></a>
                <li ><a href='#'><span>Homeware</span></a>
                <li ><a href='#'><span>Laundry</span></a></li>
            </ul>
        <li class='parent'><a href='#'><span>Cosmetics</span></a>
            <ul class='sub-menu' id=''>
                <li ><a href='#'><span>Eye Makeup</span></a>
                <li ><a href='#'><span>Lip Gloss & Moisturisers</span></a>
                <li ><a href='#'><span>Nail Polish</span></a></li>
            </ul>
        </li>
    </ul>

Problem is its not rendering closing </li> tag in the parent and nested list.

Can anybody tell me whats wrong with this?
Hope somebody may help me out.

Thank you.

 

Edited by thara
Link to comment
Share on other sites

Because the closing <li> tag needs to be echo'd after the if

            echo "<li $classParent><a href='#'><span>$nm</span></a>\n";
                if (isset($cats[$id])) {
                    displayMenuMobile($cats, $id, $level+1);  //increment level
                }
                echo "</li>\n"; // closing </li> tags needs to be echo'd here
            }
            echo "</ul>\n";  // not here

I pretty sure Jacques1 explained that last time

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.