Php – How to load a css in CodeIgniter

codeigniterPHP

I'm new to programming. I want to fetch the css saved in DB and load in a php file. I'm using CodeIgniter. After loading the css, I want to link to it as an external css. I tried following, but it is not working.

EDIT:
defaultCss.php is the file in which I want to load the css.

$strTemplate.="<link rel='stylesheet' type='text/css' href='".base_url()."user/defaultCss.php'>"

While, I view the page source it gives "Page not found" error.

Below are my controller and view code.

     function loadDefaultCSS(){
     $this->load->model('UserModel');
     $this->UserModel->loadDefaultCSS(); 
     }

View :

        if(isset($strTemplateStyles) && $strTemplateStyles!="") {
        echo $strTemplateStyles;
        }

Model function :

     function loadDefaultCSS($strTemplateStyles){
      $data['strTemplateStyles']=$strTemplateStyles;          
      }

Why this is not working ? what is the issue ?

Best Answer

You can use template library for codeigniter.

It provides more flexibility for handling views, loading js and css files. Also it provides an option for splitting the views into sections like header, content, footer etc. The template library link i have provided above is easy to use and integrate. It also has very good documentation.

In the above template library the css and js files can be loaded as follows (write below code in controller) -

For loading css files -

$this->template->add_css('path to css file');

For loading js files -

$this->template->add_js('path to js file');

For detailed documentation you can refer above hyperlink.

Related Topic