CSS not loading with codeigniter - php

IM trying to load some css directly within my view file, but its not working.
in my view file I have this.
<link href="<?php echo base_url(); ?>assets/css/core.css" rel="stylesheet" type="text/css" />
The assets folder is at the same level as Application
when I view source on the webpage it shows me this
demo.example.com/assets/css/core.css
but when i click the link to see if it's working the url becomes this...
http://demo.example.com/admin/demo.example.com/assets/css/core.css
not sure what I am doing wrong? Should I be adding something to my htaccess file?
Thanks in advance.

You don't need to include the base_url() in your path because the webserver already knows your at www.example.com/admin and auto includes that.
Just use
<link href="/assets/css/core.css" rel="stylesheet" type="text/css" />
Edit: Or actually you need to include the http: prefix on your base_url like wolfgang1983 said.
See this answer on CSS absolute and relative link paths. https://stackoverflow.com/a/17621358/3585500

Related

Css file not linking with html/php file

I have looked over the code and cant seem to see where the link between the two files is failing. My index page uses the exact same code to link the files and it works fine. When I add the styling in the header of the php/html file it works but otherwise nothing happens.
The reference that you have for the stylesheet is relative to the url of the page you're on.
For example, this:
<link rel="stylesheet" type="text/css" href="style.css">
... says that the stylesheet is at the same path that the page is on. So if the url in the address bar is https://somewebsite/admin/index.php then that page will try to load the stylesheet at https://somewebsite/admin/style.css. If the url is https://somewebsite/index.php then the page will try to load the stylesheet at https://somewebsite/style.css.
If the stylesheet resides at the root of the site then your link tag should look like this:
<link rel="stylesheet" type="text/css" href="/style.css">
This will make your page look for the stylesheet at https://somewebsite/style.css regardless of what the path of the url says.
You should make the right path, right into your css file at
Example:
<link href="assets/css/blabla.css" rel="stylesheet">

Path to another css from a no-extension file in zend

I have got a project built in Zend-framework V1, where the link to another css file from a no-extensioned file is assigned in this way.
In the header file
<link rel="stylesheet" href="<?php echo $this->baseUrl() ?>/assets/user/css/login.css" type="text/css" media="all" />
But the public folder structure is like the below
public/assets/user(now this user is a file without any extension)
and when I open this "user" file I get the following line
../../packages/user/public/assets/user
I can find the login.css which is under
packages/user/public/assets/user/css/login.css
This is working fine in live but my local is not detecting the correct css.
Any help is highly appreciated. Many thanks in advance.

PHP Website - Loading Stylesheets in Sub Directories

I'm building a PHP based site with this directory structure
index.php
css
style.css
bootstrap.css
includes
header.php
footer.php
bikes
road.php
mountain.php
The Problem
So I'm working on road.php and I obviously need to be able to link to both style.css and bootstrap.css, but when I declare at the start of road.php to include the header.php and footer.php it is like as if it cannot find the stylesheets and the site reverts back to the default 1990s look.
I have also found that any form of link on the page loads a 404. I'm only just starting out with PHP because I need some more power in my sites, but I just can't seem to get my head around the super basic things.
I just don't know what to do and I'm finding myself turning my back on the whole PHP language.
Thanking you in advance,
Stu :)
I can't be certain without seeing the actual content of header.php (in perticular the part where you import the stylesheets), but it sounds like you are using a relative path to your stylesheets. Something like <link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />. This works fine for index.php, but since the other pages are inside the subfolder bikes, they will be looking for the CSS files in yoursite.com/bikes/css.
The solution is to provide an absolute path. Something like this:
<link rel="stylesheet" type="text/css" href="http://yoursite.com/css/style.css" media="screen" />
This way, it doesn't matter if the page is inside a subfolder (or a subfolder of a subfolder) - it will allways look for the CSS file in the right location.
If you are using multiple domain names, or for some other reason you cannot hardcode the domain name, you can prepend a slash (/) to the path as well:
<link rel="stylesheet" type="text/css" href="/css/style.css" media="screen" />
This path is relative to the root of the website, not to the current directory.

Add domain URL to href and src relative paths

I'm using a MVC model and I can invoke webpages with URLs like http://mywebsite.com/product/productid.html. My folder structure is the following:
views - the views folder
js - the javascript and jquery folder
css - the stylesheets folder
images - the images folder
In views folder are contained web pages used to show data to the users. They can include scripts, images and stylesheets. The following snippet is incorrect
<link rel="stylesheet" href="css/style.css" media="all" type='text/css' />
since the webpage is called with the URL above, and css can't be found with a relative path. To solve the problem, I have defined a DOMAIN variable in PHP and changed the code into
<link rel="stylesheet" href="<?php echo DOMAIN;?>css/style.css" media="all" type='text/css' />
This works, but forces me to add the <?php echo DOMAIN;?> snippet to each href and src attribute on each page. Is it possible to automate it? I've tought to use the :before selector but I don't have idea how to use it in this case.
Any ideas? Thanks in advance.
:before only applies to CSS, so that's not of use here.
There's really no way to do automatically add it in PHP that wouldn't be cpu-intensive, and/or require a significantly more complex setup than it sounds like you have right now. Using find-and-replace in your code editor is the best option.
Using <?= DOMAIN; ?> instead would be shorter, BTW. (See this question for more info)

Path to assets folder on CodeIgniter

I'm coding something into CodeIgniter and I want to apply a css template. I found one I like and download it.
After merging the css code with the code I have before the merging, I found that the images used by the css template doesn't load. Originally they should stay on root of html folder (or www, or public_html, you know what I mean...), but I put them under an assets folder which is on same level as system folder.
Something like this...
Website Folder
|
|---application\
|-----(CodeIgniter application folders...)
|---assets\
|-----style.css
|-----css\
|-------mini.css
|-----images\
|-----js\
|-----robots.txt
|---system
|-----(CodeIgniter system folder...)
|index.php
I googled for a couple of hours and I found this post (post #5). I try what the OP says but it doesn't work.
I can autoload the url_helper adding
$autoload['helper'] = array('url');
to the autoload.php file. But when I add
<base href="<?php echo base_url() ?>"/>
the images are still absent.
The original html file has this line
<link href="style.css" type="text/css" rel="stylesheet" />
so, I'm guessing that I should add assets/ so it looks
<link href="assets/style.css" type="text/css" rel="stylesheet" />
but still the images are missing.
If I move the contents of assets folder to root, everything is fine, but of course, that's not a good practice AFAIK...
My base_url is
http://localhost/prog/nonsense/mvc/
Before you ask, yes, I did read the .htacces solutions, but I really don't want to mess with .htaccess editing for now.
A little help here could be appreciated.
have you check .htaccess ?
It must have something like:
RewriteCond $1 !^(index\.php|assets|upload|robots\.txt|.*\.css)
I have the same thing as you, and I call them like this:
<link href="<?=base_url('assets/style.css');?>" rel="stylesheet">
This works for me if the base_url() is set and the url helper is called.
I call the base_url() and then the assets folder then style.css. There's no need for a / after base_url();?> because there's one in the base_url() anyway.
Another option using codeigniter's built in HTML function:
Also, if you look at this link, you'll see you can use the HTML helper and call it via codeigniter's built in functions.
This time, you shouldn't need base_url();
do:
$autoload['helper'] = array('url', 'html');
then in the view for header add this:
<?=link_tag('assets/style.css');?>
It will output to this:
<link href="http://localhost/prog/nonsense/mvc/assets/style.css" rel="stylesheet" type="text/css" />
base_url could also be used as link_tag:
<link href="<?php echo base_url('assets/style.css'); ?>" rel="stylesheet" type="text/css" />
would work too and keep your code cleaner.
yes! I just added the following line in .htacces file it working fine
RewriteCond $1 !^(index\.php|images|robots\.txt)
I just add one line in .htacces side of application folder
RewriteCond $1 !^(index\.php|images|robots\.txt)

Categories