4

Here is a part of my CI code:

    class page extends CI_Controller {

        var $Page;

        public function __construct() {
            parent::__construct();
            $this->Page = 1;
            $this->load->model('posts_model');
            $this->load->helper('url');
        }


        public function index() {
            $data['posts'] = $this->posts_model->get_posts($this->Page);
            $this->load->view('header');
            $this->load->view('main', $data);
            $this->load->view('footer');
        }

        function page_num($page) {
            $this->Page = $page;
            $data['posts'] = $this->posts_model->get_posts($this->Page);
            echo $this->Page;
            $this->load->view('header');
            $this->load->view('main', $data);
            $this->load->view('footer');
        }

    }

And this is link tag of my View File:

<link rel="stylesheet" type="text/css" href="css/indexpage.css" media="all"/>

When I open the index file (/My-Site/), CSS works fine, but when I open for

example the url :

" /My-Site/page/page_num/3 ",

the page opens but with no CSS styles!

Can any body help me please?

4
  • try href="<?php echo base_url();?>css/style.css" Commented Oct 12, 2012 at 8:31
  • In which view file did you keep the css link? Commented Oct 12, 2012 at 8:32
  • @BhuvanRikka : in the root, css file ( not in application file ) Commented Oct 12, 2012 at 8:35
  • i'm not talking about the file.. i'm talking about the file name of your view in which you've kept the css link.. Did you keep it in header ? Commented Oct 12, 2012 at 8:37

7 Answers 7

11

use base_url() function to get the url of the site

base_url() gives the url of the website where the index file is located.

eg: /My-Site/

or you can give the file location as the parameter: base_url('/css/indexpage.css') then use it as <link rel="stylesheet" type="text/css" href="<?php echo base_url('/css/indexpage.css');?>" media="all"/>

Sign up to request clarification or add additional context in comments.

Comments

4

Try it.

you can use base_url() in your css. see below code.

<link rel="stylesheet" href="<?php echo base_url(); ?>css/style.css" type="text/css" media="all" />

Comments

3

1 - change it to :

<link rel="stylesheet" type="text/css" href="<?php echo base_url('css/indexpage.css');?>" media="all"/>

and you sould already know this , in

application/config/config.php

set

$config['base_url'] = 'www.yorsite.com';

2 -

you have to include the css only in one of your views

i.e

if you have include it in the header.php file do not add it to the main.php or footer.php

Comments

2

It is usually a good practice to use

<base href="<?php echo base_url(); ?>" />

in your head section of the view page. That way you don't have to worry about adding base_url to all your href and src attributes. So if your css file is in codeigniter/assets/css folder, just use

<link type="text/css" rel="stylesheet" href="assets/css/style.css" />

Similarly for subsequent scripts and css you can just use "assets/js/filename" or "assets/css/filename".

Comments

0

Change URL here:

<link rel="stylesheet" type="text/css" href="css/indexpage.css" media="all"/>

to:

<link rel="stylesheet" type="text/css" href="/css/indexpage.css" media="all"/>

Slash at the beginning will point to domain without any sub categories.

2 Comments

I did this, but CSS is not working with my index file any more!
This only works when the website has an hostname (vhosts) like mylocaldomain.local or if the website is the default website of the server. The slash will bring the path to the root of the website, so if the css folder is in the root and you're using a hostname, then this should work.
0

When working with codeigniter remember that the only page that outputs HTML is the index.php file so your css should be relative to this file or you use absolute url. You can create a folder anywhere some developers create an asset folder at the webroot here they place their javascript folder image folder and css folder. But the safest way to go is to use absolute links for your assets-Javascript, css, and images. The easiest way to use absolute link is to use base_url("assets/css/indexpage.css"); This is how to use it:

  1. Load url helper in any of your controllers. $this->load->helper('url);
  2. In your view the area where you want to do the linking use the base url function.

Comments

-2

It depends on where you have placed the css file. Try ../css/indexpage.css

1 Comment

very vague answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.