How to create dropdown select from db with Codeigniter

  • Your Database Structure
            Cteate table as tb_category:

                 - category_id
                 - category_name

  • Controller
 class category extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('category_model');
      
        // Load session library
        $this->load->library('session');  
  
    }

    public function index()
    {
        $data['value'] = $this->category_model->get_data();      

        $this->load->view('category',$data ); 
    }
}
  • Model
  
class category_model extends CI_Model {
  
public function __construct()
    {
        parent::__construct();
    }

public function get_data_national()
    {
        //Select data and query from table
        $this->db->select('*');
        $this->db->from('tb_category');
        $query = $this->db->get();
        $result = $query->result();

        //store data into array format
        $id = array('');
        $name = array('...Select Category...');
       
        //Loop data from table
        for ($i = 0; $i < count($result); $i++)
        {
            array_push($id, $result[$i]->id);
            array_push($name, $result[$i]->nationality);
        }

        //Loading data into value and name for select box
        return $getData = array_combine($id,$name);
    }
}

  • View
    <?php
         $attributes = 'id="levels" style="width: 315px;" class="chzn-select"'; // select box style
        echo form_dropdown('category', $value, set_value('category'),$attributes);
?>


      

How to upload file with ci.


Before you coding with ci you have to create a direct folder to uploads store file. in ci root directory.


View :

<?php 
 echo form_open_multipart('items/add_new_item',array('name'=>'sentMessage','id'=>'contactForm')); ?>
    <div class="row">
        <div class="col-md-12">
            <div class="form-group" style="color: #fff;">
                   <input  name="image" type="file">                                        
           </div>
        </div>
        <div class="clearfix"></div>
        <div class="col-lg-12 text-center">
            <div id="success"></div>
            <button type="submit" class="btn btn-success">Save</button>
            <button type="submit" class="btn btn-danger">Cancel</button>
        </div>
    </div>
</form>

Controller :

class items extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
       
        $this->load->model('items_model');
    }
 
    public function add_new_item()
    {
        //set preferences
        $config['upload_path'] = './uploads/'; // folder name
        $config['allowed_types'] = '*';
        $config['max_size'] = '10000000';
       
        //load library of file upload
        $this->load->library('upload', $config);
       
        //copy file into folder
        if(!$this->upload->do_upload('image')) // input type name
        {
            $file = "";
           
        }else{
           
            $upload_data = $this->upload->data();
            $file = $upload_data['file_name'];
           
        }
       
        $data = array(
             'image' => $file
        );
       
        //Save to db
        $this->items_model->add_new_item($data);
       
        redirect('items');

    }
 }
Model:

    public function add_new_item($data)
    {
        return $this->db->insert('tb_items', $data);
    }

How to insert one id into multi table with Codeigniter.

 

My SQL Database

Table Structure of tb_employee
            employee_id
            name
            phone
           ...........

Table Structure of tb_permission
         employee_id
         sales
         customers
         ................

Table Structure of tb_user
         employee_id
         user_name
         password
         ...............

Now let start coding:

Controler:

    public function add_employee()
    {
        $data['employees_data']=$this->employee_model->insert_employee();
        $latest_id=$this->employee_model->get_latest_id();
        $data['permissions_data']=$this->employee_model->insert_permission($latest_id);
        $data['users_data']=$this->employee_model->insert_user($latest_id);
   
        redirect('employees');
    }   

Model:

    function insert_employee()
    {
        //set preferences
        $config['upload_path'] = './uploads/users/';
        $config['allowed_types'] = '*';
        $config['max_size'] = '10000000';
       
        //load library of file upload
        $this->load->library('upload', $config);
       
        //copy file into folder
        if(!$this->upload->do_upload('files'))
        {
            $file = "";
           
        }else{
           
            $upload_data = $this->upload->data();
            $file = $upload_data['file_name'];
           
        }
       
        $data=array(
            'name' => $this->input->post('name'),
            'phone' => $this->input->post('phone'),
            'email' => $this->input->post('email'),
            'address' => $this->input->post('address'),
            'files' =>  $file,
            'message' => $this->input->post('message')
        );
        $this->db->insert('tb_employee',$data);
        //better return true on success
    }

    public function get_latest_id()
    {
        $sql=$this->db->query("SELECT MAX(employee_id) as id FROM tb_employee");
        return $sql->row_array();
    }
   
    function insert_permission($emp_id)
    {
        $data=array(
            'employee_id' => $emp_id['id'],
            'items' => $this->input->post('items'),
            'sales' => $this->input->post('sales'),
            'employees' =>   $this->input->post('employees'),
            'customers' =>   $this->input->post('customers'),
            'suppliers' =>   $this->input->post('suppliers'),
            'receivings' =>   $this->input->post('receivings'),
            'reports' =>   $this->input->post('reports'),
            'config' =>   $this->input->post('configuration')
        );
        $this->db->insert('tb_permissions',$data);
    }

  function insert_user($emp_id)
    {
        $data=array(
            'employee_id' => $emp_id['id'],
            'user_name' => $this->input->post('user_name'),
            'password' => $this->input->post('password')
        );
        $this->db->insert('tb_users',$data);
    }

Newest questions tagged codeigniter - Stack Overflow