Lamez Posted January 5, 2009 Share Posted January 5, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/139576-solved-php-menu-system/ Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 $_SERVER['PHP_SELF'] That might work? Quote Link to comment https://forums.phpfreaks.com/topic/139576-solved-php-menu-system/#findComment-730159 Share on other sites More sharing options...
castis Posted January 5, 2009 Share Posted January 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/139576-solved-php-menu-system/#findComment-730161 Share on other sites More sharing options...
Lamez Posted January 5, 2009 Author Share Posted January 5, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/139576-solved-php-menu-system/#findComment-730192 Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/139576-solved-php-menu-system/#findComment-730198 Share on other sites More sharing options...
Lamez Posted January 5, 2009 Author Share Posted January 5, 2009 lol, writing the if statement is a bit of annoyance, that is what I am saying. I want a automated system. Quote Link to comment https://forums.phpfreaks.com/topic/139576-solved-php-menu-system/#findComment-730200 Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/139576-solved-php-menu-system/#findComment-730206 Share on other sites More sharing options...
castis Posted January 5, 2009 Share Posted January 5, 2009 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"; Quote Link to comment https://forums.phpfreaks.com/topic/139576-solved-php-menu-system/#findComment-730208 Share on other sites More sharing options...
Lamez Posted January 5, 2009 Author Share Posted January 5, 2009 wow thanks, I was looking for ideas, but that works Quote Link to comment https://forums.phpfreaks.com/topic/139576-solved-php-menu-system/#findComment-730371 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.