Showing posts with label How to upload image in CodeIgniter?. Show all posts
Showing posts with label How to upload image in CodeIgniter?. Show all posts

Friday, 10 January 2014

How to Upload images in codeignitor and save file path in database ?



Here i am using twitter bootstrap theme  

Controller to open form name dashboard.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Dashboard extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->model('newcategory');
}
public function category()
{
$data['category'] = $this->newcategory->showcategorydetails();
$this->load->view('template/header',$data);
$this->load->view('template/sidebar');
$this->load->view('category', array('error' => ' ' ));
$this->load->view('template/footer');
}
}

?>




In view- file name category.php

<div id="content">

<div class="outer">

<div class="inner">
<div class="row">
  <div class="col-lg-12">
    <div class="box">
      <header>
        <div class="icons"> <i class="fa fa-table"></i> </div>
        <h5>Dynamic Table</h5>
      </header>
      <div id="collapse4" class="body">
        <table id="dataTable" class="table table-bordered table-condensed table-hover table-striped">
          <thead>
            <tr>
              <th>Image</th>
              <th>Name</th>
              <th>Description</th>
              <th>Slug</th>
              <th>Posts</th>
              <th>Parent</th>
              <th>Status</th>
              <th>Action</th>
            </tr>
          </thead>
          <tbody>
          <?php foreach ($category as $cat) { ?>
            <tr>
              <td><img src="<?php echo base_url(); ?>uploads/<?php echo $cat['img_name']; ?>" class="category-image" alt="category-image"></td>
              <td><?php echo $cat['name']; ?></td>
              <td><?php echo $cat['description']; ?></td>
              <td><?php echo $cat['slug']; ?></td>
              <td>Empty</td>
              <td>
 <?php if($cat['parent']=='0')
 {
echo '-N/A-';
 } else {
 
 $query=$this->db->query("select parent_cat_name from app_category_parent where id=".$cat['parent']);
 $cat_name = $query->result_array();
 foreach ($cat_name as $category) {
 echo $category['parent_cat_name'];
 }}
 ?></td>
              <td><?php if($cat['status']=='1'){ echo 'Active';} else { echo 'Inactive'; } ?></td>
              <td>            
              <a href="<?php echo base_url(); ?>index.php/newentry/deactivate_Category/<?php echo $cat['id']; ?>" class="customClick btn btn-warning btn-xs btn-line" data-str="0">De-activate</a>
              <a href="<?php echo base_url(); ?>index.php/newentry/delete_Category/<?php echo $cat['id']; ?>" class="deleteClick btn btn-danger btn-xs btn-line" data-str="0">Delete</a>
              </td>
            </tr>
          <?php } ?>
          </tbody>
        </table>      
      </div>
    </div>
  </div>
</div>
<div class="row">
  <div class="col-lg-6">
    <div class="box dark">
      <header>
        <div class="icons">
          <h5>Add New Category</h5>
          <div class="toolbar">
            <ul class="nav">
              <li> <a class="minimize-box" href="#div-1" data-toggle="collapse"> <i class="fa fa-chevron-up"></i> </a> </li>
            </ul>
          </div>
        </div>
      </header>
      <div id="div-1" class="accordion-body collapse in body">
        <?php echo form_open_multipart('newentry/newcategory') ?>

// for saving data from this form into database-newentry is my controller and newcategory is the function.


          <div class="form-group">
            <label class="control-label col-lg-4" for="text1">Name</label>
            <div class="col-lg-8">
              <input id="cat_title" class="form-control" name="cat_title" type="text" required="required" onblur="getslug();">
              <p>The name is how it appears on your site.</p>
            </div>
          </div>
          <div class="form-group">
            <label class="control-label col-lg-4" for="text1">Slug</label>
            <div class="col-lg-8">
              <input id="cat_slug" class="form-control" name="cat_slug" type="text" required="required">
              <p>The “slug” is the URL-friendly version of the name. It is usually all lowercase and contains only letters, numbers, and hyphens.</p>
            </div>
          </div>
          <div class="form-group">
            <label class="control-label col-lg-4">Parent</label>
            <div class="col-lg-8">
              <select name="cat_parent" onchange="selected();" id="cat_parent" data-placeholder="Choose a Country..." class="form-control chzn-select" tabindex="2">
              <option value="">None</option>
              <?
 $query=$this->db->query("select app_category.*,app_category_detail.* FROM app_category LEFT JOIN app_category_detail ON app_category.cat_id=app_category_detail.cat_id where status=1");
 $cat_name = $query->result_array();
 foreach ($cat_name as $category) { ?>
           
 <option value="<?php echo $category['cat_id'];?>"><?php echo $category['name'];?></option>
           
 <? } ?>            
              </select>            
              <input type="hidden" id="cat_parent_id" name="cat_parent_id" />
              <p>Categories, unlike tags, can have a hierarchy. You might have a Jazz category, and under that have children categories for Bebop and Big Band. Totally optional.</p>
            </div>
          </div>
          <div class="form-group">
            <label class="control-label col-lg-4" for="autosize">Description</label>
            <div class="col-lg-8">
              <textarea id="autosize" name="cat_description" class="form-control" style="overflow: hidden; word-wrap: break-word; resize: horizontal; height: 134px;"></textarea>
              <p>The description is not prominent by default; however, some themes may show it.</p>
            </div>
          </div>  
       
 <div class="form-group">
                        <label class="control-label col-lg-4">Category Image</label>
                        <div class="col-lg-8">
                          <div class="fileinput fileinput-new" data-provides="fileinput">
                            <div class="fileinput-new thumbnail" style="width: 200px; height: 150px;">
                              <img data-src="holder.js/100%x100%" alt="...">
                            </div>
                            <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;"></div>
                            <div>
                              <span class="btn btn-default btn-file"><span class="fileinput-new">Select image</span><span class="fileinput-exists">Change</span>
                              <input type="file" name="userfile" size="20" />
                              </span>
                              <a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput">Remove</a>
                            </div>
                          </div>
                        </div>
                      </div>
           
 
          <p class="submit">
            <input id="submit" class="btn btn-primary" type="submit" value="Add New Category" name="submit">
          </p>
        </form>
     
      </div>
    </div>
  </div>
</div>
</div>
</div>
</div>
<script>
function getslug() {
var str = document.getElementById('cat_title').value;
var res = str.replace(/\s+/g, '-').toLowerCase(); document.getElementById('cat_slug').value = res; }
</script>
<script>
function selected()
{
var id = document.getElementById('cat_parent').value; document.getElementById('cat_parent_id').value=id;
}
</script>






In My controller - name - newentry.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Newentry extends CI_Controller {


public function __construct()

{

parent::__construct();

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

$this->load->model('newcategory');

}

public function newcategory()

{

$this->newcategory->newcategory_partone();

$id = $this->db->insert_id();

$this->newcategory->newcategory_parttwo($id);

$cid=$this->input->post('cat_parent_id');

if ($cid !='') {

$this->newcategory->newcategory_partthree();

}

redirect('dashboard/category', 'refresh');

}

public function deactivate_Category(){

$id=$this->uri->segment(3);

$this->newcategory->deactivate_category($id);

redirect('dashboard/category', 'refresh');

}

public function delete_Category(){

$id=$this->uri->segment(3);

$this->newcategory->delete_category($id);

redirect('dashboard/category', 'refresh');

}

}






And in my model - name- newcategory.php



<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Newcategory extends CI_Controller {

public function __construct()
{
parent::__construct();
}
public function lastregistration_id()
{
return $this->db->insert_id();
public function newcategory_partone()
{
$this->load->helper('url');
$this->load->helper('form');
$this->load->helper('date');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width']  = '1024';
$config['max_height']  = '768';

$this->load->library('upload', $config);

if ( ! $this->upload->do_upload())
{
redirect('dashboard/category', 'refresh');
}
else
{
$data = array('upload_data' => $this->upload->data());
            foreach ($data as $item){
$img_path= $item['full_path']; 
$img_name= $item['file_name']; 
}
$cat = array(
'name' => $this->input->post('cat_title'),
'slug'=>$this->input->post('cat_slug'),
'date_created' => date('Y-m-d H:i:s'),
'system_ip'=> $_SERVER['REMOTE_ADDR'],
'img_name' =>$img_name,
'img_path'=>$img_path,
);
return $this->db->insert('app_category', $cat);
}
}
public function newcategory_parttwo($cat_id)
{
$this->load->helper('url');
$this->load->helper('date');
$cat_second = array(
    'cat_id' =>$cat_id,
'name'=>$this->input->post('cat_title'),
'description'=>$this->input->post('cat_description'),
'parent'=>$this->input->post('cat_parent'),
);
return $this->db->insert('app_category_detail', $cat_second);
}
public function newcategory_partthree()
{
$this->load->helper('url');
$this->load->helper('date');
$id=$this->input->post('cat_parent_id');
$query=$this->db->query("select * from app_category where cat_id=$id");
$cat_name = $query->result_array();
foreach ($cat_name as $category) { 
        $name=$category['name'];
        }
$cat_third = array(
   
'parent_cat_name'=>$name,
'status' =>1,
);
return $this->db->insert('app_category_parent', $cat_third);
}
public function showcategorydetails()
{
$query=$this->db->query("select app_category.*,app_category_detail.* FROM app_category LEFT JOIN app_category_detail ON app_category.cat_id=app_category_detail.cat_id where status=1");  
    return $query->result_array();
}
public function deactivate_category() {
$id=$this->uri->segment(3);
$data = array(
              'status '=>0
            );

$this->db->where('id', $id);
$this->db->update('app_category_detail', $data); 
}
public function delete_category() {
$id=$this->uri->segment(3);
$data = array(
             'status '=>2 
            );

$this->db->where('id', $id);
$this->db->update('app_category_detail', $data); 
}
}


That's it.Enjoy.