How to get css file from any file using php? - php

Here is what I want to state
I have this structure-
Now considering the directory I want to add the CSS file to all the files if I am using the project-1 > sub-project > subfile.php
then I should be using <link rel="stylesheet" href="../../asset/css/style.css" />
Again if I use the project-1 > file.php
then I should be using <link rel="stylesheet" href="../asset/css/style.css" />
Now even if I use index.php
then it should be like <link rel="stylesheet" href="asset/css/style.css" />
In short, I want it to access CSS file from any file dynamically. It's a type of autoloading whenever included.
I have tried something but it failed because it's not dynamic
$search = glob("asset/css/style.css");
foreach ($search as $ser) {
$ser = $ser;
}
if(strpos($ser,"asset") !== false){
echo "<link type='text/css' rel='stylesheet' href='asset/css/style.css'>";
}else {
echo "<link type='text/css' rel='stylesheet' href='../asset/css/style.css'>";
}
But the problem is it is not dynamic. Please provide a solid solution. Thanks a lot in advance.

I'm assuming you have domain name like:
domain.local
And your init style url looks like that:
href="asset/css/style.css"
Then you trying to access your project located under proj1 directory:
domain.local/proj1/index.php
using same styles method the url will look like this:
domain.local/proj1/asset/css/style.css
And they are no longer applied
Solution:
You need to use relative approach.
Try this in your link:
<link rel="stylesheet" href="/asset/css/style.css" />
This will helps you no mater how many directories are, always grab styles using root as initial point.
Here are relative question: Why the CSS style is not applied in webpages within subdirectories?

Related

Show HTML content wit PHP (and load style)

I have some HTML pages (in "web" folder) that use some styles and scripts, and i need to show these HTML pages using a PHP script. Here is a simplified version of the code i'm using:
<?php
readfile("web/index.html");
It works but it doesn't load any resources (css, js, images) because the URLs are not absolute :
<link href="css/bootstrap.css" rel='stylesheet' type='text/css' />
<link href="css/style.css" rel='stylesheet' type='text/css' />
It doesn't find the resources because it's looking for the css folder in the same folder as the PHP script.
Any workaround for this ?
You'll need to add add <base href='...'> into the html, like this (not tested):
$page_content=preg_replace("%</head%i","<base href='web/'></head",$page_content);
insert this after the readfile. See MDN web docs for more information about that tag.
In some browsers (Firefox and edge at least) it also works if you print the base tag before you output $page_content. It's dirty, but maybe it's good enough.
Simple you can use something like this
index.php
<html>
<head>
<php include("menu.php"); ?>
</head>
<body>
</body>
</html>
menu.php
<link href="css/bootstrap.css" rel='stylesheet' type='text/css' />
<link href="css/style.css" rel='stylesheet' type='text/css' />
for base path use some variable like $base = "http://localhost" then use your elements like <img scr="<?php $base ?>/images/myimage.jpg" />
Hope this answers your question

How can I call a specific CSS file based on a specific HTML class on the page?

For example, my site has 5 CSS files like this...
<link rel="stylesheet" href="/templates/purity_iii/css/custom_marketing.css" type="text/css" />
<link rel="stylesheet" href="/templates/purity_iii/css/custom_gps.css" type="text/css" />
<link rel="stylesheet" href="/templates/purity_iii/css/custom_fleet.css" type="text/css" />
<link rel="stylesheet" href="/templates/purity_iii/css/custom_service.css" type="text/css" />
<link rel="stylesheet" href="/templates/purity_iii/css/custom_corporate.css" type="text/css" />
Every page of the site will be in one of those "categories" (i.e. marketing, gps, fleet, service or corporate). And this is indicated by a class on the HTML tag. So something like this at the top of every page...
<html class="gps">
I currently have EVERY page calling all 5 style sheets listed above, but it only NEEDS the corresponding style sheet, so I'd like to be efficient if possible.
Is there a way using PHP (or whatever makes sense) to essentially search the tag's class for "gps" or "fleet" (etc.) and if, for example, it found "gps", I could then echo only that style sheet, i.e...
echo "<link rel="stylesheet" href="/templates/purity_iii/css/custom_gps.css" type="text/css" />";
This totally depends on how PHP generates your HTML. Another possibility is to do this with JavaScript. You can do it like this:
<script type="text/javascript">
var file = 'templates/purity_iii/css/custom_' + document.documentElement.className + '.css';
var link = document.createElement('link');
link.href = file;
link.type = 'text/css';
link.rel = 'stylesheet';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
You can for example place the code above between <head> and </head>. Not really a valid answer to your question (PHP solution), but you could consider this as an alternative.
This seems like a poor strategy, albeit I don't know the size of your CSS files and other parameters.
How different are these pages as far as their style goes? From the file names above it looks like you might have a lot of redundant (duplicate) CSS selectors in each file. How much of the CSS in each of those files is actually unique? Is it 5k? 10k? 50k? If it's fairly small, go ahead an put it all in one file. By placing it in one file all the CSS for all the pages of your site will be cached in the user's browser and no additional requests would be needed for subsequent pages.
If combining all files and you have a 500k file and 250k is for a single page then splitting it would make more sense.
A PHP Solution
I'm guessing that your setting the CSS class on the <html> tag with PHP. If so, why not check the value of that variable in your PHP script and add the appropriate CSS file.
Something like this:
<html class="<?php echo $page_class; ?>">
<head>
<link href="custom_<?php echo $page_class; ?>.css">
</head>
This is advice is fairly general but hopefully it points you in the right direction.
try something like
<?php
if($page_type = 'gps')
{
?>
<link rel="stylesheet" href="/templates/purity_iii/css/custom_gps.css" type="text/css" />
<?php
}
elseif($page_type = 'marketing')
{
?>
<link rel="stylesheet" href="/templates/purity_iii/css/custom_marketing.css" type="text/css" />
<?php
}
?>

CSS not working from relative path with php 'includes'

folder structure
/
|--index.php
+--includes
|--header.html
+--css
|--style.css
I have 2 subfolders in my main project folder. One is a folder called 'includes' and the other one is called 'css'.
I have my
-index.php file in the main folder
-header.html in my 'main/includes' folder
-style.css in my 'main/css' folder
My index.php includes the header.html like this:include_once('includes/header.html'); (this Works!)
My header.html file links the css like this:<link href='../css/style.css' type='text/css' rel='stylesheet'/>
(this does NOT work!)
I don't understand why the css file is not loaded.
I have tried using the base tag, although I'm not sure I'm using it right.
<base href="http://localhost/main" /> (this is NOT working)
You should try using
<link href='css/style.css' type='text/css' rel='stylesheet'/>
As index.php and the css folder lie at the same level.
With
<link href='../css/style.css' type='text/css' rel='stylesheet'/>,
you are asking your server to look after the style.css in upper level directory of index.php which does not exist.
You can also use / because, it points do the document root of the website.
<link href="/css/style.css" type="text/css" rel="stylesheet" />
I don't think you understand how include works. Essentially the code in the referenced file will be copied into the main file, and then processed. So if you're including it into index.php, then you want to reference the CSS file accordingly.
The following should work:
<link href="/css/style.css" type="text/css" rel="stylesheet" />
You'll find it is the most easy to just use absolute paths when using HTML, that way the above like will still be valid, even if you copy it to a file that is in within a folder besides the root.
$siteurl ="http://localhost/project";
(store this variable in config file so that you can use globally)
<link href='../css/style.css' type='text/css' rel='stylesheet'/>
will be changed to
<link href='<?php echo $siteurl;?>/css/style.css' type='text/css' rel='stylesheet'/>
just change from <link href='../css/style.css' type='text/css' rel='stylesheet'/> to <link href='css/style.css' type='text/css' rel='stylesheet'/>
If you are trying to add CSS in html file using PHP require:
<style><?php require("css/style.css");?></style>
Note: <style> tag is important otherwise it will echo plain text

How to refer to css file in codeigniter?

I use XAMPP and CI 2.14
(I know it's sounds ridiculous, but...) I don't know how to refer to a css file from a view. I've read and tried a lot of examples but, obviously I do something wrong...
<link rel="stylesheet" href="<?php echo base_url();?>css/main.css" type="text/css" />
The base url is: http://localhost/myapp/
Where should I put then main.css file (now is in the webroot/myapp/css folder), and how to reference it from a codeigniter view?
create folder assets:
path is: /assets/css/your_file.css
<link rel="stylesheet" href="/assets/css/your_file.css" type="text/css" />
Try to put the css folder inside the application folder, like this:
/yourapp/application/css/
And then
<link rel="stylesheet" href="<?php echo base_url();?>application/css/main.css" type="text/css" />

Header template wont alway pull up css

I am new to php, but suspect there is a simple solution that I am not aware of.
I have made a template for the header on every page, but when the user loads pages the css page changes in relation to their current page.
How do I have php track how many levels up in a folder the user is so I can pull the css from anywhere in the website?
<link rel="stylesheet" href="../templates/css/css.css" type="text/css" />
That is my current link to css, but for a page further down in folders I need it to add additional ../
You should use an absolute path from the root of the website (note, no ".." just a "/"):
<link rel='stylesheet' href='/templates/css/css.css' type='text/css' />
Will always work, as long as your css is at:
http://yourwebsite.com/templates/css/css.css
you shouldn't be using a relative path then. Why not just do something like:
<link rel="stylesheet" href="http://www.mysite.com/templates/css/css.css" type="text/css" />
or
<link rel="stylesheet" href="/templates/css/css.css" type="text/css" />
or
<link rel="stylesheet" href="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>/templates/css/css.css" type="text/css" />
whichever suits your needs - since you may be working locally and have a strange file structure, or a shared style directory for example
The best method is to use:
<link rel="stylesheet" href="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>/templates/css/css.css" type="text/css" />

Categories