Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
01-16-2013, 02:22 AM #1
- Join Date
- Jan 2009
- 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:
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
<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';
$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!
01-16-2013, 06:15 PM #2
- Join Date
- Jan 2011
- Thanked 634 Times in 633 Posts
$test = "--other=Sign Out";
$piece = explode("=", $test);
$str = $piece;
if(substr($str, 0, 1) == "-") echo "dash<br />";
if(substr($str, 0, 2) == "--") echo "dash again<br />";
01-16-2013, 06:24 PM #3
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- Thanked 2,662 Times in 2,631 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.