PHP: Navigating through subdirectories - php

Consider the following directory tree:
/root
page1.php
menu.php
/subdir
page2.php
Here, menu.php is a navigation menu:
<div class="cssmenu">
<li>page 1</li>
<li>page 2</li>
</div>
Both page1.php and page2.php invoke menu.php through the include function:
<?php include "path to menu.php"?>
However, when code is summoned through the include function, relative links in the navigation menu become ill-defined. How should I define links in the navigation menu as to be properly rendered in a browser? Preferably, I'd like this to be independent of the absolute path of /root, so that I may render my site through my localmachine.

You may start link with / (slash). It means address in current site from root of the site. See example for clarity
Link
Example: Badges (Badges)

Related

i am trying to get to the root url in php

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.

Links not resolved correctly when using relative paths

I've created a Header.php file in the root directory.
Below is the tree structure.
-SiteName
-Header.php
-Books
-Samples
-Math
-MathBook.php
-index.php
-About.php
So MathBook.php includes Header.php like below
include '../../../Header.php';
Header.php is included in About.php like this
include 'Header.php';
Now Header.php contains the code for the navigation menu.
<ul>
<li>Home</li>
<li>About Us</li>
</ul>
So now when I'm in Math.php and I press home it takes me to:
/Books/Samples/Math/index.php
Obviously the URL doesn't exist.
How i can make sure when the user is in Math.php it should go to index.php without including current path.
Header.php is included in all files.
You could define includes and links relative to your site's base url.
The hrefs can be written like so:
<ul>
<li>
<a
href="<?php echo $_SERVER['HTTP_HOST'].'/index.php';?>"
class="<?php active('index.php');?>">Home</a>
</li>
<li>
<a
href="<?php echo $_SERVER['HTTP_HOST'].'/About.php';?>"
class="<?php active('AboutUs.php');?>">About Us</a>
</li>
</ul>
You are using a relative path.
by typing "index.php" in the href attribute, you are pointing to a file named "index.php" in the current folder.
If you want to point to a file in the root folder, you need to use a slash /
href="/<sub folder if any>/index.php"
href="/<sub folder if any>/About.php"
This will take the user to the file you would like, also if you are unsure about what to use, just use absolute paths like this:
href="https://www.example.com/<sub folder if any>/index.php"
href="https://www.example.com/<sub folder if any>/About.php"
EDIT
Adding a slash works because by adding / you tell the browser to go to the root folder/website and start the path from there.
Let's say you are in yourwebsite.com/current/path/file.html.
A link to /some/path/to/a-file.php resolves to yourwebsite.com/some/path/to/a-file.php
But a link to some/path/to/a-file.php resolves to yourwebsite.com/current/path/some/path/to/a-file.php

The Wordpress way URL on static / dynamic pages

I would like to add a menu or footer like in Wordpress or any other CMS.
That really dont work with PHP includes because if a website file is in subdirectory there is a problem with the path.
As you know a normal static menu looks like
<nav>
<ul>
<li>About</li>
</ul>
</nav>
but CMS menu looks like
<nav>
<ul>
<li>About</li>
</ul>
</nav>
And that on all pages so no problems with the path
if you try it with static/dynamic pages you have to add the path or the url and that looks really ugly or not
<?php
define('WEB_ROOT', '../'); // relative path to /
?>
...
<nav>
<ul>
<li>Solution</li>
or
<li>Solution</li>
or
<li>Solution</li>
</ul>
</nav>
I think thats a bad idea and really ugly.
so how to solve that kind of problem maybe with adding a web root in php like above but looks not really good so do you have any other ideas?
And that Solution should also work for CSS and JS not only for static contents, so all styles are same and menus looks just like the other pages even if it is in the subdirectories
Generally in Wordpress I use the site url to generate full urls:
site_url('/about');
As for JS/CSS files, it depends where you are putting them. If the files are located in a specific theme use:
get_template_directory();
you can use it...in "a" tag href="", use this:
echo home_url('index.php/about');
or remove the index.php, just about [the name of the page.]
Done.

Creating Navs for multiple directories

Is it possible to create a nav that link to multiple directories and files in php,I don't want to update the link on each page when I change any one link
Those are my pages:
1) index.php
2) example.php
3) example2.php
4) example/index.php
5) example2/index.php
I want to have single nav for all above files. (Is it possible?)
//-------- Edit ----------
I don't want to start the links with "/". It looks a bit unprofessional.
if You don't want to use absolute path (with "/"), than You need to use "../" to find path. Try:
<?php //------------ nav.php ------------
$slashcount = substr_count($_SERVER['REQUEST_URI'], '/'); // count '/' in
$dir = str_repeat('../', $slashcount -1); // maybe needs to subtract with 2
?>
<ul class="nav">
<li>Home</li>
<li>example</li>
<li>example2</li>
<li>example3</li>
<li>example4</li>
</ul>
to call in index.php.
include "nav.php";
and inside of example/index.php
include "../nav.php";
this is so easy you just need to create a file which include to your each page like that.
create file navigation.php
<ul class="nav">
<li>Home
<li>example
<li>example2
<li>example3
<li>example4
</ul>
new include your navigation file into every single page like that.
in index.php
include "navigation.php";

Navigation menu A Href doesn't work when back to directory try?

I tried to make the navigation menu. I took his menu file using code "include" in my index.php file.
When I select the guitar work. but when re-pressing the home button. can not go back to the index or home? guitar.php files are in direcorty pages / guitar.php.
Why this can not go back to the index directly? please help me. Thank you
<ul>
<li><a href="index.php" >Home</a></li>
<li>Guitars
<ul>
<li>Guitar Acoustics</li>
<li>Guitar Electirs</li>
<li>Guitar Amplifiers</li>
<li>Guitar Effects</li>
</ul>
</ul>
above is the code in the file navigation menu.
<?php
require_once "layout/header.php";
include ("layout/nav.php");
require_once "layout/aside.php";?>
?>
above is the code in file index.php, and pages/guitar.php.
Thanks ...
Your links are relative, which means they'll just replace the "xxx.php" bit at the end of your URL, so when you go to "pages/guitar.php" your home link becomes "pages/index.php".
Make them start with a / and they'll be relative to your domain, eg. if your site is mydomain.com, your homepage becomes mydomain.com/index.php and the guitars page is mydomain.com/pages/guitar.php.
Home
Guitars
If gets a shade more complicated if your site isn't at the root of your domain. For example, if your homepage is actually at mydomain.com/guitar-site/index.php then you'll need to add the /guitar-site to the front of the URLs as well, so the link goes to the right place:
Home
Guitars
Try replacing include ("layout/nav.php"); with header("location:layout/nav.php");

Categories