At present I am using an include statement to display all my menus. However I understand that some of my links are broken when accessing pages that are not located in the same directory.
Is there a way, by using the same link regardless of file location it will always access the same pages.
For example
My main folder currently contains:
Files =
index.php
shop.php
mensTShirts.php
I also have the sub folders:
html
php
images
includes
css
items
The main problem I have is that from by using an include statement from to get the navBar from the html folder the links included do not work when a viewing a file from say the items folder.
So my question is to find out how to make the navigation links work for specific pages regardless of file location.
I hope you can understand my problem, I am also kind of new to this.
Any help is greatly appreciated.
Many Thanks
You should make config file to define your path. Then use the variable to include relative paths.
Related
The problem is as follows:
Originally, all of the individual pages on my website were in the root folder of the site.
I have now moved the majority of the pages into various sub-folders.
For example: The main page for our Apache Totem Freestanding Light Box used to be in the root folder, but is now in subfolders: /lightboxes/apache-totem-freestanding-lightbox/index.php
The problem is that various elements of the page have “PHP Modules” included for common elements such as the header, the breadcrumbs, the footer, the css file etc.
For example, for the header we have as a link on each page.
The files that these “PHP includes” point to are situated in their own folder on the site root, called ‘modules’.
The trouble with this, is that the ‘PHP Includes’ can’t seem to find the modules because I guess they are expecting to see them in a folder called ‘modules’ that is situated in the same subfolder as the current page.
I’ve partially got around this by changing the link to the module to have ../../ in front to tell it that it has to look two folders up from the current folder, but the page is still having difficulties displaying all of the information.
To give you an idea of this, I have uploaded the Apache Totem Index Page in its subfolder /lightboxes/apache-totem-freestanding-lightbox/
https://www.w-co.co.uk/lightboxes/apache-totem-freestanding-lightbox/
As you can see, the main content of the page is displayed ok and the css file is doing its job with styling etc.
But there are various other problems, for example:
1. There is an image missing from the header
2. The quote button in the header and the content is pointing to the wrong place (quote.php is on the root folder but the button is trying to link to the current subfolder)
3. All of the items on the breadcrumb trail link to the current page
4. All of the links in the footer are also trying to point to the subfolder lightboxes
Is there anything you can think of to fix these issues?
Thanks,
Tony
The URL for the images should be something like this https://www.w-co.co.uk/images/worldpay/visa.gif
You have to use base URL https://www.w-co.co.uk and then image path. You can define("BASE_URL", "https://www.w-co.co.uk") and use it like in the following.
<?php
define("BASE_URL", "https://www.w-co.co.uk/")
?>
<img src="<?php echo BASE_URL;?>images/worldpay/visa.gif"
I want to make multiple pages on my website, but to keep everything clean I want to make different directoriess with the different pages. However, I use php to make a different file with my header that is included in all my pages, so I only have to change the code of my header once and it will be the same on all pages.
The problem is that the links I use in my menu items (like home, contact, about, etc.) will not work anymore when you're on a page inside a directory (I'll make an example below).
So my question:
Is there a home folder on a website (like ~/ on unix) or is there another way to make it work?
Example of my directory structure:
htdocs
index.php
header.php
menus
contact.php
about.php
(a link to index.php won't work anymore if you're on the contact.php page)
Sounds like you're using relative paths in your menu links. Use an absolute path instead by starting with a "/":
Home
About
or a complete URL:
Home
About
The home directory of a website can be accessed with a simple '/' at the start of the link you want to add. From there you can enter subfolders by appending the folder name.
Example:
'example.com/subfolder/subsubfolder/page.html'
I have two sites, one stored in the root of server, another in a sub-folder as shown below.
SITE ONE
ROOT/index.php
ROOT/assets/includes/header.php - contains styles and scripts
SITE TWO
ROOT/SUB-FOLDER/index.php
ROOT/SUB-FOLDER/assets/includes/header.php - contains styles and scripts
I am using PHP includes in order to include external files where the first site works fine using the code below, however the second one don't:
include($_SERVER["DOCUMENT_ROOT"] . '/assets/includes/header.php');
Could you please help me to achieve similar result for the site stored in the SUB-FOLDER.
Thanks in advance.
Changing the include link to:
include('assets/includes/header.php');
and adding the
<base href="http://example.com/SUB-FOLDER">
tag to head of the included pages solved the my issue.
I'm making a simple website with 3 pages in php. The main page is index.php and the other two can be opened through index.php via links.
The link to CSS is given in the index.php file and I want the CSS to get applied to all the other files as well. Do I have to put link tags in all php files to refer to the stylesheet? Is there any other way possible?
If you had header/top.php then copy all those files within your top.php as like
<link rel="stylesheet" href="style.css"/>...
and you can use top.php within your each php file just by adding
<?php include 'pathtocss/top.php'?>
If you have three different pages and linking all the css file in index.php, It will not get reflected in all the pages.
So better you can follow any of the three approach,
Put all the css file in one php file, say styles.php and link it in all the pages
Separate css file for each page.
You can go for single page application. but you have to do some magics using JS
Now the choice is yours!!
You can do one page named includes.php put into all yours files include and include this page in all your pages
I am currently building a gallery using some very basic php - namely the inclusion of:
<?php include("page.php"); ?>
but I'm running into an issue calling images.
To explain the situation, I am building a php page for each item of artwork I want displayed and instead of building 30+ additional pages in the root folder I have created a sub-folder (in this case '/art/' ) where all of the artwork pages will live. Linking to them is easy enough but when I go to view the pages items I am calling in such as header.php or footer.php don't appear.
Example:
<?php include("header.php"); ?>
Now, simply adding '../' resolves this issue and the header, footer and additional php items I need are imported.
<?php include("../header.php"); ?>
Unfortunately the images are not - all I get in their place is the default missing/broken image icon. Of course if I go into the headr.php page and add the '../' to each image I am calling in then it works great in the php files in the /art/ folder but then I get the exact same broken icons when I go to any pages in the root directory such as index.php.
The conclusion I am at is that while adding ../ does point the page inside the /art/ folder to look in the previous directory it is still trying to locate images from an image folder within the /art/ folder and not the /images/ folder in the root folder. I could easily just make a copy of the contents of the images folder and place it within the the /art/ folder but that seems redundant.
Is there a better, preferred solution to tell the php include where to look for the images? Or is it best to just house all php pages in the same root folder?
Oh I had this one before :),
It's an issue because when you import the file php just pasts
the file contents in the new file so the relative pathing is
then made from there.
TL;DR
Use dirname(FILE).'/'.(..) as your path's instead.
I hope you understand what I mean.