Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts

    PHP tree list problems

    I have created some PHP code that generates an unordered list. In the pages table I can assign a parent to a page. This in turn generates an unordered list indented to reveal the children. The problem I'm having with my code is that it is generating an extra empty set of <ul></ul> tags.

    Here is the html created from the PHP:
    Code:
    <ul>
        <li><a href="01_soc.php?recordID=7">Membership</a></li>
            <ul>
            <li><a href="01_soc.php?recordID=8">Active</a></li>
            	<ul></ul>
            <li><a href="02_soc.php?recordID=9">Associate</a></li>
            	<ul></ul>
            <li><a href="01_soc.php?recordID=10">Student Associate </a></li>
            	<ul></ul>
            <li><a href="01_soc.php?recordID=5">Corporate</a></li>
            	<ul></ul>
            <li><a href="01_soc.php?recordID=13">Join</a></li>
            	<ul></ul>
    		</ul>
        <li><a href="01_soc.php?recordID=3">SOC Members</a></li>'
        	<ul></ul>
        <li><a href="01_soc.php?recordID=4">About</a></li>
            <ul>
            <li><a href="01_soc.php?recordID=11">Mission</a></li>
            	<ul></ul>
            <li><a href="01_soc.php?recordID=12">History</a></li>
            	<ul></ul>
        </ul>
        <li><a href="01_soc.php?recordID=14">Events</a></li>
        <ul>
        	<li><a href="01_soc.php?recordID=15">Underwater Workshop</a></li>
        		<ul></ul>
        </ul>
    </ul>
    Here is the PHP:
    PHP Code:
    <?php // Side Nav Querry
    $parentid 0// assuming that 0 is the main category.
    getTestParent($parentid);
    function 
    getTestParent($parentid) {
        
    $sql "SELECT * FROM pageTypes, pages 
        WHERE approved = 'y' 
        AND pages.menuPosition = 2
        AND pages.pageType = pageTypes.id 
        AND pages.pageParent = "
    .$parentid."
        ORDER BY menuOrder, pageName ASC"

    $run mysql_query($sql);

    echo 
    '<ul>';
        while (
    $rec mysql_fetch_assoc($run)) { 
        echo 
    '<li>',
        
    '<a href="',$rec['pageLinkName'],'?recordID='$rec['id'],'">',
         
    $rec['pageName'], '</a></li>';
        
    getTestParent($rec['id']);
        }
    echo 
    '</ul>';
    }
    ?>
    I've tried everything I can think of and still I end up with the extra set of tags.
    Last edited by rgEffects; 09-05-2013 at 01:34 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    That appears to be where there are no additional children.
    PHP Code:
    function getTestParent($parentid)
    {
        
    $sql "SELECT * FROM pageTypes, pages 
        WHERE approved = 'y' 
        AND pages.menuPosition = 2
        AND pages.pageType = pageTypes.id 
        AND pages.pageParent = "
    .$parentid."
        ORDER BY menuOrder, pageName ASC"

        
    $run mysql_query($sql);
        if (
    mysql_num_rows($run) > 0)
        {
            echo 
    '<ul>';
            while (
    $rec mysql_fetch_assoc($run))
            { 
                echo 
    '<li>',
                    
    '<a href="',$rec['pageLinkName'],'?recordID='$rec['id'],'">',
                     
    $rec['pageName'], '</a>';
                
    getTestParent($rec['id']);
                echo 
    '</li>';
            }
            echo 
    '</ul>';
        }

    Not 100% sure here, but I believe that ul/ol lists cannot be parented to the ul/ol, but can be parented to the li. So I moved the end li to after the recursive.

    Somewhere down the line I wrote a wicked one of these that queried once and recursive built the list. It was a pain.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    rgEffects (09-05-2013)

  • #3
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts
    That was it. Thank you so much. This works out as far as I need it to go. Tested to 8 levels.

    Thanks again.


  •  

    Posting Permissions

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