我需要在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');
}
}
视图:-
您必须像这样将所有名称复选框作为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