/    Sign up×
Community /Pin to ProfileBookmark

PHP Nav/Script/Array Help

Hi. I am using a php include script to control my tabbed site navigation. I am using lists for my nav items.

I store the nav items in an array and use logic to tell the web page what the current nav item is by putting in either a <span> tag or <li class=”current”>, depending on the situation. I code each xhtml page with $thispage = x, so that the php script knows what the current page is.

Here is the navigation.php code that I came up with:

[code=php]
<?php

$navItem = array ( ‘first’=>’Home’,
‘second’=>’Photo Gallery’,
‘third’=>’Sandbox’,
‘fourth’=>’Aaron Resume’,
‘fifth’=>’Projects’,
‘sixth’=>’Writing’
);
?>
<ul id=”primary”>

<li>
<?php if ($thispage==”Home”) {
echo “<span>” . $navItem[first]. “</span>”; } else {
echo “<a href=’/index.php’>” . $navItem[first]. “</a>”; } ?>
</li>

<li>
<?php if ($thispageg==”gallery”){
echo “<a href=’/gallery’ class=’current’>” . $navItem[second] . “</a>”; } else {
echo “<a href=’gallery/index.php’>” . $navItem[second] . “</a>”; } ?>
</li>

<li>
<?php if ($thispage==”Sandbox”) {
echo “<span>” . $navItem[third] . “</span>”; } else {
echo “<a href=’/sandbox.php’>” . $navItem[third] . “</a>”; } ?>
</li>

<li>
<?php if ($thispage==”aaronResume”) {
echo “<span>” . $navItem[fourth] . “</span>”; } else {
echo “<a href=’/aaronResume.php’>” . $navItem[fourth] . “</a>”; } ?>
</li>

<li>
<?php if ($thispage==”projects”) {
echo “<span>” . $navItem[fifth] . “</span>”; } else {
echo “<a href=’/projects.php’>” . $navItem[fifth] . “</a>”; } ?>
</li>

<li>
<?php if ($thispage==”Writing”) {
echo “<span>” . $navItem[sixth] . “</span>”; } else {
echo “<a href=’/writing.php’>” . $navItem[sixth] . “</a>”; } ?>
</li>

</ul>[/code]

So, two questions:
1) Is there a more efficent way to code this so that in each if/else statemets I only have to reference the array element once?

2) If I want to use two levels of navigation, which I have already set up to do in my xHTML, would I want to use a 2 dimensional array? If so, can someone give me some hints on how to do this?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@bokehJun 23.2006 — This question made me look at a site I did ages ago to see how I did it. On that site it was done as an afterthought so the page was built with the links active and then they were stripped just before the page was sent. Most strange but it saved editing a load of menus. Maybe not such a good idea though.[code=php]$page = preg_replace(
'#(<a[^>]*href=["]+)'.$self.'(["][^>]*>)(.*?)(</a>)#is',
'<span class="grey_out">3</span>',
$page
);[/code]
Copy linkTweet thisAlerts:
@BluesManJun 24.2006 — I don't know if this is of any use for you, but anyway:

I have

-one file with the array that holds the menu (en_hormenu2.php)

-one file that generates a list from the array (create_menu.php)

-one file that determines what to do with the links (config.php)

The files are included in index.php

en_hormenu2.php looks like this this:

[code=php]
<?php
$links = array( // 1 = Main item without submenu, 2 = Mainitem with submenu 3 = Submenu 4 = Last submenu under the main item

array(1,"a","files/" ,"startside" ,"" ,"Home" ), //

array(2,"a","" ,"" ,"" ,"About us" ), //
array(3,"a","om_oss/" ,"veibeskrivelse","" ,"Where are we?" ), //
array(3,"a","om_oss/" ,"om_wilai" ,"" ,"About Wilai" ), //
array(3,"a","om_oss/" ,"om_bjorn" ,"" ,"About Bjørn" ), //
array(3,"a","om_oss/" ,"om_willi" ,"" ,"About Willi" ), //
array(3,"a","om_oss/" ,"om_seksan" ,"" ,"About Seksan" ), //
array(3,"a","om_oss/" ,"om_edith" ,"" ,"About Edith" ), //
array(3,"a","om_oss/" ,"om_harald" ,"" ,"About Harald" ), //
array(3,"a","om_oss/" ,"om_ta" ,"" ,"About Ta" ), //
array(4,"a","om_oss/" ,"om_daeng" ,"" ,"About Daeng" ), //

array(2,"a","" ,"" ,"" ,"Images" ), //
array(3,"a","images/willi/" ,"willi_pics" ,"" ,"Willi" ), //
array(3,"a","images/seksan/" ,"seksan_pics" ,"" ,"Seksan" ), //
array(3,"a","images/borgen/" ,"borgen_pics" ,"" ,"Borgen" ), //
array(3,"a","images/harald/" ,"harald_pics" ,"" ,"Harald" ), //
array(3,"a","images/thailand/" ,"thai_pics" ,"" ,"Thailand" ), //
array(4,"a","images/andre/" ,"andre_pics" ,"" ,"Other" ), //

array(2,"a","" ,"" ,"" ,"Borgen" ), //
array(3,"x","borgen/" ,"borgen_utbygging","" ,"Borgen development" ), //
array(4,"x","borgen/" ,"julebord_bv" ,"" ,"Who hosts next years Xmas party?" ), //

array(2,"a","" ,"" ,"" ,"Miscellaneous" ), //
array(3,"a","wt/" ,"wt" ,"" ,"World time" ), //
array(3,"a","files/" ,"finans" ,"" ,"Finance page" ), //
array(3,"a","files/" ,"linker" ,"" ,"Links" ), //
array(4,"a","./" ,"login" ,"" ,"*****" ), //

array(1,"a","formmailer/" ,"kontakt" ,"" ,"Contact us" ),

array(1,"a","" ,"" ,"no" ,"Norsk" )
);
?>
[/code]


create_menu.php looks like this:

[code=php]
<?php //
for($i=0; $i < count($links); $i++)
{
if ($links[$i][0] == 1) // 1 = Main item without submenu
{
echo "rn<ul>rn";
if (!empty($links[$i][4]))
{
echo "<li><h2>rn<a href="?lang=".$links[$i][4]."">".$links[$i][5]."</a></h2></li>rn";
}
else
{
echo "<li><h2>rn<a href="?page=".$i.$links[$i][4]."">".$links[$i][5]."</a></h2></li>rn";
}
echo "</ul>rn";
}

if ($links[$i][0] == 2) // 2 = Mainitem with submenu
{
echo "rn<ul>rn";
echo "<li><h2>rn<a href="?page=#">".$links[$i][5]."</a></h2>rn";
echo "<ul>rn";
}
if ($links[$i][0] == 3) // 3 = Submenu
{
echo "<li>rn<a href="?page=".$i.$links[$i][4].""class="".$links[$i][1]."">".$links[$i][5]."</a></li>rn";
}

if ($links[$i][0] == 4) // 4 = Last submenu under the main item
{
echo "<li>rn<a href="?page=".$i.$links[$i][4]."" class="".$links[$i][1]."">".$links[$i][5]."</a></li>rn";
echo "</ul>rn</li>rn</ul>rn";
}
}

$include_page = $links[$_SESSION['page']][2].$_SESSION['lang'].$links[$_SESSION['page']][3].$ext;

?>
[/code]


config.php looks like this:

[code=php]
<?php
$languages = array("no_","en_"); // Available languages
$default_language = "en_"; // If one choose an unavailable language, default to English
$css_dir = "/css/"; // Set dir-name where css files are found
$menu_style = "menuh"; // Choose menu-style
$image_dir = "/images/";

if (isset($_GET['page']) AND is_numeric($_GET['page']))
{
$_SESSION['page'] = $_GET['page'];

}
else
{
if (!isset($_SESSION['page']) || empty($_SERVER['QUERY_STRING'])) { $_SESSION['page'] = 0; }
}

if (isset ($_GET['ext'])){$ext = $_GET['ext'];}else{$ext = ".php";} // This should be removed or set to .php
if (isset($_GET['lang'])){$_SESSION['lang'] = $_GET['lang']."_";}else{ if (!isset($_SESSION['lang'])){$_SESSION['lang'] = "en_";}}
if ($_SESSION['page'] == "" || !isset($_SESSION['page'])){$_SESSION['page'] = 0;} // If page is set but empty, default to start
if (!in_array($_SESSION['lang'],$languages)){$_SESSION['lang'] = $default_language;} // If unavailable language, default to English
?>
[/code]


index.php looks like this:

[code=php]<?php
session_start();
@require("config.php");
@require($_SESSION['lang']."hormenu2.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
<link rel="shortcut icon" href="<?php echo $css_dir; ?>favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<?php echo "<title>MATHISEN online -- ".strtoupper($links[$_SESSION['page']][5])."</title>"; ?>

<!-- Begin Grab This - You'll need this external CSS file and the IE Statement below -->

<style type="text/css">@import url("<?php echo $css_dir; ?>menuh.css");</style>
<style type="text/css">@import url("<?php echo $css_dir; ?>menuv3.css");</style>
<style type="text/css">@import url("<?php echo $css_dir; ?>102.css");</style>

<!--[if IE]>
<style type="text/css" media="screen">
#menu{float:none;} /* This is required for IE to avoid positioning bug when placing content first in source. */
/* IE Menu CSS */
/* csshover.htc file version: V1.21.041022 - Available for download from: http://www.xs4all.nl/~peterned/csshover.html */
body{behavior:url(<?php echo $css_dir; ?>csshover2.htc);
font-size:100%; /* to enable text resizing in IE */
}
#menu ul li{float:left; width: 100%;}
#menu h2, #menu a{height:1%;font:bold 0.7em/1.4em arial,helvetica,sans-serif;}
</style>
<![endif]-->

<!-- End Grab This -->

</head>
<body>
<div id="container">

<div id="header">
<?php @include "header.php"; ?>
</div>

<div id="menu">
<?php
@require("create_menu.php");
?>
</div>

<div id="content">

<?php @include $include_page; ?>
</div>

</div>

<div id="footer">
<?php @include $_SESSION['lang']."lastmod.php"; ?>
</div>

</body>
</html>
[/code]


This way, you only have to change the contents of the array in en_hormenu2.php to change the menu. You may add or delete menuitems by adding or deleting "sub-arrays" in $links . Example:

If you remove this line
[code=php]
array(1,"a","files/" ,"startside" ,"" ,"Home" ), //
[/code]

the first menuitem (Home) will be gone.

This is a suggestion, and I'm sure PHP-wizards like Bokeh (no sarcasm) are able to do this much simpler and easier. If someone finds security issues with the methods involved, I'd like to hear about it, so I can do something about that.

And, by the way, sorry for the lousy indenting. Scripts should be indented. Really.
Copy linkTweet thisAlerts:
@bokehJun 24.2006 — Here's one possible approach...[code=php]<?php

$navItem = array (
'Home' => '/index.php',
'Photo Gallery' => '/gallery/index.php',
'Sandbox' => '/sandbox.php',
'Aaron Resume' => '/aaronResume.php',
'Projects' => '/projects.php',
'Writing' => '/writing.php'
);

$menu = " <ul id='primary'>n";

foreach($navItem as $linkname => $href)
{
$menu .= " <li>n <a ".($_SERVER['PHP_SELF'] == $href ? "class='current' href='#' onclick='return false'" : "href='$href'").">$linkname</a>n </li>n";
}

$menu .= "</ul>n";

?>[/code]
Now just style [I]current[/I] to not appear as a link. Here's another approach that is a lot closer to yours...[code=php]<?php

$navItem = array (
'Home' => '/index.php',
'Photo Gallery' => '/gallery/index.php',
'Sandbox' => '/sandbox.php',
'Aaron Resume' => '/aaronResume.php',
'Projects' => '/projects.php',
'Writing' => '/writing.php'
);

$menu = " <ul id='primary'>n";

foreach($navItem as $linkname => $href)
{
$menu .= " <li>n ";
if($_SERVER['PHP_SELF'] == $href)
{
$menu .= "<span>$linkname</span>";
}
else
{
$menu .= "<a href='$href'>$linkname</a>";
}
$menu .= "n </li>n";
}

$menu .= "</ul>n";

?>[/code]
×

Success!

Help @cohena spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...