Search code examples
phpcodeignitermodel-view-controllercontrollercodeigniter-query-builder

How insert Multiple checkbox value in codeigniter


I need to insert multiple values in the checkbox in Codeigniter. how I can write this in the controller. I tried so many but the values not inserting .

View.Php

<div class="custom-file">
 <input type="checkbox" name="size[]" value="Medium"> M 
<input type="checkbox" name="size[]" value="Large">L
 <input type="checkbox" name="size[]" value="XL"> XL
</div>

Model

public function product_insert($data)
{
    // grab user input
    $this->db->insert('product', $data);
    return $this->db->insert_id();
}

Controller

public function newProduct()
{
    $data   = array();
    $this->load->library('form_validation');
    $this->load->library('image_lib');
    $this->load->helper('file');
    $this->load->helper('string');
    // Load the model
    $this->load->model('admin/product_model');
    $this->load->model('admin/category_model');

    $data['category']   = $this->category_model->active_category_listing();
   // $data['brand']        = $this->product_model->brand_listing();
   
    $this->form_validation->set_error_delimiters('<div class="alert alert-danger">', '</div>');

    // Validating  Field
    $this->form_validation->set_rules('product_title', 'Title', 'required');
    if ($this->form_validation->run() == false) {
        $this->load->view('admin/add_product', $data);
    } 
         //insert size checkboxes into database.
         $sizeArray = $this->input->post('size');  // Array
       
         $sizeString = implode(",", $sizeArray);    // String
    else {
        $config['upload_path'] = 'uploads/product_images/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = 10024;
        $this->load->library('upload', $config);
        // upload file to directory
        $uploadedFile ='';
        if ($this->upload->do_upload('product_img')) {
            $uploadData = $this->upload->data();
            $uploadedFile = $uploadData['file_name'];
            $config['image_library'] = 'gd2';
            $config['source_image'] = $uploadData['full_path'];
            $config['new_image'] = 'uploads/product_images/thumb';
            $config['create_thumb'] = false;
            $config['maintain_ratio'] = TRUE;
            $config['width']         = 311;
            $config['height']       = 415;
            $this->image_lib->initialize($config);
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();
            
            $data['success_msg'] = 'File has been uploaded successfully.';
        } else {
            $data['error_msg'] = $this->upload->display_errors();
        }
        // Setting values for tabel columns
        $data = array(
            'product_title' => $this->input->post('product_title'),
            'product_description' => $this->input->post('product_description'),
            'product_price' => $this->input->post('product_price'),
            'product_discount' => $this->input->post('product_discount'),
            'product_quantity' => $this->input->post('product_quantity'),
            'sub_category_id' => $this->input->post('sub_category_id'),
            'category_id' => $this->input->post('category_id'),
            'product_status' => $this->input->post('product_status'),
            'product_img' => $uploadedFile,
            'product_key'   => random_string('alnum',8),
            'product_cuttingprice'   => $this->input->post('product_cuttingprice'),
            'size'=>$sizeString);

        );
        // Transfering data to Model
        $prdID  = $this->product_model->product_insert($data);
        $upImg      = $this->uploadAddtnlImages($prdID);    
        $this->session->set_flashdata('msg', 'Product Added Successfully');
        redirect('admin/product/listProduct/', 'refresh');

    }

}

Solution

  • View:-

    You must do all name checkbox as an array like this.

    <div class="custom-file">
     <input type="checkbox" name="size[]" value="Medium"> M 
    <input type="checkbox" name="size[]" value="Large">L
     <input type="checkbox" name="size[]" value="XL"> XL
    </div>
    

    Controller:-

           public function newProduct()
    {
        $data   = array();
        $this->load->library('form_validation');
        $this->load->library('image_lib');
        $this->load->helper('file');
        $this->load->helper('string');
        // Load the model
        $this->load->model('admin/product_model');
        $this->load->model('admin/category_model');
    
        $data['category']   = $this->category_model->active_category_listing();
       // $data['brand']    = $this->product_model->brand_listing();
       
        $this->form_validation->set_error_delimiters('<div class="alert alert-danger">','</div>');
    
        // Validating  Field
        $this->form_validation->set_rules('product_title', 'Title', 'required');
        if ($this->form_validation->run() == false) {
            $this->load->view('admin/add_product', $data);
        } 
              else {
            $config['upload_path'] = 'uploads/product_images/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $config['max_size'] = 10024;
            $this->load->library('upload', $config);
            // upload file to directory
            $uploadedFile ='';
            if ($this->upload->do_upload('product_img')) {
                $uploadData = $this->upload->data();
                $uploadedFile = $uploadData['file_name'];
                $config['image_library'] = 'gd2';
                $config['source_image'] = $uploadData['full_path'];
                $config['new_image'] = 'uploads/product_images/thumb';
                $config['create_thumb'] = false;
                $config['maintain_ratio'] = TRUE;
                $config['width']         = 311;
                $config['height']       = 415;
                $this->image_lib->initialize($config);
                $this->load->library('image_lib', $config);
                $this->image_lib->resize();
                
                $data['success_msg'] = 'File has been uploaded successfully.';
            } else {
                $data['error_msg'] = $this->upload->display_errors();
            }
            
             //insert size checkboxes into database.
             $sizeArray = $this->input->post('size');  // Array
           
             $sizeString = implode(",", $sizeArray);    // String
      
            // Setting values for tabel columns
            $data = array(
                'product_title' => $this->input->post('product_title'),
                'product_description' => $this->input->post('product_description'),
                'product_price' => $this->input->post('product_price'),
                'product_discount' => $this->input->post('product_discount'),
                'product_quantity' => $this->input->post('product_quantity'),
                'sub_category_id' => $this->input->post('sub_category_id'),
                'category_id' => $this->input->post('category_id'),
                'product_status' => $this->input->post('product_status'),
                'product_img' => $uploadedFile,
                'product_key'   => random_string('alnum',8),
                'product_cuttingprice'   => $this->input->post('product_cuttingprice'),
                'size'=>$sizeString);
    
            );
            // Transfering data to Model
            $prdID  = $this->product_model->product_insert($data);
            $upImg      = $this->uploadAddtnlImages($prdID);    
            $this->session->set_flashdata('msg', 'Product Added Successfully');
            redirect('admin/product/listProduct/', 'refresh');
    
        }
    
    }
    

    Model Code:-

    public function product_insert($data)
    {
        // grab user input
        $this->db->insert('product', $data);
        return $this->db->insert_id();
    }
    

    Note:- For More info regarding implode().

    The implode() function returns a string from the elements of an array.

    https://www.php.net/manual/en/function.implode.php