I'm using PHP and XAMPP for a website. My current dilemma is that I'm not finding any answers/help on how to properly route links in my nav bar depending on which directory I am in. Here is a general file setup:
-index.php
|
-views folder-> nav.php, testing.php, (all php pages here)
|
-images folder-> all imgs
The issue is the links in my < a href="" > tags will be different for the index vs all other php pages (since they are all in my views folder).
I am including my nav on all php pages as such:
<?php
require "nav.php";
$thisPage = "pagesName";
?>
I am attempting to utilize the $thisPage="" (for all pages) to help with routing in the nav page:
<?php
if ($thisPage=="indexPage") {
echo '
<li>Contact Us</li>
<li>Testing Centers</li>
<li>Home</li>
<li><img src="images/global.png"></li>';
} else { //every other page already in the views folder
echo '
<li>Contact Us</li>
<li>Testing Centers</li>
<li>Home</li>
<li><img src="../images/global.png"></li> ';
}
?>
The nav loads correctly for index, however, none of the links/imgs work, all links give "Object not found! Error 404". Using the console, I know the if($thisPage=="index"){} is entered, but still no links are working.
Thanks in advance!
Related
I have written some code that will highlight a link in the header based on: class="active". The class is connected to some CSS code to style it.
I am currently adding the class="active" the specified link for each page. However, since I want to move my header into it's own file and include it on each page, I will lose the ability to set the class for each page. I could of course add a variable that will allow me to sort of do the same thing.
What I wish to know is if there is a better and secure way to automatically set the class="active" to the page that I am on. I have seen some posts that suggest using: ($_SERVER['PHP_SELF'] but I have read that it could cause some security issues.
Lastly the main problem that I have is that I need to set two links to
class="active" if I am viewing a page that is in the portfolio list item. For instance, if games.html is currently viewed, both the games and the portfolio page should have the code: class="active".
(All of my .html files are read as .php)
Here is the html code I have so far:
<ul>
<li>Home</li>
<li><a class="active" href="portfolio.html">Portfolio</a>
<ul>
<li><a class="active" href="games.html">Games</a></li>
<li>2D Art</li>
<li>3D Models</li>
<li>Particles</li>
<li>Shaders</li>
<li>Environments</li>
<li>Programming</li>
<li>Substance Designer</li>
<li>Music</li>
</ul>
</li>
<li>About</li>
<li>Contact</li>
<li>Store</li>
</ul>
Any help will be appreciated!
PHP_SELF is safe when you only use it to compare values and don't directly use it in your html:
The portfolio menu can be made active by nested all related pages in a folder called portfolio and checking if the path starts with that folder.
<?php
$currentPage = $_SERVER['PHP_SELF'];
$portfolioFolder = 'portfolio/';
$isPortfolioFolder = substr($currentPage, 0, strlen($portfolioFolder)) == $portfolioFolder);
?>
<ul>
<li>Home</li>
<li><a class="<?= $isPortfolioPage ? 'active' : '' ?>" href="portfolio.html">Portfolio</a>
<ul>
<li>Games</li>
<li>2D Art</li>
<li>3D Models</li>
<li>Particles</li>
<li>Shaders</li>
<li>Environments</li>
<li>Programming</li>
<li>Substance Designer</li>
<li>Music</li>
</ul>
</li>
<li>About</li>
<li>Contact</li>
<li>Store</li>
</ul>
I have this link in my db.php. I want everytime the dropdown menu is clicked it redirect to the corresponding page.
$ROOT_URL = '192.167.1.67/office/';
I want to go to this root url on the navigation every time i click on the link.
Home
<ul class="dropdown-menu">
<li>Add Product</li>
<li>View Product</li>
<li>Removed Product</li>
</ul>
instead the page is redirecting as
192.168.1.67/admin/192.168.1.67/admin/product/addproduct.php
How can i solve this problem??
use protocol before your url like..
$ROOT_URL = 'http://192.167.1.67/office/';
OR if having https $ROOT_URL = 'https://192.167.1.67/office/';
If the link points to a local location, you can just use / as root.
This, for examples will work:
Home
If $ROOT_URL is dynamic and you want to include the variable, define it as:
$ROOT_URL = '/office/';
Then you can do:
Home
Also, the links in the list
<ul class="dropdown-menu">
<li>Add Product</li>
<li>View Product</li>
<li>Removed Product</li>
</ul>
point to a relative URL, so when your current URL is /office, the links will point to: /office/addproduct.php. But if this navigation is also included on a page where the URL is /some/other/url, this link will point to: /some/other/url/addproduct.php.
Unless you are really sure the HTML with the link will only show at a certain path, try to avoid relative URLs.
I am creating a HTML/PHP based Website. Now I would like to add a menu but that menu should have on all pages the same structure like on a CMSso I mean:
On a normal/static dynamic website with subdirectories you have following structure
<nav>
<ul>
<li>Solution</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
but if it a php file is on a subdirctoryas solution
<nav>
<ul>
<li>Solution</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
On a normal CMS you see on all page also on pages witch are in 'subdirectories'
<nav>
<ul>
<li>Solution</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
I think with you can easy manage and change that kind of menu and you donnt have to change a mistake on 50 pages:)
I think I could do it with PHP but dont have any idea.
Maybe with Base URL but the the url would look like
<li>Contact</li>
and thats not that what I mean and want to get.
Hope you could give me an advice how to realize that
It really depends on the CMS of your choice. For Wordpress for example to establish base reference you use something like Your Page. If you later decide that sample page need to be called page-new for example I would go with find and replace function of any good editor out there. It will take certainly time for 50 or more links but it will be significantly less.
I have a navbar with sublists. I use this navbar for a CSS navbar at the top of my web page, with links for the main pages and drop-down lists for the pages in the sub-lists. I would like to use the same structure to create a navbar at the side of my page for sublists.
All of my HTML pages have the following line near the top of the <BODY> that creates the navbar:
<?php include('navbar.php'); ?>
CSS styling of this navbar is taken care of elsewhere.
Suppose my navbar.php file has the following:
<nav id="mainnavbar>
<ul id="index">
<li>Home</li>
<li>Personal
<ul id="personal">
<li>About Me</li>
<li>Contact Info</li>
</ul>
</li>
<li>Professional
<ul id="professional">
<li>Activities</li>
<li>Resume</li>
</ul>
</li>
</ul>
</nav>
The horizontal navbar at the top of my page would list "Home | Personal | Professional", and there would be dropdown menus on "Personal" and "Professional". This navbar is easily included on ALL pages for a uniform experience. Not too hard.
If a user actually navigates to one of the main (or "parent") pages (e.g. "Home", "Personal", or "Professional"), however, I want there to be a second, vertical navbar on the side of that page with links to that page's "children."
For example, if the user goes to the "Personal" page, there should be a side-navbar with links to "About Me" and "Contact Info". If instead the user navigates to "Professional", that sidebar should instead display links to "Activities" and "Resume". If the user decides to go back to the "Home" page, the sidebar should display "Personal" and "Professional". (Although it would be 100% fine if it also displayed "Home" -- perhaps it would be easier that way?)
While I could hard-code a new <nav> for each page, I already have a unordered list structure and would like to make use of it. Any ideas how this could be done, or if there's a better way to do it?
Perhaps I would want to dynamically generate (via the magic of PHP) a second instance of <nav> that is a sort of partial copy of <nav id="mainnavbar">, listed above, but including one of the nested unordered lists, rather than everything. Then I could position and stylize each navbar separately.
(Note that only the Home, Personal and Professional pages should have a sidebar navigation menu because only they have "child" pages. The "child" pages should not have a sub-menu. )
In essence, I should have the following:
For index.html, I should have the following dynamically generated based on navbar.php:
<nav id="subnavbar">
<ul id="index">
<li>Home</li>
<li>Personal </li
<li>Professional</li>
</ul>
</nav>
For personal.html, I should have the following dynamically generated based on navbar.php:
<nav id="subnavbar">
<ul id="professional">
<li>Activities</li>
<li>Resume</li>
</ul>
</nav>
For professional.html, I should have the following dynamically generated based on navbar.php:
<nav id="subnavbar">
<ul id="professional">
<li>Activities</li>
<li>Resume</li>
</ul>
</nav>
For all other pages, there should be no sidebar navigation. (Or perhaps I should have some default navbar as a placeholder??)
I'm not sure if having the same id for an unordered list in two different <nav> sections would be problematic, however.
Thanks again for your help!
You can use PHP to determine what page you are on, and print out different HTML for the different pages. I put together the simplest version of what you are asking for. Use the same principle to expand on this. You can use the $_SERVER superglobal to find out the current URL.
<?php
// See if current URL ends with "personal.html"
$personal = preg_match('/personal\.html$/',$_SERVER['REQUEST_URI']);
?>
<nav>
<ul>
<li>Home</li>
<li>Personal
<?php if (!$personal) print '<ul>'; ?>
<li>About Me</li>
<li>Contact Info</li>
<?php if (!$personal) print '</ul>'; ?>
</li>
<li>Professional
<ul>
<li>Activities</li>
<li>Resume</li>
</ul>
</li>
</ul>
</nav>
All I am doing here is removing the <ul> tags for the personal page, which will put the two nested <li> inline with the rest of the navbar elements. You need to expand on this to fit the design you need.
I have a single index.php page that should link to the rest of my websites.
<?php
if(!isset($_GET['page']))$page = 'home.php';
else{
$page = $_GET['page'] . ".php";
}
include("_includes/header.php");
include("_includes/navigation.php");
include("_pages/$page");
include("_includes/footer.php");
?>
This is what my index.php looks like. It's supposed to get the page i click on (when i clikc on the navigation) and direct it to this page so that the header, navigation, and footer will be reused for every page.
<div id="navi">
<ul>
<li>Home</li>
<li>Skills</li>
<li>Projects</li>
<li>Experience</i></li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
according to the code the "home" link should work but it says URL not found because it is looking for "home" in the directory that I'm in. however its in my pages directory. "skills" does the same thing. the only ones that do work is the links projects through contact because i am specifying a path. however, the links that work don't go through my index page because the header.php (which contains the css), navigation.php, and footer.php is not included here.
i did research and i found that i had to do something with the .htaccess file and/or something with mod_rewrite. can someone please help me out. I've been looking for about 2-3weeks now and nothing.
You should be referencing index.php, not home..
<div id="navi">
<ul>
<li>Home</li>
<li>Skills</li>
<li>Projects</li>
<li>Experience</i></li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
Your links should be:
<div id="navi">
<ul>
<li>Home</li>
<li>Skills</li>
<li>Projects</li>
<li>Experience</i></li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
It's strectly related on how your web server handles requests.
If you try to visit url that's not handeled by PHP you won't get it to work. By deafult PHP handles only .php request url (also .php5 and some others).
A solution could be to write a rewrite condition that convert your /home/ to index.php?page=home. This assuming you're running Apache Web Server with mod_rewrite enabled and htaccess enabled.
So, supposing to normalize your links this way:
<div id="navi">
<ul>
<li>Home</li>
<li>Skills</li>
<li>Projects</li>
<li>Experience</i></li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
you can write an .htaccess like this, and put it in /pages/ folder:
RewriteEngine On
RewriteRule ^/pages/(.*)$ /index.php?page=$1 [QSA,L]
Beware that every request to /pages/ will be rewrited, so you should write them carefully avoiding rewrite of .css .png and so on...
This will effect your request this way:
Apache will recive request for a file in /pages/ named home
RewriteRule will detect it with the regexp
The rule forces apache to answer with your index.php with the parameter page=home (or whatever the regex has catched)
P.S.
Maybe you could be interested in flag P to send request to index.php as a Proxy, but for further infos you should refer to mod_rewrite docs (http://httpd.apache.org/docs/current/mod/mod_rewrite.html)