php require method wont display css styling - php

I created a index.php file that's uses <?php require("sidebar.html"); ?> to include a sidebar; the HTML element of the sidebar shows, however the css styling isn't showing. I've search Google and tried different method but it's not showing, any help would be highly appreciated.
The sidebar.html is located in HTML/ folder. And index.php is located in root/ folder
The css styling for the sidebar is being reference within the sidebar.html file
My css file is located in CSS/ folder
new to web development; Trying to make a sidebar that I can call on every page instead of hard-coding it to every page.

When you include an HTML file into a PHP script, path to all the related files (i.e. files that are referenced in the HTML document) must be relative to the PHP script in which you have included the HTML.
Have a look at the file structure below:
Home
index.php
Includes
Assets
style.css
action.js
header.html
The Assets directory contains CSS and JS files which are included in header.html. Now, if header.html has to be included in index.php that is inside the Home directory, the src/href attributes need to point to the path of css/js files relative to index.php.
Something like this:
<link rel="stylesheet" href="../Includes/Assets/style.css" />
Happy coding :)

Prepare a BASE URL at the top of the page like below (based on your project directory location).
Try
$baseURL = "http://".$_SERVER['SERVER_NAME'];
or
$baseURL = "http://".$_SERVER['SERVER_NAME']."/your_project_dir/";
Use HTML base tag in your <head> tag before your<link> tags like
<html>
<head>
<title>Project Title</title>
<base href="<?php echo $baseURL;">
<link href="your_css_file.css" type="text/css">
</head>
<body>
page content
</body>
</html>
Cross check your "your_css_file.css" location (like css/your_css_file.css or styles/your_css_file.css ...)

Try to add
<style>
...
</style>
In the first and end of your css require file, the css will read as internal stylesheet. And change the extension .css to
.php

Related

Including css from other folder

I would like to know whether there's an easy common way to include a css file, which is in a folder, from a html/php file inside another folder?
The structure looks like this:
index.php
about
index.php
css
style.css
What I want is to know how to get the correct path to the style.css file inside the css folder from the index.php hidden in that about directory.
This is going in about/index.php?
<link rel="stylesheet" href="../css/style.css" />
.. basically says, "Go up one level from where this script is located."

How to properly link CSS files with PHP footer and header

So I've recently discovered how to use php includes to include a footer and header in each of my files to avoid copy pasting all the header/footer code to each file. But let's say I have a footer.php, header.php, home.php, and about.php
Do I have my title, opening html/body tag, etc. in the header.php or home.php and about.php.
//header.php
<html>
<head>
links to header.css
links to home.css
links to about.css
</head>
<body>
//home.php
<?php include("header.php"); ?> //PROBLEM: the header.php also includes other .css such as "about.css", etc. that could result in problems later.
</body>
</html>
What Should I do to fix this? One way I thought of is to remove the beginning part(html,head,title) of the header.php file and move it home.css and about.css so they each have their own css links.
You're on the right track. Break out the stylesheets as well as the javascripts into other php files and include them as well. So all pages have the following structure.
home.php
<?php $this_page = "home.php";
include "template.php";
For other pages, just replace the $this_page variable. The structure common to all pages is actually the template.
template.php
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<!-- CSS-->
<?php include "stylesheets.php" ?>
</head>
<body>
<!-- common header -->
<?php include "header.php" ?>
<section>
<!-- PAGE CONTENT HERE determined by $this_page value -->
<!-- 'content_home.php', 'content_about.php'... have the content-->
<?php include "content_$this_page" ?>
</section>
<!-- common footer -->
<?php include "footer.php" ?>
<!-- link javascript files -->
<?php include "scripts.php" ?>
</body>
</html>
The only thing that changes from one page to the next is the value of $this_page. It's what determines which content gets loaded in the template above, and it also determines which CSS and JS files to include.
stylesheets.php
<?php
$cssDir = "path/to/styles/"; //folder where all CSS files live
//Link each page to its CSS file
$styles = [
'home.php' => 'home.css',
'about.php' => 'about.css',
'contact.php' => 'contact.css',
];
?>
<!-- CSS common to all pages -->
<link rel="stylesheet" type="text/css" href="<?="$cssDir/common.css"?>>
<!-- CSS, specific to the current page -->
<link rel="stylesheet" type="text/css" href="<?="$cssDir/$styles[$this_page]"?>>
The same approach can be used with the javascript you link to in scripts.php. Now that your HTML is into discrete modules, it is easy to edit a part of your site without worrying about another part breaking. In particular I recommend never to open a tag in one php file and close it in another because that would be a nightmare to debug, maintain and modify as your site gets bigger.
About paths:
Remember that when the browser sees the page, in place of include "stylesheets.php" and include "scripts.php", it will see the echoed contents of that file exactly as they are. So in those files you want your path to be either:
absolute paths from your domain root (simplest)
relative paths from the location of the top-level php file (eg home.php)
just the file name, if it is located in PHP's include PATH (places where PHP looks for content by default before throwing an error)
For header and nav you have to create a seperate file like nav.php which will contain only the nav and your site header not <head></head> and include it after your header.php. LIKE
//Home.php
<?php
include("header.php"); this will contain your head part mostly your .css and .js files
include("nav.php"); This will only contain header and nav
// home.php code goes here
?>
Also use below code will automatically get path to your root.
<?php
$PATH = "http://localhost/Folder/"; // change this when needed
$PAGE = basename($_SERVER['PHP_SELF']);
?>
Then Add your files like this
<link rel="stylesheet" href="<?php echo $PATH; ?>assets/plugins/font-awesome/css/font-awesome.css">
the bottom line is your code being accessible & easy to maintain, I would have a head.php, header.php & footer.php file. In the head.php you may want to include your config.php if you are connecting to a database & also have all the <html><head><title><link><script> tags you will include in every page then on your index.php or home.php
include('head.php');
include('header.php');
etc etc

trouble with php include and file paths

I have a question related to php include.
I have a folder called login and another folder files. I have a footer.php in files and its stylesheet lies also in same folder as style.css.
so , I want to include footer.php in login.php as <?php include('../files/footer.php');?> but its css is missing. how can I solve this problem?
You make your login.php extend some layout.php that has all the required CSS files attached.
When you write your html code, use the complete url (start with http://) to your css and js file in the <head>. This way you will not have problems about relative paths
In the head, you should use an absolute path for your css link:
<link rel="stylesheet" href="http://localhost/your-main-folder/files/footer.css" />
or you can use this:
<base href="http://localhost/your-main-folder/" />
<link rel="stylesheet" href="files/footer.css" />

php include file from another directory

Here is a structure example:
/main
/css
style.css
/include
article1.php
article2.php
header.php
index.php
In my header.php I have the following code for the css:
<link rel="stylesheet" type="text/css" href="css/style.css" />
And, for example, in my index.php I have the following as the first line:
<?php include 'header.php'; ?>
Now, everything works fine as it is. Next I'm going to insert the following code in the article1.php file:
<?php include '../header.php'; ?>
The contents (menus and other html) are displayed correctly, however the CSS won't be displayed/recognized at all. Basically what's happening is that the header file is being included but the server isn't respecting the directory parenting. For the CSS to be displayed correctly I'd have to change the link rel for the CSS to ../css/style.css, but if I do so it won't work on files located in the main directory.
I hope I made my problem clear. What am I doing wrong? How can I include files from different directories and preserve the links inside them?
In your site's <head> section, insert a <base> element with the href attribute. Set the href attribute to the base URL of your website, and all relative requests will be sent through that base URL.
<base href="http://my-website-url.com/" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
With a correctly-set base tag, the user's browser will attempt to load your stylesheet from the URL http://my-website-url.com/css/style.css.
Note: this not only affects stylesheets, but all relative links in the document.
It has to do with how pathing works in includes. I recommend pathing things from the doc root whenever possible.
<?php include( $_SERVER['DOCUMENT_ROOT'] . '/header.php' ); ?>
That will work in any of your files.
Instead of using relative paths:
href="css/style.css"
use absolute paths from the webroot:
href="/css/style.css"
You should include your css file from the root. So /css/style.css so way it will always start at the root and then go down from there. I believe that should fix your problem in all cases.
First off, the problem is that your link to your CSS files is wrong. The best thing to do is look at the output of the HTML (view source) from the page. So lets break it down (form index.php):
Index.php is located at domain.tld/index.php. Your css files are located at domain.tld/css/*. When viewing the index file, your header is
<link rel="stylesheet" type="text/css" href="css/style.css" />
which works because you are at the top of the directory (or root) and the links are made relative to this directory. Now when you go up to domain.tld/include/article1.php, you are no longer at the root. It is trying to access:
domain.tld/include/css/style.css
This means you have to build the full link or change your relative. The easy way since the CSS is at the root is just to use the following
<link rel="stylesheet" type="text/css" href="/css/style.css" />
^
This means it will look at the root of the domain for the css directory as such domain.tld/css/styles.css. If your top directory is /main, use /main/css/styles.css
Using relative includes from filed already called as includes is always a problem, each time you specify a relative location, replace your
../some_directory/some_file.php
with
dirname(dirname(__FILE__)) . '/some_directory/some_file.php'
dirname(__FILE__) is the current directory.
May I explain your problem and how to solve it.
An include in php works as in C.
When you include a page that copy/paste content of the page into the first one.
So, there are two files:
include.php
<?php
$var = 'PHP';
?>
main.php
<?php
include 'include.php';
echo $var; // affiche 'PHP'
?>
When you request for "main.php" page you will get the following:
<?php
$var = 'PHP';
echo $var; // affiche 'PHP'
?>
So if you need to include an element to your page. You have two choices:
1. absolute path (better way)
Instead of using relative path, use absolute path. This way allows you to include a file from everywhere without of use of the current path.
2. variable
You can use a variable containing the relative path to the root directory of your repository/website. So each time you have to include a page or create one you have to define a variable as following:
include.php
<?php
$var = 'PHP';
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"".$currentPath."css/style.css\" />";
?>
main.php
<?php
# path to the root of the website
$currentPath = "../";
include 'include.php';
echo $var; // affiche 'PHP'
?>
To learn more about include, see this page
You have two options
Pass the relative path to the included file
ie.
<link rel="stylesheet" type="text/css" href="<?=$path ?>css/style.css" />
and
$path = "../";
include "header.php";
or 2. you use absolute paths to your files
<link rel="stylesheet" type="text/css" href="/css/style.css" />

Web development - relative URLs without duplicating files

I have a site with index.php in the root folder, images in /img , and overview.php in /content. I have a sidebar.php file that is included in both index.php and overview.php. How should I refer to /img/image.gif if I include a link in each file?
The location of image.gif changes relative to the location of the file that references it.
Using /img/image.gif in sidebar.php will work in index.php, but it fails for the file located at /content/overview.php.
The only solution that I can see is to either include a separate sidebar.php in each sub-directory, or include an /img directory in every sub-directory.
The best suggestion that I can find is to use the <base> HTML tag as suggested here:
Change relative link paths for included content in PHP
However, in the same link, SamGoody suggests that the <base> tag is no longer properly supported in Internet Explorer, since version 7.
I'd like some insight on the matter before committing to a course of action.
Thanks.
EDIT: I am using the wrong approach below with "../"
Example-
root/index.php:
...
<link rel="stylesheet" type="text/css" href="style.css" />
<title>title</title>
</head>
<body>
<?php include('include/header.php'); ?>
<?php include('include/menu.php'); ?>
...
root/include/header.php:
...
<div id="header">
<span class="fl"><img src="img/dun1.png"/></span><span class="fr"><img src="img/dun2.png"/></span>
...
root/content/overview.php:
...
<link rel="stylesheet" type="text/css" href="../style.css" media="screen" />
<title>Overview</title>
</head>
<body>
<?php include('../include/header.php'); ?>
<?php include('../include/menu.php'); ?>
...
Using /img/image.gif in sidebar.php will work in index.php, but it fails for the file located at /content/overview.php
But it shouldn't. The preceding / makes it an absolute path which will work from any point on the server. If this doesn't work for you, there's a problem somewhere - in that case, post some examples.
Unless you are planning to move the whole site into a sub-directory one day, or move images to a Content Delivery Network (both actions would require re-writing the addresses) you can safely use absolute URLs.

Categories