keeping active page using include_once to include site navigation - php

Another, what could be a silly, question. I have a very in-depth navigation menu on my clients site. I don't mind doing the hard slog now changing all the pages and links to .php - will save mucho time in the future. I want to use <?php include_once("nav.php"); ?> to pull this into every page. At the moment the parent item that is active will be highlighted depending on which child page is being viewed. The menu structure is currently just part of each of the .html page.
HTML markup:
<div id="navigation-wrapper">
<nav id="main-navigation">
<ul class="main-menu">
<li><span aria-hidden="true" class="li_display nav_icon"></span>About
<ul>
<li>Corporate Profile</li>
<li>Board of Directors</li>
<li>Operating Divisions</li>
<li>Organisational Chart</li>
<li>Corporate Documentation</li>
</ul>
</li>
<li><span aria-hidden="true" class="li_news nav_icon"></span>Investor
<ul>
<li>Overview</li>
<li>Financial</li>
<li>News</li>
<li>Events</li>
<li>General</li>
<li>Register for Alerts</li>
</ul>
</li>
<li><span aria-hidden="true" class="li_bulb nav_icon"></span>Products
<ul>
<li>Mecer</li>
<li>Hardware
<ul>
<li>APC</li>
<li>Acer</li>
<li>ASUS</li>
<li>E-Beam</li>
<li>Fujitsu</li>
<li>Huawei</li>
<li>Lenovo</li>
<li>Microsoft</li>
<li>NEC</li>
<li>Samsung</li>
<li>Toshiba</li>
</ul>
</li>`enter code here`
<li>Mustek Energy
<ul>
<li>Solar Panels</li>
<li>LED Lighting Solutions</li>
</ul>
</li>
<li>Mustek Solutions
<ul>
<li>Mustek Cloud Computing</li>
<li>Mustek Digital Signage</li>
<li>Mustek POS Solutions</li>
<li>Mustek Security Technology</li>
</ul>
</li>
<li>Networking Solutions
<ul>
<li>D-Link</li>
<li>Huawei</li>
<li>Miniflex</li>
<li>NEC</li>
<li>NComputing</li>
</ul>
</li>
<li>Printing Solutions
<ul>
<li>Brother</li>
<li>Epson</li>
</ul>
</li>
<li>Complete Product Catalogue</li>
</ul>
</li>
<li><span aria-hidden="true" class="li_settings nav_icon"></span>Support
<ul>
<li>DealerNet</li>
<li>Dealer Application Form</li>
<li>Dealer Locator</li>
<li><a href="http://downloadcenter.mustek.co.za/list.php?dir=Repository" target=_blank>Download Latest Drivers</a></li>
<li>Mecer EyeSPY</li>
</ul>
</li>
<li><span aria-hidden="true" class="li_phone nav_icon"></span>Contact
<ul>
<li>Branches</li>
<li>Customer Liaison</li>
<li>Investors</li>
<li>Sales Queries</li>
<li>Technical Queries</li>
<li>Careers</li>
</ul>
</li>
</ul>
</nav>
</div>
As you can see the class="active-page" dictates which parent icon to show. I read somewhere that by adding <?php $page = "page-name"; ?> it will say which page this is and will act like the class="active-page". Do I need to create an id for the page in the nav.php file that it will reference?
Sorry guys, but my knowledge of PHP is very limited, but I am trying to learn the basics on the fly. Any help is greatly appreciated!

Just use an if-statement to add "active-page" as class to the current active page:
<li><span aria-hidden="true" class="li_display nav_icon"></span>About
You then need to define a constant ACTIVE_PAGE before including your navigation.
<?php
define ('ACTIVE_PAGE', "About");
include ('yournav.php');
?>
This should work and is okay if you do not have too many pages.
An alternative could be in getting the currently visited page via
$_SERVER['PHP_SELF']
and then add to navigation.php which html-file shall be called for which entry in php_self. The advantage would be that you have all your navigation-based code in one place. Personally, I would prefer this last solution.

Related

Links from require_once broken

I have a menu file that i want to include in every file of my site to avoid writing every time the same code. I do this with require_once("menu.php"); The problem is that when i click on a link generated by menu.php it results to be a relative link instead, because it doesn't find the file.
This is my directory:
menu.php:
<?php echo'
<div class="topnav-bar">
<ul class="topnav">
<li class="dropdown">Home
<ul class="dropdown-content">
<li>Geografia</li>
<li>Clima</li>
<li>Storia</li>
</ul>
</li>
<li class="dropdown">Luoghi
<ul class="dropdown-content">
<li>7 Chiesette</li>
<li><a href="pages/luoghi/catajo.php" class="active">
Castello del Catajo</a></li>
<li>Abbazia di Praglia</li>
<li><a href="pages/luoghi/carrareseeste.php">
Castello carrarese di Este</a></li>
<li>Castello di Lispida</li>
<li>Castello San Pelagio</li>
</ul>
</li>
<li class="icon">
☰
</li>
</ul>
</div>'; ?>
if from "index.php" i click on "chiesette.php" for example, the browser shows me the url http://127.0.0.1/pages/luoghi/chiesette.php. Now if from this page i click on "catajo.php" it shows me http://127.0.0.1/pages/luoghi/pages/luoghi/catajo.php.
What am i doing wrong?
When you are in index.php you are at the root so the link will refers to the right location.
From /pages/luoghi because of the lack of slash, it builds the link from you actual directory, eg /pages/luoghui
<li>7 Chiesette</li>
<li><a href="/pages/luoghi/catajo.php" class="active">
Castello del Catajo</a></li>
<li>Abbazia di Praglia</li>
<li><a href="/pages/luoghi/carrareseeste.php">
Castello carrarese di Este</a></li>
<li>Castello di Lispida</li>
<li>Castello San Pelagio</li>
THe code above (whith slashes) should work because the link will be build from root.

Catch the URL and redirect somewhere else

i ve done this below menu in php which is autogenerated with a function :
As you know, if i click on "Red Chicken", it will try to open a ../Red%20Chicken URL, which doesnt exist till it comes from a big table which change everytime.
What i want to do is : to know where we clicked (example : by generating the url and cut it ) and then redirect to a page like result.php (+ get something like the variable to know where do we come from). And of course, i don't want to create a .php page for each element of my table.
Is this thing possible ?
EDIT : i found a way, it's to change my function and how it generates the menu.
Not really what i wanted but it's okay.
<ul class=\"niveau1\">
<li class=\"sousmenu\"><a href=\"Food\">Food</li>
<ul class=\"niveau2\">
<li class=\"sousmenu\">Meat</li>
<ul class=\"niveau3\">
<li class=\"sousmenu\">Poultry</li>
<ul class=\"niveau4\">
<li class=\"sousmenu\">Red Chicken</li>
</ul>
</ul>
<ul class=\"niveau3\">
<li class=\"sousmenu\">Beef</li>
<ul class=\"niveau4\">
<li class=\"sousmenu\">Hamburgers</li>
</ul>
<ul class=\"niveau4\">
<li class=\"sousmenu\">Steak</li>
</ul>
</ul>
</ul>
<ul class=\"niveau2\">
<li class=\"sousmenu\">Dairy</li>
<ul class=\"niveau3\">
<li class=\"sousmenu\">Cow</li>
</ul>
<ul class=\"niveau3\">
<li class=\"sousmenu\">Sheep</li>
</ul>
</ul>
</ul>
<ul class=\"niveau1\">
<li class=\"sousmenu\">name</li>
</ul>
http://jsfiddle.net/w10j0a38/1/
The PHP-Way would probably be to use URLs like <a href='menuhandler.php?sel=cow'>Cow</a>, and if you want to be lazy, you could also JS/jquery to manupulate the URLs and replace <a href="something">with a call in the style shown before - but then this would not work w/o JS, so it is not a real option IMHO.
changed my function to generate it in another way
<a href=\"Recette.php?var=food\">
Not really what i wanted but it's okay.

If query for tags in SPIP

It is possible to create an IF query for the tag in SPIP?
When i have only one article linked to the tag than he take me directly to the article. But if i have more than 1 article, than he does taken me to the tag page of spip, where are the listing of the linked article.
Here the code to go directly to the article
<div id="tagsphere-#ENV{id_article}">
<ul>
<BOUCLE_mot(MOTS){id_groupe ?}>
<B_article>
<li>
<a<BOUCLE_article(ARTICLES){id_mot = #ID_MOT}{0, 1}> href="#URL_ARTICLE"</BOUCLE_article>>
#TITRE
</a>
</li>
</B_article>
</BOUCLE_mot>
</ul>
</div>
and here the code to go to the tag menu
<div id="tagsphere-#ENV{id_article}">
<ul>
<BOUCLE_mot(MOTS){id_groupe ?}>
<li>[(#TITRE)]</li>
</BOUCLE_mot>
</ul>
</div>
Thanks in advance
Try this, will work with any SPIP version.
<BOUCLE_mot(MOTS){id_groupe ?}>
[(#REM) Link to mot]
<BOUCLE_check(ARTICLES) {id_mot} {1,1}>
<li>#_mot:TITRE</li>
[(#ID_ARTICLE|oui)]
</BOUCLE_check>
[(#REM) Link to one article]
<BOUCLE_one(ARTICLES) {id_mot} {0,1}>
<li>#_mot:TITRE</li>
</BOUCLE_one>
<//BOUCLE_check>
</BOUCLE_mot>
Conditional display is a real lack in SPIP. This is what I would do (don't have any SPIP environment with me to check).
<div id="tagsphere-#ENV{id_article}">
<ul>
<BOUCLE_mot(MOTS){id_groupe ?}>
<BOUCLE_articles(ARTICLES){id_mot = #ID_MOT}>
<B_article_unique>
<li>
<a<BOUCLE_article_unique(ARTICLES){id_article}{si #TOTAL_BOUCLE|==1}> href="#URL_ARTICLE"</BOUCLE_article_multiple>>
#TITRE
</a>
</li>
</B_article_unique>
<BOUCLE_article_multiple(ARTICLES){id_article}{si #TOTAL_BOUCLE|!=1} />
<li>
<a href="#URL_MOT">
#TITRE
</a>
</li>
</B_article_muliple>
</BOUCLE_articles>
</BOUCLE_mot>
</ul>
</div>

How to have the class="selected" depending on what the current page/url is

This is my first post so forgive as I am just new in the world of web development.
Normally, when I try to make a website, I create a file called header.html and footer.html so that I only change data once in all of the pages rather than having multiple same headers on many html files. And include them all in a php file together with the content and the php codes that comes per page.
Now my problem is because I only have 1 header, the css is designed in a way that whatever the current menu/tab is, it will be marked as "selected" so that its obvious to the user what page they are currently in.
My question is how do I solve this problem:
1.) To have the class="selected" depending on what the current page/url is.
<!--Menu Starts-->
<div class="menu">
<div id="smoothmenu" class="ddsmoothmenu">
<ul>
<li>Home</li>
<li>About </li>
<li>Services </li>
<li>Features</li>
<li>Support
<ul>
<li>Support 1</li>
<li>Support 2</li>
</ul>
</li>
</ul>
</div>
</div>
<!-- Menu Ends--!>
Thank You :)
If you're looking for a non-javascript / php approach...
First you need to determine which nav-link should be set as active and then add the selected class. The code would look something like this
HTML within php file
Call a php function inline within the hyperlink <a> markup passing in the links destination request uri
<ul>
<li><a href="index.php" <?=echoSelectedClassIfRequestMatches("index")?>>Home</a></li>
<li><a href="about.php" <?=echoSelectedClassIfRequestMatches("about")?>>About</a> </li>
<li><a href="services.php" <?=echoSelectedClassIfRequestMatches("services")?>>Services</a> </li>
<li><a href="features.php" <?=echoSelectedClassIfRequestMatches("features")?>>Features</a></li>
<li>Support
<ul>
<li><a href="support1.php" <?=echoSelectedClassIfRequestMatches("support1")?>>Support 1</a></li>
<li><a href="support2.php" <?=echoSelectedClassIfRequestMatches("support2")?>>Support 2</a></li>
</ul>
</li>
</ul>
PHP function
The php function simply needs to compare the passed in request uri and if it matches the current page being rendered output the selected class
<?php
function echoSelectedClassIfRequestMatches($requestUri)
{
$current_file_name = basename($_SERVER['REQUEST_URI'], ".php");
if ($current_file_name == $requestUri)
echo 'class="selected"';
}
?>
You could ID each link and use JavaScript/Jquery to add the selected class to the appropriate link.
<!--Menu Starts-->
<div class="menu">
<div id="smoothmenu" class="ddsmoothmenu">
<ul>
<li id="home-page">Home</li>
<li id="about-page">About </li>
<li id="services-page">Services </li>
<li id="features-page">Features</li>
<li id="support-page">Support
<ul>
<li id="support1-page">Support 1</li>
<li id="support2-page">Support 2</li>
</ul>
</li>
</ul>
</div>
</div>
<!-- Menu Ends--!>
On your content page use jQuery to do something like:
$(document).ready(function(){
$("#features-page").addClass("selected");
});
Another method you could use is:
Add class element based on the name of the page
Give each link a separate id then use jQuery on the individual pages.
<li>Home</li>
<li>About </li>
<li>Services </li>
<li>Features</li>
<li>Support
<ul>
<li>Support 1</li>
<li>Support 2</li>
</ul>
</li>
On the services page:
$(document).ready(function(){
$("#services").addClass("selected");
});
Or even better as robertc pointed out in the comments, there is no need to even bother with the id's just make the jquery this:
$(document).ready(function(){
$("[href='services.php']").addClass("selected");
});
One variant on Chris's approach is to output a particular class to identify the page, for example on the body element, and then use fixed classes on the menu items, and a CSS rule that targets them matching. For example, this page:
<DOCTYPE HTML>
<head>
<title>I'm the about page</title>
<style type="text/css">
.about .about,
.index .index,
.services .services,
.features .features {
font-weight: bold;
}
</style>
</head>
<body class="<?php echo basename(__FILE__, ".php"); ?>">
This is a menu:
<ul>
<li>Home</li>
<li>About </li>
<li>Services </li>
<li>Features</li>
</ul>
</body>
...is pretty light on dynamic code, but should achieve the objective; if you save it as "about.php", then the About link will be bold, but if you save it as "services.php", then the Services link will be bold, etc.
If your code structure suits it, you might be able to simply hardcode the page's body class in the page's template file, rather than using any dynamic code for it. This approach effectively gives you a way of moving the "logic" for the menu system out of the menu code, which will always remain the same for every page, and up to a higher level.
As an added bonus, you can now use pure CSS to target other things based on the page you're on. For example, you could turn all the h1 elements on the index.php page red just using more CSS:
.index h1 { color: red; }
You can do it from simple if and PHP page / basename() function..
<!--Menu Starts-->
<div class="menu">
<div id="smoothmenu" class="ddsmoothmenu">
<ul>
<li><a href="index.php" <?php if (basename($_SERVER['PHP_SELF']) == "index.php") { ?> class="selected" <?php } ?>>Home</a></li>
<li><a href="about.php" <?php if (basename($_SERVER['PHP_SELF']) == "about.php") { ?> class="selected" <?php } ?>>About</a> </li>
<li><a href="services.php" <?php if (basename($_SERVER['PHP_SELF']) == "services.php") { ?> class="selected" <?php } ?>>Services</a> </li>
<li><a href="features.php" <?php if (basename($_SERVER['PHP_SELF']) == "features.php") { ?> class="selected" <?php } ?>>Features</a></li>
</ul>
</div>
</div>
Sorry for my bad English, however may be it could help. You can use jQuery for this task. For this you need to match the page url to the anchor of menu and then add class selected to it. for example the jQuery code would be
jQuery('[href='+currentURL+']').addClass('selected');

Populate menu list with multiple text files using php

I'm trying to build a kiosk on a local machine, so no online requirement. I am going to build a big menu which will show exhibition stores and products. Due to the stores and products will always change, the client has requested to use a simple text file - .txt to populate the menu.
I used jQuery last week but unfortunately it didn't work. So after searching I've decided to use php instead. I'm new to php, but I have given it a try today, I have only managed to populate one list with the external file kate.txt
Here is my code for the page
<body>
<ul class="sf-menu">
<li class="current">
Area1
<ul>
<li class="current">
Kate
<ul>
<?php
//variables
$dir = "C:\wamp\www\Menu";
$txtfile="Textfiles\kate.txt";
foreach (glob("$txtfile") as $filename) {
$file = $filename;
$contents = file($file);
$string = implode("<li>", $contents);
}
?>
<li><?php echo $string ?></li>
</ul>
</li>
<li>
Cathy
<ul>
<li>Banana</li>
<li>Apple</li>
</ul>
</li>
</ul>
</li>
<li>
Area2
<ul>
<li>
lily
<ul>
<li>Watermelon</li>
<li>Grapefruit</li>
</ul>
</li>
<li>
John
<ul>
<li>Orange</li>
<li>Pineapple</li>
</ul>
</li>
</ul>
</li> <!--current-->
</ul> <!--sf-menu-->
</body>
Can someone please give me some direction on the php code? I don't mind how the text files are structured in a folder, as long as I can populate all the menu list items and links with a text file or multiple text files not in the html code.
Assuming that your text file holds all your list items
kate.txt:
<li>stuff</li>
<li>stuff</li>
...
the php file
<?php
$txtfile="Textfiles\kate.txt";
$content = file_get_contents($txtfile);
<li class="current">
Kate
<ul>
<?php echo $content ?>
</ul>
</li>

Categories