Reputation: 1401
I have a file named administrator1.php
and inside it test.php
is included. So code inside administrator1.php
is
<?php include 'test.php'; ?>
I have a dynamic navigation menu inside test.php
. Code is
<?php
$menu = array(
'users' => array('text'=>'USERS', 'url'=>'administrator1.php'),
'createProfile' => array('text'=>'CREATE PROFILE', 'url'=>'administrator2.php'),
'payment' => array('text'=>'PAYMENTS', 'url'=>'administrator3.php'),
'defense' => array('text'=>'DEFENSE', 'url'=>'administrator4.php'),
'progressReport' => array('text'=>'PROGRESS REPORT', 'url'=>'administrator5.php'),
);
class Nav {
function GenerateMenu($items) {
$html = "<ul>";
foreach($items as $item) {
if(stristr(__FILE__,$item['url'])===FALSE) {
$html .= "<li><a href='{$item['url']}' ><span><B>{$item['text']}</B></span> </a> </li>";
}
else {
$html .= "<li class='active'><a href='{$item['url']}' ><span><B>{$item['text']}.nnn</B></span> </a></li>";
}
}
$html .= "</ul>";
return $html;
}};
echo Nav::GenerateMenu($menu);
?>
And I have called the Nav
method inside the same file.
Problem here is, the whole navigation bar will be printed, but selected item's ('USERS') CSS class active
will not be called. But if I replace this whole code in administrator1.php
file without including a test.php
file it works fine. So I guess it is a problem to file path of the included file. And is it because I have used __FILE__
to get current path? Then how can I get current file path excluding included files names in the path?
Upvotes: 2
Views: 271
Reputation: 1401
Found the answer!
Replaced __FILE__
with $_SERVER["PHP_SELF"]
. So the code is;
class Nav {
function GenerateMenu($items) {
$html = "<ul>";
foreach($items as $item) {
if(stristr($_SERVER["PHP_SELF"],$item['url'])===FALSE) {
$html .= "<li><a href='{$item['url']}' ><span><B>{$item['text']}</B></span> </a> </li>";
}
else {
$html .= "<li class='active'><a href='{$item['url']}' ><span><B>{$item['text']}.</B></span> </a></li>";
}
}
$html .= "</ul>";
return $html;
}};
$nav_ob = new Nav();
echo $nav_ob->GenerateMenu($menu);
Now it works as i wanted! Thank you everyone for the help given!
Upvotes: 1
Reputation: 2265
For calling the static function we can use ::
operator. But for accessing the non-static member of the class you should make the object, like -
$nav_ob = new Nav();
echo $nav_ob->GenerateMenu($menu);
And there s problem, when you are creating array -
$menu = array(
'users' => array('text'=>'USERS', 'url'=>'administrator1.php'),
'createProfile' => array('text'=>'CREATE PROFILE', 'url'=>'administrator2.php'),
'payment' => array('text'=>'PAYMENTS', 'url'=>'administrator3.php'),
'defense' => array('text'=>'DEFENSE', 'url'=>'administrator4.php'),
'progressReport' => array('text'=>'PROGRESS REPORT', 'url'=>'administrator5.php'), //Here
);
Comma after the last element. It should be -
$menu = array(
'users' => array('text'=>'USERS', 'url'=>'administrator1.php'),
'createProfile' => array('text'=>'CREATE PROFILE', 'url'=>'administrator2.php'),
'payment' => array('text'=>'PAYMENTS', 'url'=>'administrator3.php'),
'defense' => array('text'=>'DEFENSE', 'url'=>'administrator4.php'),
'progressReport' => array('text'=>'PROGRESS REPORT', 'url'=>'administrator5.php')
);
UPDATED
<?php
$menu = array(
'users' => array('text'=>'USERS', 'url'=>'administrator1.php'),
'createProfile' => array('text'=>'CREATE PROFILE', 'url'=>'administrator2.php'),
'payment' => array('text'=>'PAYMENTS', 'url'=>'administrator3.php'),
'defense' => array('text'=>'DEFENSE', 'url'=>'administrator4.php'),
'progressReport' => array('text'=>'PROGRESS REPORT', 'url'=>'administrator5.php')
);
class Nav {
function GenerateMenu($path, $items) {
$html = "<ul>";
foreach($items as $item) {
if(stristr($path,$item['url'])===FALSE) {
$html .= "<li><a href='{$item['url']}' ><span><B>{$item['text']}</B></span> </a> </li>";
}
else {
$html .= "<li class='active'><a href='{$item['url']}' ><span><B>{$item['text']}.nnn</B></span> </a></li>";
}
}
$html .= "</ul>";
return $html;
}};
?>
And on each administrator.php file after including the above php file call the method like -
$nav_ob = new Nav();
echo $nav_ob->GenerateMenu(__FILE__, $menu);
Hope this will help.
Upvotes: 2