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')}
broj1
Nearly a Posting Virtuoso
1,216 posts since Jan 2011
Reputation Points: 167
Solved Threads: 165
Skill Endorsements: 13
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.
broj1
Nearly a Posting Virtuoso
1,216 posts since Jan 2011
Reputation Points: 167
Solved Threads: 165
Skill Endorsements: 13
I do not exacly understand what you want to achieve. Can you describe please.
broj1
Nearly a Posting Virtuoso
1,216 posts since Jan 2011
Reputation Points: 167
Solved Threads: 165
Skill Endorsements: 13
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).
broj1
Nearly a Posting Virtuoso
1,216 posts since Jan 2011
Reputation Points: 167
Solved Threads: 165
Skill Endorsements: 13
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.
diafol
Keep Smiling
10,841 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,536
Skill Endorsements: 61
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.
diafol
Keep Smiling
10,841 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,536
Skill Endorsements: 61
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
broj1
Nearly a Posting Virtuoso
1,216 posts since Jan 2011
Reputation Points: 167
Solved Threads: 165
Skill Endorsements: 13
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>';
broj1
Nearly a Posting Virtuoso
1,216 posts since Jan 2011
Reputation Points: 167
Solved Threads: 165
Skill Endorsements: 13
You are welcome. If this ends this discussion please mark it as solved.
broj1
Nearly a Posting Virtuoso
1,216 posts since Jan 2011
Reputation Points: 167
Solved Threads: 165
Skill Endorsements: 13
Question Answered as of 9 Months Ago by
broj1
and
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.
diafol
Keep Smiling
10,841 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,536
Skill Endorsements: 61