3

Hi Frnds i cannot able to load css file in codeigniter. so please tell me how to load. Codeigniter sample Directory Structure

  1. views
  2. models
  3. controllers
  4. assets a) stylesheets b) javascript c) images
  5. Config

Controller:

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    class Learnersway extends CI_Controller
    {
        function index()
        {
            $this->load->view('home_learnersway');
        }
    }
    ?>

View

    <html>
    <head>

        <link rel="stylesheet" type="text/css" href=“assets/stylesheets/main.css”   />

    </head>
    <body>
    <div class="wrapper">
    <div class="header">
    <div class="title"><img height="136" src="../images/learnersway.jpg" width="272" /></div>

    <div class="main_menu">
    <ul>
        <li>HTML</li>
        <li>XML</li>
        <li>CSS</li>

    </ul>
    </div>
    </div>

    <div class="main_container">
    <div class="sub_menu">
    <ul>
        <li>HTML Introduction</li>
        <li>HTML Editors</li>
        <li>HTML Basic</li>
        <li>HTML Elements</li>
        <li>HTML Attributes</li>
        <li>HTML Headings</li>
        <li>HTML Paragraphs</li>
        <li>HTML Formatting</li>
        <li>HTML Links</li>
        <li>HTML Head</li>

    </ul>
    </div>

    <div class="sub_container">
    <div>Under Construction...</div>
    </div>
    </div>

    <div class="footer">
    <div class="left">All Rights Reserved @ LearnersWay.com</div>

    <div class="right">
    <ul>
        <li>About Us</li>
        <li>Terms &amp; Privacy Policy</li>

    </ul>
    </div>
    </div>
    </div>
    </body>
    </html>

Note:

Please tell me how to load css file in codeigniter. Even if can tell me how load javascripts and images also. I am a beginner in codeigniter MVC Framework.

7 Answers 7

9

First go to application/config/autoload.php. Then add $autoload['helper'] = array('html','url');

<link rel="stylesheet" href="<?php echo base_url('assets/stylesheets/main.css')?>"/>
//OR
<?php echo link_tag('assets/stylesheets/main.css')?>

//Image
<?php echo img('asset/images/learnersway.jpg')?>
//OR
<img src="<?php echo base_url('asset/images/learnersway.jpg')?>" />

//Javascript
<script src='asset/javascript/yourscript.js'></script>

Please visit userguide http://ellislab.com/codeigniter%20/user-guide/helpers/html_helper.html

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

8 Comments

Hi Boss, if i use echo base_uri() showing empty page. if i use echo base_url() showing content without CSS format. Please tell me now what i need to do. NOTE: i am running codeigniter in my local system
Are you sure you loaded url helper? What is your base_url() in config.php file?
ya sure i was loaded helper and base_url showing empty like this $config['base_url']= '';
it is your folder project
I disagree with telling a new user to First go to application/config/autoload.php. Then add $autoload['helper'] = array('html','url'); ... ... ... ... ... ... during learning phase, doing it manually is more meaningful ...
|
4

There are many ways to include css-files, e.g.:

Load this helper: $this->load->helper('url'); and then you can use this in your view:

<link rel="stylesheet" type="text/css" href=“<?php echo base_url(); ?>path/to/css-file">

or

<link rel="stylesheet" type="text/css" href=“<?php echo base_url('path/to/css-file'); ?>">

1 Comment

I did echo base_url('path/to/css-file'); instead
1

In your view do use site_url(); to load css,js and images as:

<link rel="stylesheet" type="text/css" href=“<?php echo site_url();?>assets/stylesheets/main.css”  
<script type="text/javascript" language="javascript" src="<?php echo site_url(); ?>assets/javascript/yourjs.js"></script>

Put this both in head tag and wherever you want to show images use this:

<img title="yourimagetitle"src="<?php echo site_url();?>assets/images/yourimage.extension" height="100" width="200">

Comments

1

use below lines in your template file

<link rel="stylesheet" href="<?php echo $base?>/application/css/style.css" type="text/css">

and load this helper file $this->load->helper('url')

4 Comments

where should i need to load helper file either view or controller. plz tell me.
If i use the above code u mentioned. i am getting following error: A PHP Error was encountered Severity: Notice Message: Undefined variable: base Filename: views/home_learnersway.php Line Number: 9 /application/assets/stylesheets/main.css" type="text/css">
so instead of $base use your base_url() path.
can u tell me how to identify my base_url. Because i am running my source codes in my local system
0

you can use my answer on this question how to load css, js dynamically in codeigniter

    public function index() { 
// View "css_js_view" Page.
 //conditional 
if(your condition){
 $this->data = array( 'css' => site_url()."your css file", //this directory you css 'js' => site_url()."your js file" //this directory you js 
);
 }else{
 $this->data = array( 'css' => site_url()."your css file", //this directory you css 'js' => site_url()."your js file" //this directory you js 
);
 } 
$this->load->view('load_view',$this->data);

}

than on your view

<!--Load css and js--> 
    <link rel="stylesheet" type="text/css" href="<?php echo $css; ?>">
     <script src="<?php echo $js; ?>"></script>

also you can ue this tutorial How to load css,javascript conditionally?

Comments

0

The best practice in codeigniter for set up the assets as below flow:

  • application
  • assets
  • system

But you want the flow as below:

  • application > assets > css > style.css
  • system

So if you want to keep the assets folder inside the application, then you've to do some extra functionality as below: First, make sure that you've loaded the URL helper in your controller which is:

$this->load->helper("url");

you have to first create a helper named "my_helper.php" at "application/helpers" directory with this code:

if ( ! function_exists('asset_url()'))
{
  function asset_url() {

    return base_url().'application/assets/';

  }
}

Now, you have to load this helper into your controller as below:

$this->load->helper("my_helper");

Now replace your .htaccess code at 'application/'' directory with the below code:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|assets|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

After doing all the above functionality, you have to declare your assets in view as below:

<link rel="stylesheet" href="<?php echo asset_url(); ?>css/style.css">

Comments

0

First open config.php file and give base_url like

$config['base_url'] = 'http://localhost/your_file_name/';

after this load helper into your controllers file like this:

$this->load->helper('url');

then goto your view folder and give link like this:

<link rel="stylesheet" href="<?php echo base_url(); ?>assets/stylesheet/main.css">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.