Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Menu from array little bit of help needed 1

Status
Not open for further replies.

4x4uk

Technical User
Apr 30, 2002
381
GB
Been working on a simple script that generates a website navigation bar from an array, I'm fairly happy with the results and its ability to handle multiple sub menus, however the one thing I cannot figure out and could do with a little bit of help on is how to configure the array so a menu that has a sub menu can also have a link as well. Current code
Code:
<?php
// The menu bar is built from the array below.
$pageArray = array(
	"Home" => array( 
        "How to join" => "/index.php?opt=1",
		"Legal" => array(
						"Copyright&nbsp;Information" => "index.php?opt=2",
						"Privacy&nbsp;Statement" => "index.php?opt=3",
						"Disclaimer" => "index.php?opt=4",
						"Club Rules" => "index.php?opt=5",),
		"Contact Us" => "index.php?opt=6&amp;ec=info",
		),
	"Calendar" => "/calendar/",
	"Classifieds" => "/classifieds/",
    "Info" => array(
		"Downloads" => "/downloads/",
		"Calculators" => array(
		"Tyre Size Calculator" => "index.php?opt=6",
		"Wheel Offset Calculator" => "index.php?opt=7",
		"Gear Ratio Calculator" => "index.php?opt=8",)
        ),
	"Forums" => "/forum/",
	"Photo Gallery" => "/photos/",
		);
    
function navGen($pageArray) {
// Walk through $pageArray and generate the navigation list.
	echo "<ul>\n";
		foreach ($pageArray as $name => $url) {
			if (is_array($url)) {
				echo "<li>".$name."\n";
				navGen($url);
				echo"</li>\n";
            } else {
					echo "<li><a href=\"$url\">".$name."</a></li>\n";
					}
        }
		echo "</ul>\n";
}
?>
<div id="navbar">
<?= navGen($pageArray); ?>
</div>
Many thanks
Ian

4x4 UK | The Ultimate Off Road Club for all types of 4x4
 
Hi

I would use some special keys, for example empty strings :
Code:
[navy]$pageArray[/navy] [teal]=[/teal] [b]array[/b][teal]([/teal]
  [green][i]"Home"[/i][/green] [teal]=>[/teal] [b]array[/b][teal]([/teal]
     [highlight][green][i]''[/i][/green][teal]=>[/teal][green][i]'/index.php?whatever=1'[/i][/green][teal],[/teal][/highlight]
    [green][i]"How to join"[/i][/green] [teal]=>[/teal] [green][i]"/index.php?opt=1"[/i][/green][teal],[/teal]
    [green][i]"Legal"[/i][/green] [teal]=>[/teal] [b]array[/b][teal]([/teal]
      [highlight][green][i]''[/i][/green][teal]=>[/teal][green][i]'/index.php?whatever=11'[/i][/green][teal],[/teal][/highlight]
      [green][i]"Copyright&nbsp;Information"[/i][/green] [teal]=>[/teal] [green][i]"index.php?opt=2"[/i][/green][teal],[/teal]
      [green][i]"Privacy&nbsp;Statement"[/i][/green] [teal]=>[/teal] [green][i]"index.php?opt=3"[/i][/green][teal],[/teal]
      [green][i]"Disclaimer"[/i][/green] [teal]=>[/teal] [green][i]"index.php?opt=4"[/i][/green][teal],[/teal]
      [green][i]"Club Rules"[/i][/green] [teal]=>[/teal] [green][i]"index.php?opt=5"[/i][/green][teal],[/teal]
    [teal]),[/teal]
    [green][i]"Contact Us"[/i][/green] [teal]=>[/teal] [green][i]"index.php?opt=6&amp;ec=info"[/i][/green][teal],[/teal]
  [teal]),[/teal]
  [green][i]"Calendar"[/i][/green] [teal]=>[/teal] [green][i]"/calendar/"[/i][/green][teal],[/teal]
  [green][i]"Classifieds"[/i][/green] [teal]=>[/teal] [green][i]"/classifieds/"[/i][/green][teal],[/teal]
  [green][i]"Info"[/i][/green] [teal]=>[/teal] [b]array[/b][teal]([/teal]
    [highlight][green][i]''[/i][/green][teal]=>[/teal][green][i]'/index.php?whatever=2'[/i][/green][teal],[/teal][/highlight]
    [green][i]"Downloads"[/i][/green] [teal]=>[/teal] [green][i]"/downloads/"[/i][/green][teal],[/teal]
    [green][i]"Calculators"[/i][/green] [teal]=>[/teal] [b]array[/b][teal]([/teal]
      [highlight][green][i]''[/i][/green][teal]=>[/teal][green][i]'/index.php?whatever=21'[/i][/green][teal],[/teal][/highlight]
      [green][i]"Tyre Size Calculator"[/i][/green] [teal]=>[/teal] [green][i]"index.php?opt=6"[/i][/green][teal],[/teal]
      [green][i]"Wheel Offset Calculator"[/i][/green] [teal]=>[/teal] [green][i]"index.php?opt=7"[/i][/green][teal],[/teal]
      [green][i]"Gear Ratio Calculator"[/i][/green] [teal]=>[/teal] [green][i]"index.php?opt=8"[/i][/green][teal],[/teal]
    [teal])[/teal]
  [teal]),[/teal]
  [green][i]"Forums"[/i][/green] [teal]=>[/teal] [green][i]"/forum/"[/i][/green][teal],[/teal]
  [green][i]"Photo Gallery"[/i][/green] [teal]=>[/teal] [green][i]"/photos/"[/i][/green][teal],[/teal]
[teal]);[/teal]

[b]function[/b] [COLOR=darkgoldenrod]navGen[/color][teal]([/teal][navy]$pageArray[/navy][teal])[/teal]
[teal]{[/teal]
  [b]echo[/b] [green][i]"<ul>\n"[/i][/green][teal];[/teal]
  [b]foreach[/b] [teal]([/teal][navy]$pageArray[/navy] [b]as[/b] [navy]$name[/navy] [teal]=>[/teal] [navy]$url[/navy][teal])[/teal] [teal]{[/teal]
    [b]if[/b] [teal]([/teal][COLOR=darkgoldenrod]is_array[/color][teal]([/teal][navy]$url[/navy][teal]))[/teal] [teal]{[/teal]
      [highlight][b]if[/b] [teal]([/teal][COLOR=darkgoldenrod]array_key_exists[/color][teal]([/teal][green][i]''[/i][/green][teal],[/teal][navy]$url[/navy][teal]))[/teal] [b]echo[/b] [green][i]"<li><a href=\"{$url['']}\">"[/i][/green][teal].[/teal][navy]$name[/navy][teal].[/teal][green][i]"</a>\n"[/i][/green][teal];[/teal][/highlight]
      [highlight][b]else[/b][/highlight] [b]echo[/b] [green][i]"<li>"[/i][/green][teal].[/teal][navy]$name[/navy][teal].[/teal][green][i]"\n"[/i][/green][teal];[/teal]
      [COLOR=darkgoldenrod]navGen[/color][teal]([/teal][navy]$url[/navy][teal]);[/teal]
      [b]echo[/b][green][i]"</li>\n"[/i][/green][teal];[/teal]
    [teal]}[/teal] [b]else[/b] [teal]{[/teal]
      [highlight][b]if[/b] [teal]([/teal][navy]$name[/navy][teal])[/teal][/highlight] [b]echo[/b] [green][i]"<li><a href=\"$url\">"[/i][/green][teal].[/teal][navy]$name[/navy][teal].[/teal][green][i]"</a></li>\n"[/i][/green][teal];[/teal]
    [teal]}[/teal]
  [teal]}[/teal]
  [b]echo[/b] [green][i]"</ul>\n"[/i][/green][teal];[/teal]
[teal]}[/teal]


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top