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
    New Coder
    Join Date
    Jan 2009
    Posts
    58
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Automatic List/Nav Generation from textfile

    Ok, So Ive been developing websites for a long time now and Im halfway through writing my own cms.

    But ive been having a real issue with auto generating my navigation menus.

    Basically the Menu Structure is in a plain text file:
    Code:
    index=Home
    hello=Hello 
    goodbye=Exit
    -other=Sign Out
    -other2=Sign Out2
    -other3=Sign Out3
    girls=Girls
    -ali=Ali
    -emma=Emma
    testingfile=TEST
    boys=Boys
    -johns=Johns
    --johnjames=John James
    --johnmaycock=John Maycock
    -jamie=Jamie
    Id like to automatically get that into this format:

    Code:
    <ul>
    <li><a href='index'>Home</a></li>
    <li><a href='hello'>Hello</a></li>
    <li><a href='goodbye'>Exit</a>
    	<ul>
    		<li><a href='other'>Sign Out</a></li>
    		<li><a href='other2'>Sign Out2</a></li>
    		<li><a href='other3'>Sign Out3</a></li>
    	</ul>
    </li>
    <li><a href='girls'>Girls</a>
    	<ul>
    		<li><a href='ali'>Ali</a></li>
    		<li><a href='emma'>Emma</a></li>
    	</ul>
    <li><a href='testingfile'>TEST</a></li>
    <li><a href='boys'>Boys</a>
    	<ul>
    		<li><a href='johns'>Johns</a>
    			<ul>
    				<li><a href='johnjames'>John James</a></li>
    				<li><a href='johnmaycock'>John Maycock</a></li>
    			</ul>
    		</li>
    		<li><a href='jamie'>Jamie</a><li>
    	</ul>
    </li>
    </ul>

    Im not looking for a full solution, Im a proficiant coder, but i just cant work out the logic behind it.
    Basically Im splitting each line into an 2-dimensional array:

    so we have:
    $arr['id'] = 'index';
    $arr['name'] = 'Home';

    or

    $arr['id'] = '--johnmaycock';
    $arr['name'] = 'John Maycock';

    Then I was trying to read the first couple of characters to work out if it was a sub, or sub-sub menu.. but.... I cant get my head around it.
    I got it working with just sub menus, but couldn't get sub-sub to work.

    Im more than welcome to completely different suggestions!

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,096
    Thanks
    23
    Thanked 594 Times in 593 Posts
    One way:
    PHP Code:
    <?php
    $test 
    "--other=Sign Out";
    $piece explode("="$test);
    $str $piece[0];
    if(
    substr($str01) == "-") echo "dash<br />";
    if(
    substr($str02) == "--") echo "dash again<br />";
    ?>

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Not really sure that's all that helpful.

    This would be done either iteratively or recursive. Both are good options; keeping it flat I'd say go for an iterative approach and keep a second array stack of referenced variables as you drill through the depth. Then you can push and pop the stack as needed during iteration to move forward and append. You can determine where you need to go back to based on the level of hyphens used within the depth.
    Another approach would be to use recursion or iteration to build an array with the depth you need, and then use a recursive function to display it.
    Fortunately with HTML you only need to care about how you nest it, not needing to change the overall structure. So a list item that's top level is <li>Item</li>, but an item that's 12 levels deep is also just <li>Item</li>. So all that matters is using the depth to control the opening and closing <li> to <ul>'s.


  •  

    Posting Permissions

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