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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.