Jump to content

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";

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.