Get current url excluding Included file names - php

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?

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.

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!

Related

PHP echo last Array values

Im trying few hours repair my code but its always fail.
I have array in file:
$liquidyLista7 = array(
'Arbuz' => array(
'Wyraźny arbuz, soczysty arbuz, naprawde chyba najlepszy z oferty',
'Delikatny arbuz.',
'odświeżajacy arbuz',
'Bardzo delikatny, mniej wyrażny arbuz'
),
// and much more...
... i want to echo all values from each arrays but my "bad epic" code not work :/
function pokazOpisyVolish($liquidyLista7)
{
$i = 0;
$select = '<div id="volishPage'.$i.'" class="tab-pane fade"><ul class="comment-list">';
foreach($liquidyLista7 as $key => $record) {
$i++;
if (is_array($record)) {
$select .= '<li><p class="desc">'.$key.'</p>';
$select .= pokazOpisyVolish($record);
$select .= '</li>';
} else {
$select .= '<li><p class="desc">'.$record.'</p></li>';
}
}
$select .= '</ul></div>';
return $select;
echo pokazOpisyVolish($liquidyLista7);
Pls visit my test website and check how it works in "Mr Jack" (simple HTML - not PHP) but in "Volish" is my PHP code... :(
may be you are looking for some thing like this. the link you gave
uses plugin and css to display the result onto the webpage,
i can show you how to do it, but after that you have use your own css to display the result, i am thinking you might have add some div
name
just run this code in separate file, and you are good, understand what's happening and than add the div
<?php
$liquidyLista7 = array(
'Arbuz' => array(
'Wyraźny arbuz, soczysty arbuz, naprawde chyba najlepszy z oferty',
'Delikatny arbuz.',
'odświeżajacy arbuz',
'Bardzo delikatny, mniej wyrażny arbuz'
),
'newArtist' => array(
'Wyraźny arbuz, soczysty arbuz, naprawde chyba najlepszy z oferty',
'Delikatny arbuz.',
'odświeżajacy arbuz',
'Bardzo delikatny, mniej wyrażny arbuz'
)
);
//get the link from url
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
foreach ($liquidyLista7 as $key => $record) {
//ad artist name to the current url
echo "<a href=$actual_link?artistName=$key>$key</a><br>";
if(isset($_GET['artistName'])){
if(is_array($record)){
echo "<ul>";
foreach($liquidyLista7[$key] as $value)
if($_GET['artistName']==$key)
echo "<li>$value</li>";
}
echo "</ul>";
}
}
?>

How can I remove nested nav from all top-level listed items, except for Services in a PHP array?

I am trying to remove the nested list ($subPages) from displaying under all of the top-level lists ($pages), except for Services; however my code is allowing the nested list items to display under all three top-level list items.
Can anyone help me remove the nested list items from all top-level list items, except for one?
NB. I am new to PHP, and would like to just stick to PHP without any other languages. My PHP code may be in efficient; if you spot room for improvement, let me know.
<nav class="navBar wAuto fRight positAb">
<?php
$pages = array(
'/about/' => 'About',
'/services/' => 'Services',
'/book-a-service/' => 'Book a service'
);
$subPages = array(
'/services/bathroom-installation' => 'Bathroom installation',
'/services/boiler-repair' => 'Boiler repair',
'/services/boiler-service' => 'Boiler service',
'/services/gas-oil' => 'Gas & oil',
'/services/heat-recovery' => 'Heat recovery',
'/services/heating-plumbing' => 'Heating and plumbing',
'/services/rainwater-harvesting' => 'Rainwater harvesting',
'/services/solar-solutions' => 'Solar solutions',
'/services/underfloor-heating' => 'Underfloor heating'
);
$activePage = $_SERVER['REQUEST_URI'];
echo '<ul>';//open nav
foreach( $pages as $url => $anchor){
$activeClass = ($_SERVER['REQUEST_URI'] == $url) ? " active" : "";
echo '<li class="fLeft"><a class="font13'.$activeClass.'" href="'.$url.'"';
if($url == '/services/'){
echo 'id="parent"';
}
if($url == '/book-a-service/')
echo 'id="ctaUHP"';
echo '>'.$anchor.'</a>';
//Open nested nav
if(isset($url) == '/services/'){
echo '<ul class="wAuto positAb">';
foreach ($subPages as $url => $anchor){
echo '<li class="fullWidth"><a class="font10" href="'.$url.'"';//open nested nav
echo '>'.$anchor.'</a></li>';
}
};
echo '</li></ul>'; //close nested nav
}
//Close submenu
echo '</ul>';
//close nav
?>
</nav>
In the code for open nested nav, change if(isset($url) == '/services/') to if($url == '/services/'){
Explanation: You don't need to verify if the $url is set, you just need to print the nested nav when your $url == '/services/', that's why you need to remove the function isset() :)
EDIT
You lacked some {} and I even rearranged some of your code, so now looks like this :)
echo '<ul>'; //open nav
foreach( $pages as $url => $anchor) {
$activeClass = ($_SERVER['REQUEST_URI'] == $url) ? " active" : "";
echo '<li class="fLeft"><a class="font13'.$activeClass.'" href="'.$url.'"';
if($url == '/services/') {
echo 'id="parent"';
}
if($url == '/book-a-service/') {
echo 'id="ctaUHP"';
}
echo '>'.$anchor.'</a>';
//verify if exists nested nav
if($url == '/services/') {
echo '<ul class="wAuto positAb">'; //open nested nav
foreach ($subPages as $url => $anchor) {
echo '<li class="fullWidth"><a class="font10" href="'.$url.'">'.$anchor.'</a></li>';
}
echo '</ul>'; //close nested nav
}
echo '</li>';
}
echo '</ul>'; //close nav

How to do template tree in PHP?

I use this recursive function to build a tree in PHP:
private function getSubscribesTemplate($main = array(), $child = array()) {
foreach ($main as $key => $val) {
if($val->SubscrubeToUsersIdNote > 0){
echo 'Subcategory'. $val->NameCategorysubscribetotype;
} else {
echo '<div class="itm">
<div class="nav-header nav-header-little">' . $val->NameCategorysubscribetotype . ' | Subscribe</div>
<div>// HERE ALL SUBCATEGORIES</div>
</div>';
}
if (isset($child[$val->SubscrubeToUsersType])) {
$this->getSubscribesTemplate($child[$val->SubscrubeToUsersType], array());
}
}
}
My problem is that in the result function I need to get an HTML template like this:
<div class="itm">
<div class="nav-header nav-header-little">Music | Subscribe</div>
<div>Jazz</div>
<div>Rock</div>
<div>Pop</div>
</div>
So to create the parent block with child <div>s. What am I doing wrong in my code?
Move the if block:
<div>';
// HERE ALL SUBCATEGORIES
if (isset($child[$val->SubscrubeToUsersType])) {
$this->getSubscribesTemplate($child[$val->SubscrubeToUsersType], array());
}
echo '</div>';

how to add active class in current page in CakePhp

i have a problem similar to this question
How to identify active menu link in CakePHP
i have a page in my default.ctp file in which i want to add 'active' class on links. how can i identify the current url of the page and then apply the class on link.. i have followed the answer also there which is
$url = $this->Html->url('INPUT_THE_URL') ;
$active = $this->request->here == $url? true: false;
i dont know how can i do this in my code .. sorry for asking as i am newbie in cakephp .. here is my code
**default.ctp file**
<li>
<?php echo $this->Html->link('Dashboard', array('controller'=>'users','action' => 'controlpanel'), array('title' => 'Dashboard','class' => 'shortcut-dashboard'));?></li>
<li> <?php echo $this->Html->link('Contacts', array('controller'=>'contacts','action' => 'index'), array('title' => 'Contacts','class' => 'shortcut-contacts'));?></li>
i want to add a class with li like this
<li class = 'active''>
This is a simple logic as follows
<li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='controlpanel') )?'active' :'inactive' ?>">
<?php echo $this->Html->link('Dashboard', array('controller'=>'users','action' => 'controlpanel'), array('title' => 'Dashboard','class' => 'shortcut-dashboard'));?>
</li>
<li class="<?php echo (!empty($this->params['action']) && ($this->params['action']=='index') )?'active' :'inactive' ?>">
<?php echo $this->Html->link('Contacts', array('controller'=>'contacts','action' => 'index'), array('title' => 'Contacts','class' => 'shortcut-contacts'));?></li>
If you have a different controller and you have declared a method with same name, and the above code is not working, then you can do the following:
<li class="<?php echo (($this->params['controller']==='hotels')&& ($this->params['action']=='view') )?'active' :'' ?>" >
<?php echo $this->Html->link('Hotels', array('controller' => 'hotels', 'action' => 'view')); ?>
</li>
<li class="<?php echo (($this->params['controller']==='packages')&& ($this->params['action']=='view') )?'active' :'' ?>" >
<?php echo $this->Html->link('Packages', array('controller' => 'packages', 'action' => 'view')); ?>
</li>
Here view method is declared in different controller. i hope it will be helpful for you.
Not to revive a dead post, but this is what I do (which I believe is a bit cleaner and faster and a bit more manageable)
I create an element that has an array of pages, then I check against each item in the array to see if it is the current page. If it is I add the active class.
I can then call this element from anywhere.
// Changed the line below to a multi-dimensional array to cater for different controllers and actions
//$mypages = array('Home','About','Pricing','FAQs','Contact');
$mypages = array(
array('controller'=>'controller1','action'=>'action1','name'=>'name1'),
array('controller'=>'controller2','action'=>'action2','name'=>'name2
')
);
foreach ($mypages as $page ){
// Changed to account for controller and action
//$currentPage = isset($this->params['pass'][0]) ?$this->params['pass'][0] : "";
$controller = isset($this->request->params['controller'])?$this->request->params['controller']: "";
$action= isset($this->request->params['action'])?$this->request->params['action']: "";
if (strtolower($page['controller']) == $controller && strtolower($page['action']) == $action) {
echo "<li class='active'>" . $this->Html->link($page,array("controller"=>"pages", "action"=>strtolower($page))) . "</li>" ;
}
else {
echo "<li>" . $this->Html->link($page,array("controller"=>"pages", "action"=>strtolower($page))) . "</li>";
}
}

functions.php causes header error in Wordpress

I have a problem with my wordpress theme, the functions.php file ouputs ome bad header error:
Warning: Cannot modify header information - headers already sent by (output started at /home/slavisap/public_html/femarkets/wordpress/wp-content/themes/Flex E/functions.php:67) in /home/slavisap/public_html/femarkets/wordpress/wp-includes/pluggable.php on line 934
It happens randomly, when I try to visit some page or create another one from admin.
this is my functions.php:
<?php
if (function_exists('register_nav_menus')) {
register_nav_menus(
array(
'primary' => 'Primary Header Nav',
'footer_menu' => 'Footer Menu',
'exploring' => 'Exploring Page Menu',
'using' => 'Using Page Menu',
'downloading' => 'Downloading Page Menu'
)
);
}
function get_breadcrumbs() {
global $wp_query;
if (!is_home()) {
// Start the UL
echo '<ul class="breadcrumbs">';
// Add the Home link
echo '<li>' . get_bloginfo('name') . '</li>';
if (is_category()) {
$catTitle = single_cat_title("", false);
$cat = get_cat_ID($catTitle);
echo "<li> / " . get_category_parents($cat, TRUE, " / ") . "</li>";
} elseif (is_archive() && !is_category()) {
echo "<li> / Archives</li>";
} elseif (is_search()) {
echo "<li> / Search Results</li>";
} elseif (is_404()) {
echo "<li> / 404 Not Found</li>";
} elseif (is_single()) {
$category = get_the_category();
$category_id = get_cat_ID($category[0]->cat_name);
echo '<li> / ' . get_category_parents($category_id, TRUE, " / ");
echo the_title('', '', FALSE) . "</li>";
} elseif (is_page()) {
$post = $wp_query->get_queried_object();
if ($post->post_parent == 0) {
echo "<li> / " . the_title('', '', FALSE) . "</li>";
} else {
$title = the_title('', '', FALSE);
$ancestors = array_reverse(get_post_ancestors($post->ID));
array_push($ancestors, $post->ID);
foreach ($ancestors as $ancestor) {
if ($ancestor != end($ancestors)) {
echo '<li> » ' . strip_tags(apply_filters('single_post_title', get_the_title($ancestor))) . '</li>';
} else {
echo '<li> » ' . strip_tags(apply_filters('single_post_title', get_the_title($ancestor))) . '</li>';
}
}
}
}
// End the UL
echo "</ul>";
}
}
?>
my website URL: http://slavisaperisic.com/femarkets/wordpress/
do you know what I'm doing wrong?
Since there is no line 67 in the functions.php file you posted (at least, the version I copy/pasted into my editor), I'm guessing you have some extra white-space at the beginning and/or end of your functions.php file (before the opening <?php or after the closing ?> tags).
Any characters in a PHP file outside the <?php ?> tags is treated as standard output and immediately written to STDOUT (or to the web servers output stream) and in a web server scenario this will cause headers to be sent, since what you are outputting is the body of the response.
Make sure that the opening < is the first character in the file, and the closing > is the last character in the file.
You actually don't need to include a closing ?> tag if all the data in the file is PHP code, and omitting it will help to avoid problems like this...

Categories