Jump to content

[SOLVED] What's the best way of doing this?


eMonk

Recommended Posts

I'm working with multiple pages and would like to use :

 

<?php include("menu.php"); ?>

 

on all my pages so I won't need to update my css menu bar one by one when changes need to be made.

 

here's part of my css menu :

<li><a href="#" title="" class="current">Home</a></li>
<li><a href="#" title="">About Us</a></li>
<li><a href="#" title="">Services</a></li>
<li><a href="#" title="">Our Work</a></li>
<li><a href="#" title="">Contact Us</a></li>

 

the tag i want checked through php is the class="current" part. This visually tells the visitor where they are by changing the background color in the menu tab.

 

if the page their on is 'about us' then i would want :

<li><a href="#" title="" class="current">About Us</a></li>

 

what's the best way of doing this?

 

$class = 'page name here' at the top of the page then a if/else statement in the menu bar code?

Link to comment
https://forums.phpfreaks.com/topic/134941-solved-whats-the-best-way-of-doing-this/
Share on other sites

try

 

<?php
$this_file = strrchr($_SERVER['REQUEST_URI'], '/');
$this_file = str_replace('/', '', $this_file);
$file = explode("?", basename($this_file));
$this_file = $file[0];

echo '<ul>';

echo '<li><a href="#" title="" ';
	if($this_file == 'index.php' || $this_file == '')
			echo 'class="current"';
echo '>Home</a></li>


<li><a href="#" title="" ';
	if($this_file == 'aboutus.php')
			echo 'class="current"';
echo '>About Us</a></li>

<li><a href="#" title="" ';
	if($this_file == 'services.php')
			echo 'class="current"';
echo '>Services</a></li>

<li><a href="#" title="" ';
	if($this_file == 'ourwork.php')
			echo 'class="current"';
echo '>Our Work</a></li>

<li><a href="#" title="" ';
	if($this_file == 'contact.php')
			echo 'class="current"';
echo '>Contact Us</a></li>';

echo '</ul>';
?>

Well first you need to have the script figure out what page your on. We can use PHP_SELF

 

$page = $_SERVER['PHP_SELF'];

 

Then we will use IF statements for each menu item

 

<li><a href="#" title="" <?php if ($page='home.php'): ?>class="current"<?php endif; ?>>Home</a></li>
<li><a href="#" title="" <?php if ($page='aboutus.php'): ?>class="current"<?php endif; ?>>About Us</a></li>
<li><a href="#" title="" <?php if ($page='services.php'): ?>class="current"<?php endif; ?>>Services</a></li>
<li><a href="#" title="" <?php if ($page='work.php'): ?>class="current"<?php endif; ?>>Our Work</a></li>
<li><a href="#" title="" <?php if ($page='contact.php'): ?>class="current"<?php endif; ?>>Contact Us</a></li>

 

of course your going to need to customize it to you needs.

try

 

<?php
$this_file = strrchr($_SERVER['REQUEST_URI'], '/');
$this_file = str_replace('/', '', $this_file);
$file = explode("?", basename($this_file));
$this_file = $file[0];

echo '<ul>';

echo '<li><a href="#" title="" ';
	if($this_file == 'index.php' || $this_file == '')
			echo 'class="current"';
echo '>Home</a></li>


<li><a href="#" title="" ';
	if($this_file == 'aboutus.php')
			echo 'class="current"';
echo '>About Us</a></li>

<li><a href="#" title="" ';
	if($this_file == 'services.php')
			echo 'class="current"';
echo '>Services</a></li>

<li><a href="#" title="" ';
	if($this_file == 'ourwork.php')
			echo 'class="current"';
echo '>Our Work</a></li>

<li><a href="#" title="" ';
	if($this_file == 'contact.php')
			echo 'class="current"';
echo '>Contact Us</a></li>';

echo '</ul>';
?>

 

It is bad practice to use PHP to display HTML. Your Method seems a little too much for this task also.

Ouch, that hurts my head.

 

Here's one I wrote up:

 

<?php

class awpnav {

        function build_links($input_menu, $css_id, $css_id_ns, $uri)
        {
                if(!is_array($input_menu))
                {
                        return FALSE;
                }
                else
                {
                        foreach($input_menu as $key => $value)
                        {
                                $class = '';
                                if($uri == str_replace('/', '', $key))
                                {
                                        $class = 'id="'.$css_id.'"';
                                }
                                elseif($css_id_ns !== '')
                                {
                                        $class = 'id="'.$css_id_ns.'"';
                                }
                                $menu_final[] = '<li '.$class.'><a href="'.$key.'">'.$value.'</a></li>';
                        }
                }
                //Self-explanitory class. Returns a simple array or FALSE
                return $menu_final;
        }
}

 

Here's how you use it:

 

<?php
$menu = array
(
'/'             => 'Home',
'/hosting'      => 'Web Hosting',
'/services'     => 'Services',
'/about'        => 'About'
);

$uri = ....; // Code to figure out the KEY of the array

$css_id = 'current';
$css_id_ns = ''; //unselected item has no CSS ID

$nav = new awpnav;

$result = $nav->build_links($menu, $css_id, $css_id_ns, $uri);

 

Essentially:

 

build_links(array $menu, string $css_id, string $css_id_ns, string $uri);

 

It figures out which menu option to highlight based on the array key. The value of the key is what I actually pass to the page title before rendering the view (content). This needs to be manually edited each time you add a new menu item, but you don't have to add 5-6 lines of HTML+some code.

 

You can see it working here: http://awpti.org/

Well first you need to have the script figure out what page your on. We can use PHP_SELF

 

$page = $_SERVER['PHP_SELF'];

 

Then we will use IF statements for each menu item

 

<li><a href="#" title="" <?php if ($page='home.php'): ?>class="current"<?php endif; ?>>Home</a></li>
<li><a href="#" title="" <?php if ($page='aboutus.php'): ?>class="current"<?php endif; ?>>About Us</a></li>
<li><a href="#" title="" <?php if ($page='services.php'): ?>class="current"<?php endif; ?>>Services</a></li>
<li><a href="#" title="" <?php if ($page='work.php'): ?>class="current"<?php endif; ?>>Our Work</a></li>
<li><a href="#" title="" <?php if ($page='contact.php'): ?>class="current"<?php endif; ?>>Contact Us</a></li>

 

of course your going to need to customize it to you needs.

 

result...

 

<li><a href="#" title="" class="current">Home</a></li>
<li><a href="#" title="" class="current">About Us</a></li>
<li><a href="#" title="" class="current">Services</a></li>
<li><a href="#" title="" class="current">Our Work</a></li>
<li><a href="#" title="" class="current">Contact Us</a></li>

 

any ideas?

Well first you need to have the script figure out what page your on. We can use PHP_SELF

 

$page = $_SERVER['PHP_SELF'];

 

Then we will use IF statements for each menu item

 

<li><a href="#" title="" <?php if ($page='home.php'): ?>class="current"<?php endif; ?>>Home</a></li>
<li><a href="#" title="" <?php if ($page='aboutus.php'): ?>class="current"<?php endif; ?>>About Us</a></li>
<li><a href="#" title="" <?php if ($page='services.php'): ?>class="current"<?php endif; ?>>Services</a></li>
<li><a href="#" title="" <?php if ($page='work.php'): ?>class="current"<?php endif; ?>>Our Work</a></li>
<li><a href="#" title="" <?php if ($page='contact.php'): ?>class="current"<?php endif; ?>>Contact Us</a></li>

 

of course your going to need to customize it to you needs.

 

result...

 

<li><a href="#" title="" class="current">Home</a></li>
<li><a href="#" title="" class="current">About Us</a></li>
<li><a href="#" title="" class="current">Services</a></li>
<li><a href="#" title="" class="current">Our Work</a></li>
<li><a href="#" title="" class="current">Contact Us</a></li>

 

any ideas?

 

Forgot double equal sign:

 

<li><a href="#" title="" <?php if ($page='home.php'): ?>class="current"<?php endif; ?>>Home</a></li>
<li><a href="#" title="" <?php if ($page=='aboutus.php'): ?>class="current"<?php endif; ?>>About Us</a></li>
<li><a href="#" title="" <?php if ($page=='services.php'): ?>class="current"<?php endif; ?>>Services</a></li>
<li><a href="#" title="" <?php if ($page=='work.php'): ?>class="current"<?php endif; ?>>Our Work</a></li>
<li><a href="#" title="" <?php if ($page=='contact.php'): ?>class="current"<?php endif; ?>>Contact Us</a></li>

 

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.