Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-16-2013, 02:22 AM   PM User | #1
CalumK
New Coder

 
Join Date: Jan 2009
Posts: 52
Thanks: 4
Thanked 3 Times in 3 Posts
CalumK is an unknown quantity at this point
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!
CalumK is offline   Reply With Quote
Old 01-16-2013, 06:15 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,364
Thanks: 18
Thanked 347 Times in 346 Posts
sunfighter is on a distinguished road
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 />";
?>
sunfighter is offline   Reply With Quote
Old 01-16-2013, 06:24 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:38 PM.


Advertisement
Log in to turn off these ads.