Using php include with multiple sub directories - php

Hello i recently just got my web server to run all .html files as .php files which now allows me to use the php include function, which is very useful.
However i have multiple sub directories with multiple files in each.
Homepage
--banners
--banner2.html
--banner2.html
--signs
--sign1.html
and so on. The problem here is im trying to use include for my top nav bar. when i include the bar in the sub files, for example banner2.html it wont correctly link.
nav.php
<ul class="nav navbar-nav">
<li>
About
</li>
<li>
Services
</li>
<li>
Contact
</li>
<li>
Login
</li>
<li>
Logout
</li>
<li>
<img src="images/Cart.gif" style="height: 20px; width:20px;"> Cart
</li>
</ul>
in the nav bar is a link to about.html normally i would do the
about
however i cant do that when every file is going to share the same navigation bar.
How can I fix this?

I have answered my own quest and feel silly for even asking the question. I just used absoulte paths
nav.php
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
About
</li>
<li>
Services
</li>
<li>
Contact
</li>
<li>
Login
</li>
<li>
Logout
</li>
<li>
<img src="images/Cart.gif" style="height: 20px; width:20px;"> Cart
</li>
</ul>

There are some ways to fix this, one of them is:
Use $_SERVER["DOCUMENT_ROOT"] – We can use this variable to make all our includes relative to the server root directory, instead of the current working directory(script’s directory). Then we would use something like this for all our includes:
`about`
Also you should check this post.

You could use the relative path to the root directory which is /file.ext
Example:
About

Related

Making a Navigation bar to use across all pages

I am having issues getting my navigation bar to display across multiple pages.
I have already tried several methods including PHP and javascript but I can't get anything to work.
Here is my navbar code:
<header>
<ul class="navbar">
<li style="left:5%; position:fixed;"><img width=35 height=35 src="images/4227LogoBig.png"></li>
<li style="left: 14%; position:fixed;">Home</li>
<li>
<!-- About us -->
<div style="left: 27%; position:fixed;" class="dropdown">
About Us
<div class="dropdown-content">
<ul style="list-style: none;">
<li><a class="navbarLink" href="#">The Team</a></li>
<li><a class="navbarLink" href="#">Mentors</a></li>
</ul>
</div>
</div>
</li>
<li>
<!-- Robots -->
<div style="left: 44%; position:fixed;" class="dropdown">
Robots
<div class="dropdown-content">
<ul style="list-style: none;">
<li><a class="navbarLink" href="#">2018-2019</a></li>
</ul>
</div>
</div>
</li>
<li>
<!-- Outreach -->
<div style="left: 58%; position:fixed;" class="dropdown">
Outreach
<div class="dropdown-content">
<ul style="list-style: none;">
<li><a class="navbarLink" href="#">2018-2019</a></li>
</ul>
</div>
</div>
</li>
<li style="left: 74%; position:fixed;">Contact</li>
</ul>
</header>
and I am currently trying to use <?php include('includes/navbar.php');?> in the body of my html.
I can't get any of it to display using this method however if I put it directly in the index.html it functions flawlessly. Any suggestions? Thanks in advance! Here's a link to the demo homepage if you're interested. http://chasekaplan.com/FTC%204227/index.html
PHP Code runs only within the .php files. You can't include a .php file within a .html file.
Ensure that your main page is .php, otherwise it won't work.
A common reason for PHP include, require, require_once not displaying files is incorrect pathing. Pathing rules can vary between webservers. First thing to try is adding a leading slash to reference the root. Some servers will work only with it, some only without it.
Since 'includes/navbar.php' isn't loading, try '/includes/navbar.php'.
Depending on how the server's configured and where your pages are relative to the root, you may need to explicitly reference the root path. For example, include(__DIR__ . 'includes/navbar.php'); or include($_SERVER['DOCUMENT_ROOT'] . 'includes/navbar.php'); Note: you may need to add the leading slash like so include($_SERVER['DOCUMENT_ROOT'] . '/includes/navbar.php');

CSS not called from header.php

I had some big problems calling my header at all from project1.php. It got solved with the header included like this:
<?php include $_SERVER['DOCUMENT_ROOT'] . '/vouzalis/resources/includes/header.php'; ?>
The header is now called, but all my CSS is not called. Howcome can that be? On all my pages there is lying in the root folder the CSS is working fine.
My folder structure is looking like this:
This code below is a snip of my header. As you can see Home should go to index.php. But when I am in my project1.php and I click on Home I get the URL: `
http://localhost:8888/vouzalis/projects/index.php
The correct root should be:
http://localhost:8888/vouzalis/index.php
It does not make sense to be. Does anybody have a clue why? Is it something with how the header is included?
<div class="navbar-collapse collapse">
<ul class="nav nav-pills nav-main pull-right">
<!-- begin navigation items -->
<li class="active">Home</li>
<li>
Services
</li>
<li>
Portfolio
</li>
<li>
About Me
</li>
<li>Contact</li>
<!-- end navigation items -->
</ul>
</div>
I believe the problem is here:
<li class="active">Home</li>
Replace href="index.php" with href="/vouzalis/index.php". The / specifies the domain root, without it, the root is the your current location (/vouzalis/projects/).

How do I get absolute URL's to work locally?

Everything works how I want on my live website, but because I'm using absolute URL's in my header.php and footer.php they don't work when I'm working locally (XAMPP) - I can't figure out how to get it working on both.
Header.php
<div id="top"><center><img src="http://cosworth-europe.com/images/header.png" style="max-width:100%;"></center></div>
<header id="header" class="site-header" role="banner">
<div id="header-inner" class="container sixteen columns over">
<hgroup class="one-third column alpha">
</hgroup>
<nav id="main-nav" class="two thirds column omega">
<ul>
<li>
Home
</li>
<li>
About Us
</li>
<li>
News
</li>
<li>
Dealers
</li>
<li>
Products
</li>
<li>
Buy Online
</li>
<li>
Contact
</li>
</ul>
</nav>
</div>
</header>
Code to grab header.php
<?php include($_SERVER['DOCUMENT_ROOT']."/header.php"); ?>
FTP Folder Structure
You should use relative urls, its recomended for portability.
</hgroup>
<nav id="main-nav" class="two thirds column omega">
<ul>
<li>
Home
</li>
<li>
About Us
</li>
<li>
News
</li>
<li>
Dealers
</li>
<li>
Products
</li>
<li>
Buy Online
</li>
<li>
Contact
</li>
</ul>
</nav>
</div>
</header>
Always use relative URLs, at least to the domain.
<link rel="stylesheet" href="/css/style.css" />
If you really wanna use absolute URLs, set a variable as $homeroot and use it this way:
<link rel="stylesheet" href="<?php echo $homeroot; ?>/css/style.css" />
And you can declare it this way:
$homeroot = "http://my.example.com";
If you still want that to be more better, you can use this way:
$homeroot = $_SERVER["HTTP_HOST"];
You can do two things.
First -
Home // The relative urls
Second -
Define a variable with the host name -
$host = ($_SERVER['HTTP_HOST'] === 'www.cosworth-europe.co.uk') ? 'http://www.cosworth-europe.co.uk' : 'http://localhost';
And
Home
You should not use absolute URLs, if you sometimes have to change something you will have a big mess with it.
You should use either a function or relative URLs.
Function example:
function getURL() {
//get THIS ($develop) value from a config file
$develop = true;
return ( $develop ) ? "localhost" : "http://".$_SERVER["HTTP_HOST"];
}
i.e.
Home
QUICKHELP:
Just edit your hosts file like this:
127.0.0.1 www.cosworth-europe.co.uk
Relative URLs (relative from doc root in this example)
<ul>
<li>
Home
</li>
<li>
About Us
</li>
<li>
News
</li>
</ul>
If you want to make it work without editing your source file:
Actually, you shouldn't have done with absolute URL's, but still you can make it to work locally, you need small changes in hosts file. I don't know which OS you are currently in, so you can edit hosts like this:
Ubuntu:
In your terminal, type
sudo gedit /etc/hosts
You will be required to enter your password.
In the file opened add this line to the end:
127.0.0.1 www.cosworth-europe.co.uk
Windows:
c:\windows\system32\drivers\etc\hosts
Open this in notepad with administrative privilege and add the above lines. Now local works in the similar way to your live website.
[But make sure to remove these lines, if you want to access live website (www.cosworth-europe.co.uk)]
See this for pictorial representation. In this method you need not change your files for now. But it is strongly recommended to use relative URLs for excellent portability!

disable TYPO3 link parsing in html block

In TYPO3 I used html blocks which contains a nav of hash-tags
<li class="navbox default">
About us
</li>
<li class="navbox default">
Shop
</li>
<li class="navbox default">
Images
</li>
<li class="navbox default">
News
</li>
<li class="navbox default">
Sale
</li>
<li class="navbox default">
Contact us
</li>
This nav is used for Bootstraps Scroll-Spy and the hash-tags represent content-elements. Everything works fine, but only for the page "/". If I open the page in another language like /de.html or /en.html, TYPO3 prepend this part to the href and I get something like this
About us
This is fine for most cases, but Bootstraps Scroll-Spy expect all links as a "pure" Hash without anything in front.
My question is now, how can I disable TYPO3 parsing of html blocks/link replacement? I don't want to modify the bootstrap js files
config.prefixLocalAnchors = 0 is your friend if it is compatible to the rest of your environment (e.g. RealURL configuration):
http://docs.typo3.org/typo3cms/TyposcriptReference/Setup/Config/Index.html?highlight=prefixlocalanchors

Making dynamic navigations

I just want to ask if what would I do to transfer the class attribute "active" based on the page I visit on my site.
For example [Bootstrap snippet]:
<ul class="nav nav-pills">
<li class="active">Home</li>
<li>Profile</li>
<li>Messages</li>
</ul>
So if I visit profile, the class is transferred into the <li> tag of Profile.
Try this
<ul class="nav nav-pills">
<li <?php if($_SERVER['REQUEST_URI']=='/index.php'){ ?> class="active" <?php } ?>>Home</li>
<li <?php if($_SERVER['REQUEST_URI']=='/profile.php'){ ?> class="active" <?php } ?>>Profile</li>
<li <?php if($_SERVER['REQUEST_URI']=='/message.php'){ ?> class="active" <?php } ?>>Messages</li>
</ul>
//we are taking demo php file like index.php,profile.php,message.php. you can use your own
Hope it will help
If you don't mind using Javascript on your site, Bootstrap has some built-in functionality to do this for you. Just make sure you have targets with resolvable id's (such as <div id="home">).
The biggest issue I run into with this design is that all of your "pages" are rendered at load. I find myself having to use a lot of AJAX if I am building anything that changes based on user input.
From the bootstrap site:
Markup
You can activate a tab or pill navigation without writing any JavaScript by simply specifying data-toggle="tab" or data-toggle="pill" on an element. Adding the nav and nav-tabs classes to the tab ul will apply the Bootstrap tab styling.
<ul class="nav nav-tabs">
<li>Home</li>
<li>Profile</li>
<li>Messages</li>
<li>Settings</li>
</ul>

Categories