views:

86

answers:

4

Hi, 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 ?

A: 

If your provided code is exactly what you're using, then you're not setting a link to a css file.

   $strTemplate.="<link rel='stylesheet' type='text/css' href='".base_url()."user/defaultCss/$name_of_your_css_file.css">"
Anzeo
I have edited my question. defaultCss.php is the file in which I want to load the css. Thanks for quick reply.
KutePHP
OK, and is this your exact code? I need to know for my answer :)
Anzeo
+1  A: 

Well the name of your controller action is loadDefaultCSS, so I would expect the URL for the generated stylesheet to be: (assuming your controller is indeed called User)

base_url()."user/loadDefaultCSS"

Does this work?:

$strTemplate .= '<link rel="stylesheet" type="text/css"
    href="'.base_url().'"user/loadDefaultCSS">';
captaintokyo
A: 

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.

Alpesh