Jump to content

Navigation Menu with PHP Arrays


ThisIsPHP

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/219503-navigation-menu-with-php-arrays/
Share on other sites

$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.

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.