ThisIsPHP Posted November 22, 2010 Share Posted November 22, 2010 Hi PHP Freaks! Here is my problem. I want to have a PHP navigation system that uses Arrays to control it. $nav = array( "title1" => "url1", "title2" => array( "sub-page1" => "url1", "sub-page2" => "url2" ), "title3" => array( "sub-page1" => "url1", "sub-page2" => "url2" ), "title4" => array( "sub-page1" => "url1", "sub-page2" => "url2" ), "title5" => "url1", "title6" => "url1" ); $loc = basename($_SERVER["REQUEST_URI"]); What I'm trying to do, is if the $loc is a url in "title4" I want a ul/li printout of that array. Any help you could share with me would be MUCH appreciated. The printout should look like: <ul> <li>Title4<ul> <li>url1</li> <li>url2</li> </ul></li> </ul> Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted November 22, 2010 Share Posted November 22, 2010 What have you tried so far? $nav[$loc] will give you the array inside the key "title4", just use a loop to print it out. -Dan Quote Link to comment Share on other sites More sharing options...
ThisIsPHP Posted November 22, 2010 Author Share Posted November 22, 2010 $nav = array( "title1" => "url1", "title2" => array( "sub-page1" => "url1", "sub-page2" => "url2" ), "title3" => array( "sub-page1" => "url1", "sub-page2" => "url2" ), "title4" => array( "sub-title1" => array( "sub-sub-page1" => "url1", "sub-sub-page2" => "url2", ), "sub-title2" => array( "sub-sub-page1" => "url1", "sub-sub-page2" => "url2", ) ), "title5" => "url1", "title6" => "url1" ); Actually, the array looks more like that. I've tried using foreach, and multiple for loops, but the code just doesn't seem to work : / Here is some of the code I've been working with: public function __construct( ) { /*foreach( $include as $item ) { $link = $this->pages[$item]; if( is_array( $link ) ) { echo '<li><a href="' . reset( $link ) . '" title="' . $item . '">' . $item . '</a></li>' . "\r\n"; } else { echo '<li><a href="' . $link . '" title="' . $item . '">' . $item . '</a></li>' . "\r\n"; } }*/ /* if ( empty( $array ) ) return ''; $output = '<ul>'; foreach ( $array as $key => $subArray ) { $output .= '<li>' . $key . makeList($subArray) . '</li>'; } $output .= '</ul>'; return $output; */ } //Make a list from an array public function makeList( $pages ) { //Base case: an empty array produces no list if( empty( $array ) ) return ''; //Recursive Step: make a list with child lists $output = '<ul>'; foreach( $array as $key => $subArray ) { $output .= '<li>' . $key . makeList( $subArray ) . '</li>'; } $output .= '</ul>'; return $output; } I'm just jumping back into this project, so I'm not 100% sure where it left off. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted November 22, 2010 Share Posted November 22, 2010 Use a recursive function. Every time you encounter another array, call the function. Otherwise, just print the <li> tags. -Dan Quote Link to comment Share on other sites More sharing options...
ThisIsPHP Posted November 22, 2010 Author Share Posted November 22, 2010 Thanks! That should work! Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted November 23, 2010 Share Posted November 23, 2010 It seems like this will work: <?php $nav = array( "title1" => "url1", "title2" => array( "sub-page1" => "url1", "sub-page2" => "url2" ), "title3" => array( "sub-page1" => "url1", "sub-page2" => "url2" ), "title4" => array( "sub-title1" => array( "sub-sub-page1" => "url1", "sub-sub-page2" => "url2", ), "sub-title2" => array( "sub-sub-page1" => "url1", "sub-sub-page2" => "url2", ) ), "title5" => "url1", "title6" => "url1" ); function print_nav ( $key, $value ) { if ( is_array($value) ) { echo "<ul>\n<li>{$value}"; foreach ( $value as $innerKey => $innerValue ) { print_nav($innerKey, $innerValue); } echo "</li>\n</ul>"; } else { echo "<ul>\n<li>{$value}</li></ul>"; } } However, your example is confusing and doesn't include all the items under title4. -Dan Quote Link to comment Share on other sites More sharing options...
ThisIsPHP Posted November 24, 2010 Author Share Posted November 24, 2010 Thanks!!! Not every title links to a page. Some will need to link to the first child page in the first sub section. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted November 24, 2010 Share Posted November 24, 2010 There's no "rule" for your display here. When is the key printed and when is it not? It seems that you can't use a recursive algorithm here because you can't tell which rule to follow. Nested loops may be your best bet. -Dan Quote Link to comment Share on other sites More sharing options...
ThisIsPHP Posted November 25, 2010 Author Share Posted November 25, 2010 I'll play with it. Thank you for your help! If I run into anything else, I'll post back up here. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.