Jump to content

[SOLVED] PHP Menu System


Lamez

Recommended Posts

I have a CSS menu and it when you are on the "Home" page, on the menu it is selected. Well I have a dynamic php website, and I want to know how I can tell if the user is on that page so I can use the select feature. I cannot wrap my head on something that could be so simple, I want it to be easily manipulated without touching the source code. How would one go about this?

Link to comment
https://forums.phpfreaks.com/topic/139576-solved-php-menu-system/
Share on other sites

the way I've done this is to have a variable that is set at the top of every page. a little tedious but works absolute best.

 

Have something like $thisPage = 'login'; or something.

 

then use that in your generated menu and check for the flag in $thisPage to decided whether or not to output something like {class="active"} in your li tag or whatever you're using.

so that sounds great, but I have to write a new if statement every time I make a page.

 

here is my current code:

<?php
if($page == "register"){
$reg = 'class="current"';
}
if($page == "home"){
$home = 'class="current"';
}
echo'
<div id="menu">
<ul id="si-menu">
<li><a href="index.php" title="Home" '.$home.'>Home</a></li>
<li><a href="login.php" title="Login" '.$log.'>Login</a></li>
<li><a href="register.php" title="Register" '.$reg.'>Register</a></li>
</ul>
</div>';
?>

 

is there a way around this?

Or put that into an included file and just include that file...

 

pageChange.php

<?php
if($page == "register"){
$reg = 'class="current"';
}elseif($page == "home"){
$home = 'class="current"';
}// etcc....
?>

 

home.php

<?php
include('pageChange.php');

echo'
<div id="menu">
<ul id="si-menu">
<li><a href="index.php" title="Home" '.$home.'>Home</a></li>
<li><a href="login.php" title="Login" '.$log.'>Login</a></li>
<li><a href="register.php" title="Register" '.$reg.'>Register</a></li>
</ul>
</div>';
?>

 

Should work.

lol, writing the if statement is a bit of annoyance, that is what I am saying. I want a automated system.

 

You will have to write the if statement one way or the other. There is no avoiding that.

 

Using my first method, however, you could do this:

 

<?php
$selected = str_replace(".php", "", $_SERVER['PHP_SELF']);
// now you should have "home" in there
$pages[$selected] = 'class="current"';

echo'
<div id="menu">
<ul id="si-menu">
<li><a href="index.php" title="Home" '.$pages['home'].'>Home</a></li>
<li><a href="login.php" title="Login" '.$pages['login'].'>Login</a></li>
<li><a href="register.php" title="Register" '.$pages['register'].'>Register</a></li>
</ul>
</div>';
?>

 

To avoid the notice errors, if you want to, do this.

 

<?php
$pages = array("home" => "", "login" => "", "register" => "");
$selected = str_replace(".php", "", strtolower($_SERVER['PHP_SELF']));
$pages[$selected] = 'class="current"';

echo'
<div id="menu">
<ul id="si-menu">
<li><a href="index.php" title="Home" '.$pages['home'].'>Home</a></li>
<li><a href="login.php" title="Login" '.$pages['login'].'>Login</a></li>
<li><a href="register.php" title="Register" '.$pages['register'].'>Register</a></li>
</ul>
</div>';
?>

 

That should work without the if statement.

you want automated?

 

$page = 'Home';

$menu['Home'] = 'index';
$menu['Login'] = 'login';
$menu['Register'] = 'register';

echo "<ul>\n";
foreach ($menu as $key => $value) {
echo ($page == $key) ? "\t".'<li><a href="'. $value .'.php" title="'. $key .'" class="current">'. $key .'</a></li>'."\n" : "\t".'<li><a href="'. $value .'.php" title="'. $key .'">'. $key .'</a></li>'."\n";
}
echo "</ul>\n";

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.