PHP file from subfolder does not call css - php

I had to call my resources/includes/header-test.php from a subfolder called subfolder/mypage.php. That was solved with the following code, but gave me a new problem.
<?php include __DIR__.'/../resources/includes/header-test.php'; ?>
The folder structure can be seen here:
The problem is now that the css is not called when I go to mypage.php. So how can I call my css, so it is working in the php files there is in the root directory, and in the subfolders?
testProject/index.php -> css is working
testProject/subfolder/mypage.php -> css is not working
header-test.php
<html>
<head>
<link href="css/style.css" rel="stylesheet" />
</head>
<h1>THIS IS THE HEADER FROM INCLUDE</h1>
index.php
<?php include 'resources/includes/header-test.php' ?>
<body>
<h2 class="font">THIS IS THE INDEX BODY</h2>
<p>The css works on this page when I link to the css like this in the header:</p>
<p>link href="css/style.css" rel="stylesheet" </p>
MyPage subfolder link
</body>
</html>
mypage.php
<?php include __DIR__.'/../resources/includes/header-test.php'; ?>
<body>
<h2 class="font">THIS IS THE BODY OF SUBFOLDER/MYPAGE.PHP</h2>
<p>The css does not work when I link like this in the header</p>
<p>link href="css/style.css" rel="stylesheet"</p>
</body>
</html>
style.css
.font {
color: red;
}

This is a problem with the css/style.css path being relative the the file. You should prefix it with / to tell the page to load the asset from the 'root' of the web directory. In short it should be /css/style.css and that will work for both pages.
You should note that the root of the web directory, is different from the file server root. If you're developing locally and have a file such as index.html that lives in /path/to/your/website/ and you view it in your browser with /path/to/your/website/index.html, it will break the path to the asset.

Related

Add a css file to Kohana template

I want to add a css file to my template. I created the template.php into the folder : kohana-v3.3.5\application\views. Into this folder, I created an other folder called "css" and inside it, there is my styles.css with some code.
For now, my template file is :
<html>
<head>
<link href="/css/bootstrap-3.3.6-dist/css/bootstrap.css" rel="stylesheet">
<link href="css/styles.css" rel="stylesheet">
</head>
<body>
<div class="row">
<div class="col-md-12 banner">
Persyst
</div>
</div>
<?php echo $content; ?>
</body>
</html>
I don't understand why the links for my css files aren't working.
I saw something with HTML:style() but it doesn't work either.
Thanks for your help !
Using HTML::style() is a good idea. The reason why it fails is because you put the assets folder inside the template folder. The latter is outside of web access (or should be) and is only used internally.
Kohana routes everything through the index.php file, so this is the level your stylesheets, scripts and images belong.
<html>
<head>
<?php
print HTML::style('vendor/bootstrap-3.3.6-dist/css/bootstrap.css') ."\n"
. HTML::style('assets/css/styles.css');
?>
With a directory structure like
application
modules
system
assets
css
js
vendor
bootstrap

php require method wont display css styling

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

External Style Sheet

I've created an CSS external style sheet for a webpage of my website. I have uploaded it to the server and have used the following code in my PHP file to load the webpage. However, the webpage just loads as if it ignores the style sheet. Here's the code in the PHP file that I have used so I just uploaded the HTML tags here. Any help would be a great help. Thanks.
<html>
<head>
<link rel="styleshhet" type="text/css" href="/webspace/httpdocs/new_select3_style_sheet.css">
<script>function goBack() {window.history.back()}
</script>
</head>
<body>
<div id="main">
</div>
</body>
</html>
<link rel="styleshhet" type="text/css" href="/webspace/httpdocs/new_select3_style_sheet.css">
goes to:
<link rel="stylesheet" type="text/css" href="new_select3_style_sheet.css">
First you had a typo -> stylesheet not stylshhet second all paths run from above httpdocs in public facing HTML (where your site will be) so you need to remove everything above that! e.g.
/webspace/httpdocs/new_select3_style_sheet.css
If your domain was example.com then /webspace/httpdocs/ = example.com/ when building URLs

Create design template file in PHP

I intent to create a template PHP file, this template will only serve the design, not the content. The purpose is to decrease the development time in a sense that when creating a new PHP file or new module, I can only need to concentrate on the main function of that PHP file not the design. Once I created the new file using the template, it should be able to display the consistent design and serve its specific function.
The issue is that I am not sure on how to make the design of the template works and applied to all of the new files created regardless of the location (as long as it is within the root directory).
As an example:
root directory (www.example.com): /
homepage (www.example.com/index.php): /index.php
css file: /style/style.css
template file: /template.php
newly created file (www.example.com/subone/find/css/file.php): /subone/find/css/file.php
another newly created file (www.example.com/subtwo/locate/css.php): /subtwo/locate/css.php
Content of the homepage (which is created base on the template.php, but the CSS file location is hard coded):
<html>
<head>
<title>Testing</title>
<link rel="stylesheet" type="text/css" href="style/style.css" />
</head>
<body>
<div id="header">logo and login form goes here
<div class="nav"> navigation goes here;</div>
</div>
<div id="main">main content goes here;</div>
<div id="footer">footer goes here; </div>
</body>
</html>
but, when I created a new file, /subone/find/css/file.php
the location of the css must be changed and specified manually, like this:
<html>
<head>
<title>Testing</title>
<link rel="stylesheet" type="text/css" href="../../../style/style.css" />
</head>
<body>
<div id="header">logo and login form goes here
<div class="nav"> navigation goes here</div>
</div>
<div id="main">main content goes here;</div>
<div id="footer">footer goes here;</div>
</body>
</html>
So, what I want to achieve is that, when creating a new file (/subone/find/css/file.php), I don't need to do anything, I can straight away concentrate on the main section:
<html>
<head>
<title>Testing</title>
...style.css is handled automatically
</head>
<body>
<div id="header">logo and login form goes here
<div class="nav"> navigation goes here</div>
</div>
<div id="main">main content goes here;
<?php
//I can continue to edit the file from this line onward
echo "I am concentrating on the main function of file.php right now!!";
?>
</div>
<div id="footer">footer goes here;</div>
</body>
</html>
example page can be seen at (only the desired design): neoborn.kodingen.com
I accept any answers as long as it can achieve my intention (template).
Thank you :)
Why don't you use absolute paths when referring to CSS files and other resources in your template file?
For example:
<link rel="stylesheet" type="text/css" href="/style/style.css" />
There are 2 options,
Use absolute paths for your css files <link rel=stylesheet href="/style/style.css">
Use HTML's <base> element to cause all relative paths on the page relate to it.
I would use a easy to install template engine. That will help speed up development and still give you the freedom to do whatever PHP you like.
Try http://www.raintpl.com/ that should be quick and easy for you to install and get back to coding the pages. If you include it in your PHP inc folder, it will be available for every PHP file you create. So you won't need to add an include line at the top of each PHP file.
Index.php
<?php define('BASE_URL', 'http://localhost'); ?>
Template.php
<link rel="stylsheet" type="text/css" href="<?php echo BASE_URL; ?>/style/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