Static HTML to PHP Navigation - php

I'm looking to dynamically change the navigation of a static html site. I'd like to detect which page I'm on and adjusts the navigation accordingly, since certain pages are in folders and causing the navigation to break when linking back to previous pages. If I need to rebuild my navigation I'm okay with that. Just not sure how to deal with navigating nested folders in php.
I have a site with a navigation structure like so
index.php
sample-page.php
Resources(folder) > resource-1.php, resource-2.php, resource-3.php
Contact(folder) > contact-1.php, contact-2.php
I can go from index.php to Resources/resources-1.php fine but if I click resources-2.php I go to Resources/Resources/resources-1.php.
The same thing happens if I'm on Resources/resources-1.php to index.php I go to Resources/index.php instead of the root.
Not sure how to change this. I've broken up the nav so it's
being inserted by <?php include ("navigation.php"); ?>
<?php set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] ); ?>
Thank you!
p.s. Sorry for the poor formatting, long time lurker and first time poster. Haven't quite figured out it out just yet.

Be sure to include a forward slash to avoid the unwanted relative paths. Assuming / is your base:
Index
Sample Page
Resources 1
Resources 2
Resources 3
Contact 1
Contact 2
Give that a go, I hope it helps!

Related

CSS Menu URL's not directing appropriately

I created a CSS menu, to use on a PHP site, but now I realised that for example:
From the Home tab (URL localhost/site), all the item links are correct if you hover over them, but now i navigate to "Stock" for example, who's URL is localhost/site/stock... it opens correctly. So from Stock i want to navigate to Sales (localhost/site/sales), the URL of sales become localhost/site/stock/sales and not just localhost/site/sales.
I added this menu only recently, the previous one was working fine, so for a test, i replaced the css menu with the previous one, but to no avail. the problem still persist, so I assume that something else must have changed the behaviour of the links...
I can probably fix this by added " ../ " in front of the menu's URL, but on the other hand. not all items are just one step back.
Your help would be greatly appreciated.
Try using absolute URLs.
When your link is on a page located at localhost/site/stock:
Link <!-- goes to localhost/site/stock/sales -->
But if you prepend a forward slash, the destination will be absolute, relative to your document root:
Link <!-- goes to localhost/site/sales -->
Assuming of course that your document root is localhost/site
More likely your links will have to look something like:
Link
You're correct in saying you need to add slashes. My suggestion would be to provide absolute links from / to your page:
/site
/site/sales
/site/sales/sale24
/site/stock
/site/stock/secondstockpage
/site/stock/thirdpage
I have changed the menu as you suggested, but now, it works as follow:
if i for example set my menu link to direct to: /site/sales/sold.php
so the menu.php file link would look like this:
<li>Sales</li>
<ul>
<li>Sold Items</li>
</ul>
it would sometime direct to /site/sales/sold.php and the next moment, it repeats the menu's directory like: /site/sales/sales/sold.php
so i removed the sales directory, as its directing there by itself, worked for a little while and now it directs to /site/sold.php which does not exist so it ends up with a 404.
should i specify a siteroute somewhere else?
this is very weird to me. first time i encounter something like this!
Hope my examples make sense!

How to link navigation/buttons to the whole site using one file?

Is there a quicker way to make the navigation bar/buttons go onto all my pages of my site instead of going into each .php file and copying and pasting it. How would I link up the class="button" to another file so that it changes the whole navigation on my site?
Just use an include statement.
include 'nav.php';
on each page.
Nav.php could then contain nothing more than just the navigation.
First Separate your php file as header.php for header menu and footer.php for footer menu.
And then include the file in pages .
See this example for reference.

PHP Include links

Hello I'm having my first serious go with PHP to create a sample script for my self. It has a basic structure, in my root folder I have:
index.php
core folder (holds most of my php function files)
includes (holders my header.php and footer.php)
sites - (sites has 3 further folders site A, B, C)
CSS
js
All pages are made up by taking a header.php and footer.php from the includes folder and then each page has its own content in the middle. The header.php contains (as well as basic html and links to javascripts stylesheets ect) includes from core folder like so:
include_once '/core/connect.php';
Now these all work great using the index.php which provides links to the 3 different sections of the site, sitea, siteb and sitec.
But when you navigate out of the document root to say /sites/sitea/index.php all those links are now broken.
What is the best way to go about building the links in the header.php section so they are relative site wide no matter which folder you are in?
The idea behind this is that you do only have ONE file for each process.
So process all pages through index.php
index.php would contain, for example,
require('header.php');
include('content.php');
require('footer.php');
That way, it won't break the site if your content doesn't show.
Your index is always loaded from the same path, so header/footer wouldn't change. Just content.
When you're including you want to use a real path, not a relative path...
require_once ($_SERVER['DOCUMENT_ROOT'].'/includes/header.php');
/* something happens here */
require_once ($_SERVER['DOCUMENT_ROOT'].'/includes/footer.php');
The best way is to always use physical path from wherever you are - this way every page that include other page with includes won't get break:
PHP 5.2 and down:
require(dirname(__FILE__) . '/core/connect.php');
PHP 5.3 and above
require(__DIR__ . '/core/connect.php');

Location of "assets" (php, css, jss, ect)

In my root directory I have a bunch of single pages and then the folder "blog" and "assets." For the pages I have a header.php/nav.php/footer.php to call for various css and js.
for example: within the header.php:
<link href="http://beta.rfahaiti.org/assets/css/bootstrap.css" rel="stylesheet">
Then, in the pages I call for: <?php include 'assets/header.php'; ?>
However, this does not seem to be working for any pages within the blog folder -- such as the index.php file in /blog/news/. I assume it's a relative vs absolute link issue but I'm not sure how to fix. Question: what does the php include call need to be for to call for the header.php file?
Thanks!
Try:
<?php include '../assets/header.php'; ?>
or
<?php include '../../assets/header.php'; ?>
depending on your folder structure.
Include paths are relative, try:
<?php include '../assets/header.php'; ?>
You will find the same with HTML document referring to resources e.g CSS.
It is a relative link issue, as you say. For pages two levels deep in /blog/news, you need to go two levels back:
../../assets/header.php
Edit thanks to Juan Sosa for pointing out that what follows is completely wrong.
Alternatively, you could write this:
/assets/header.php
The second approach is cleaner in one sense; however, beware it assumes that your site will always be located at the root of the domain (ie, if it ever got moved to http://beta.rfahaiti.org/theapplication/ or something, then all those type of links would break).

How to use images and php includes across multiple directories?

I am brand new to PHP. I want to use it to include a universal header and footer in an html/jquery site. Currently I am using includes to do this:
<?php include('../includes/footer.php'); ?>
This works fine. Where I encounter a problem is with any images in the header or footer.
An explanation of my file structure:
Root folder: contains index.php and the folders "includes", "img", "php" etc.
php folder: contains gallery.php
includes folder: contains header.php, footer.php
When viewing the index.php all images in the header and footer show properly, but, because they are linked relatively (ex "img/facebook.png"), the do not show in gallery.php. To work they would need a ../ included. But then this would defeat the purpose of a universal header.
Thus I am trying to figure out how to link the images in the includes files in way that is doesn't matter where the php file is located. I have read this thread (which sounds like my problem) but I do not understand any of the answers. I have also read things that suggest $_SERVER['DOCUMENT_ROOT'] .'/folder/';, in conjunction with an echo to display the image. I tried this in my footer.php with this code:
<?php
$path = $_SERVER['DOCUMENT_ROOT'] . '/img/';
$image = ($path . "facebook.png");
echo "<img src=\"$image\" />"; ?>
When I view the page though, I end up with a little torn paper icon instead of my image. I assume this means that the path is incorrect, but I do not know why. facebook.png resides in the img folder. This problem occurs on both index.php and gallery.php.
After this rather long winded explanation (sorry), my mains questions are:
1) How do I get images to show up properly in my includes across multiple directories?
2) If I am going about this in the right way with the echo, what are the possible reasons why it is not working?
Once again, I know nothing about php, so if you could try to make your answers as basic as possible, it would be much appreciated.
Thank you!
Instead of img/facebook.png, add a / before the img/ like this: /img/facebook.png
This says "go to the root, and look for the img folder there. All your images should work fine then. The path of the images are absolute or relative based on the HTML page you're viewing, not which files you use to create it.
Though there's probably not much of reason for a "php" folder - just keep all your pages in the root directory.

Categories