hi
i have two menus that i use as include in index.php file:
topmenu and leftmenu

I'm using the same code in oder to show the active header switch (THIS_PAGE):

Code for the "Top menu include":

<?php
    define('THIS_PAGE', 'pagename');

    $menuitem1 = '<a href="index.php">Home</a>';
    $menuitem2 = '<a href="about.php">About</a>';
    $menuitem3 = '<a href="news.php">News</a>';
    $menuitem4 = '<a href="contact.php">Contact</a>';

    switch (THIS_PAGE) {

    case 'HOME':
    $menuitem1 = '<a style="font-weight:bold" href="#nogo">Home</a>';
    break;

    case 'About':
    $menuitem2 = '<a style="font-weight:bold" href="#nogo">About</a>';
    break;

    case 'News':
    $menuitem3 = '<a style="font-weight:bold" href="#nogo">News</a>';
    break;

    case 'Contact':
    $menuitem4 = '<a style="font-weight:bold" href="#nogo">Contact</a>';
    break;


    default:
    break;
    }

    echo ' <ul id="list-nav">
    <li>'.$menuitem1.'</li>
    <li>'.$menuitem2.'</li>
    <li>'.$menuitem3.'</li>
    <li>'.$menuitem4.'</li>
    </ul>';
    ?> 

code for the "left menu include":

<?php
    define('THIS_PAGE', 'pagename2');
    $menuitem1 = '<a href="book.php">Book Design</a>';
    $menuitem2 = '<a href="identityDesign.php">Identity Design</a>';
    $menuitem3 = '<a href="logos.php">Logos</a>';
    $menuitem4 = '<a href="packaging.php">Packaging</a>';
    $menuitem5 = '<a href="index.php">Selected Projects</a>';

    switch (THIS_PAGE) {

    case 'BookDesign':
    $menuitem1 = '<a style="font-color:#eb0089" href="#nogo">Book Design</a>';
    break;

    case 'IdentityDesign':
    $menuitem2 = '<a style="font-color:#eb0089" href="#nogo">Identity Design</a>';
    break;

    case 'Logos':
    $menuitem3 = '<a style="font-color:#eb0089" href="#nogo">Logos</a>';
    break;

    case 'Packaging':
    $menuitem4 = '<a style="font-color:#eb0089" href="#nogo">Packaging</a>';
    break;

    case 'Selected':
    $menuitem5 = '<a style="font-color:#eb0089" href="#nogo">Selected Projects</a>';
    break;

    default:
    break;
    }

    echo ' <ul>
    <li>'.$menuitem1.'</li>
    <li>'.$menuitem2.'</li>
    <li>'.$menuitem3.'</li>
    <li>'.$menuitem4.'</li>
    <li >'.$menuitem5.'</li>
    </ul>';
    ?> 

the problem is that the index.php doesn't like that i use the same "define" twice

<?php
define('THIS_PAGE', 'Selected');
include('includes/left.php');
?>
</section>

<section id="content">
 <!--top-->  
<?php
define('THIS_PAGE', 'HOME');
include('includes/menu.php');
?>

i tried to change the name of one of them but ist still doesn't work, what should i do in order to make them work.
Thanks

Define is used for defining constants and constans once defined can not be changed (by another define). You can always test if the constant has already been defined:

if(!defined('THIS_PAGE')) {define('THIS_PAGE', 'pagename')}

Also you probably meant:

define('THIS_PAGE', $pagename);

since $pagename probably comes from user's choice. I think using constans here is not appropriate since you are actualy testing for a value of a variable.

so how can i use it twice?

I do not exacly understand what you want to achieve. Can you describe please.

I am just guessing while waiting for your reply :-)

I think that included menu scripts should not have constant definitions at all since they are not needed. Each page that links point to should have a variable $this_page set to its page name on the beginning of the script (before including menus) e.g.:

$this_page='BookDesign';

Then you include the menus and use the variable in the switch:

switch ($this_page) {
case 'BookDesign':
    $menuitem1 = '<a style="font-color:#eb0089" href="#nogo">Book Design</a>';
    break;
case 'IdentityDesign':
    $menuitem2 = '<a style="font-color:#eb0089" href="#nogo">Identity Design</a>';
    break;
...

PHP will not complain since the variable can have any value at any time (unlike constant).

Basically i have two menus one on the top and one the left when i click on one particular item it should open a new page and here the item changes the color in order to appear clicked ( switch (THIS_PAGE)), the problem is that it works correctly only in the top menu and not in the left one

the following code appears in the index:

<?php
define('THIS_PAGE', 'Selected');
include('includes/left.php');
?>
</section>

<section id="content">
 <!--top-->  
<?php
define('THIS_PAGE', 'HOME');
include('includes/menu.php');
?>

And it calls the two include pages, i'm not good in PHP this is why i can't understand where is the problem, when both of them use "this page" i can't see the clicked item only if i use "this page" onece it works

Member Avatar for diafol

If the value of a constant can change within the scope of the page, by definition it is not a constant. Try using a variable instead.

So it should be written like this?

<section id="left"> 
<?php
$this_page='Selected';/************Variable**************/
include('includes/left.php');
?>
</section>

<section id="content">
 <!--top-->  
<?php
define('THIS_PAGE', 'HOME');
include('includes/menu.php');
?>
<!--/top--> 

If yes nothing changes
Thanks For all your help

Member Avatar for diafol

OK scrap that. WHy not use two constants (or variables):

define('TOP_PAGE', 'HOME');
define('SIDE_PAGE', 'Selected');

or

$top_page = 'HOME';
$side_page = 'Selected';

This nav system looks a little strange to me. SO when you select a link in the sidebar - say 'Book Design' - what's supposed to happen with regard to the top nav? Anything highlighted? To my mind the sidebar will change in accordance to the main (top) navigation, although it seems that all nav items are static.

If you could explain why you're doing it this way? Just a bit confused.

This is how I would do it:

Each page can be identified by its script name so you do not need a name for it, just put a scriptname into variable first (this is not necessary for the menu pages):

$script_name = end(explode('/',$_SERVER["SCRIPT_NAME"]));

Then include the menus. The menus have using an associative array that holds scriptnames and texts that should be displayed. This improves scalability, since you just change the arrays. See the code below and se more info in comments.

Top menu:

<?php

// array of pages for the top menu
$top_menu_items = array(

    'index.php' => 'Home',
    'about.php' => 'About',
    'news.php' => 'News',
    'contact.php' => 'Contact',
);

// start the html string for menu
$t_menu = ' <ul id="list-nav">';

// add all the ittems from the $top_menu_items array
foreach($top_menu_items as $url => $text) {

    if($script_name == $url) {

        // if the item in the array is the current page, highlight it
        $t_menu .= '<li><a style="font-weight:bold" href="#nogo">'. $text . '</a></li>';

    } else {

        // else display it as usual link
        $t_menu .= '<li><a href="' . $url . '">' . $text . '</a></li>';
    }
}

$t_menu .= '</ul>';

echo $t_menu;
?>

Left menu (very similar):

<?php

// array of pages for the left menu
$left_menu_items = array(

    'book.php' => 'Book Design',
    'identityDesign.php' => 'Identity Design',
    'logos.php' => 'Logos',
    'packaging.php' => 'Packaging',
    'index.php' => 'Selected Projects',
);

// start the html string for menu
$l_menu = ' <ul>';

// add all the ittems from the $top_menu_items array
foreach($left_menu_items as $url => $text) {

    if($script_name == $url) {

        // if the item in the array is the current page, highlight it
        $l_menu .= '<li><a style="font-color:#eb0089" href="#nogo">'. $text . ' </a></li>';

    } else {

        // else display it as usual link
        $l_menu .= '<li><a href="' . $url . '">' . $text . '</a></li>';
    }
}

$l_menu .= '</ul>';

echo $l_menu;
?>

index.php

<?php
    // the name of the current script
    $script_name = end(explode('/',$_SERVER["SCRIPT_NAME"]));

    // include the menus (do it within sections as per your design)
    include('includes/menu.php');
    include('includes/left.php');

    echo "<p>The script name is $script_name</p>";
?>

book.php

<?php
    // the name of the current script
    $script_name = end(explode('/',$_SERVER["SCRIPT_NAME"]));

    // include the menus (do it within sections as per your design)
    include('includes/menu.php');
    include('includes/left.php');

    echo "<p>The script name is $script_name</p>";
?>

etc

First of all thank you for your time and help!!!!

I tried to use your code, and it looks that it works fine it realizes that we are in index.php, The "home" menu item is bold but in the left side the "Selected Projects" is not red.

in other words it behaves as before

It works here. It is probably the css statement for font color which should be color and not font-color (which is an old html equivalent).

$l_menu .= '<li><a style="color:#eb0089" href="#nogo">'. $text . ' </a></li>';

it works!!!!!
thanks
it was such a stupid error, thanks again

You are welcome. If this ends this discussion please mark it as solved.

i have another question regarding this table, should i open another post? This is not the same subject

Member Avatar for diafol

Difficult to say, your thread title is 'repetition of "define"', so other users will access it to find info regarding that query. If the question is connected to that issue, post it here, otherwise a new thread may be appropriate. As you've already marked this issue solved, I imagine that a new thread is indeed what you require, as other contributors may be more inclined to offer suggestions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.