php - How can I include css and js file in codeigniter -
i trying toinclude css file in following code.
config :
$config['base_url'] = 'http://localhost/asoft/projects/ci_search'; $config['site_url'] = 'http://localhost/asoft/projects/ci_search/index.php'; $config['js'] = 'assets/js';
view:
<link rel="stylesheet" href="<?php echo $base?>/<?php echo $css?>/style.css"> <link rel="stylesheet" href="<?php echo $base?>/<?php echo $css?>/bootstrap.min.css"> <script src="<?php echo $base?>/<?php echo $js?>/jquery.min.js"></script> <script src="<?php echo $base?>/<?php echo $js?>/jquery.js"></script> <script src="<?php echo $base?>/<?php echo $js?>/bootstrap.min.js"></script>
i put css file in ci_search/css , js file in ci_search/assets/js got error undefined variable css , undefined variable js.please provide solution problem.
update
controller:
public function index() { $this->data = array( 'site' => $this->config->item('site_url'), 'base' => $this->config->item('base_url'), 'css' => $this->config->item('css'), 'js' => $this->config->item('js'), 'image'=>$this->config->item('image') ); $data = $this->data; $data['error'] = ''; $this->load->view('index',$data); }
as far can see, din't declared $config['css'] in config file, it's normal undefined variable error css. shouldn't have problem js.
when declaring base_url use trailing slash @ end (eg. "http://localhost/fancysite/")
also can use ci's url helper use functions base_url() or site_url() , many more. (as @likee suggested).
config.php
// should first variable in ci's config.php $config['base_url'] = "http://localhost/asoft/projects/ci_search/"; // many more lines other configuration variables // .................................................. // .................................................. // .................................................. // own configuration variables @ end of file $config['css'] = 'css/'; $config['js'] = 'assets/css/'; $config['image'] = 'images/';
controller
public function index() { // loading url helper use base_url() function in view, // if load helper in autoload.php don't need load here again $this->load->helper('url'); // if didn't declare data class property // can use // $data = array( // 'css' => $this->config->item('css'), // 'js' => $this->config->item('js'), // 'image'=>$this->config->item('image') // ); $this->data = array( 'css' => $this->config->item('css'), 'js' => $this->config->item('js'), 'image'=>$this->config->item('image') ); // don't need line below // $data = $this->data; $this->data['error'] = ''; $this->load->view('index',$this->data); }
view
<link rel="stylesheet" href="<?php echo base_url($css . 'style.css'); ?>"> <link rel="stylesheet" href="<?php echo base_url($css . 'bootstrap.min.css'); ?>"> <script src="<?php echo base_url($js . 'jquery.min.js'); ?>"></script> <!-- need include jquery.js while included jquery.min.js above? here go :) --> <script src="<?php echo base_url($js . 'jquery.js'); ?>"></script> <script src="<?php echo base_url($js . 'bootstrap.min.js'); ?>"></script>
probably need use url helper lot. can autoload in autoload.php file in config folder. must somewhere around line 90
$autoload['helper'] = array('url');
Comments
Post a Comment