0

我需要在 Codeigniter 的复选框中插入多个值。我怎么能在控制器中写这个。我尝试了很多,但值没有插入。

查看.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>

模型

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

控制器

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');

    }

}
4

1 回答 1

0

看法:-

您必须array像这样执行所有名称复选框。

<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>

控制器:-

       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');

    }

}

型号代码:-

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

注意:-有关更多信息implode()

implode()函数从数组的元素中返回一个字符串。

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

于 2020-11-24T06:18:25.493 回答