我尝试了以下模型视图控制器,但我需要一次提交多行:请任何人帮我如何实现这一目标?
这是我的控制器:
function add_item(){
$this->form_validation->set_rules('item_name', 'Item Name', 'trim|required');
if ($this->form_validation->run() == FALSE)
{
$this->load->model('mdl_item');
$data['main_content'] = 'backend/items/add_item';
$data['title'] = 'Create item';
$this->load->view('includes/template', $data);
}
else
{
$this->load->model('mdl_item');
$data = $this->input->post();
$this->mdl_item->create_item($data);
$this->session->set_flashdata('message', 'Items successfully created');
redirect('admin/items', 'refresh');
}
}
这是我的模特:
function create_item($data)
{
$data['expiry_date'] = date('Y-m-d', strtotime(element('expiry_date', $data)));
$crop_data = elements(array(
'item_name',
), $data);
$add_item = $this->db->insert_string('items', $crop_data);
$this->db->query($add_item);
}
这是我的观点:
<?php echo form_open('admin/items/add_item', 'id="item_form_validate"'); ?>
<div class="col-sm-12 col-md-12 jumbotron">
<input type="text" class="form-control" name="item_name[]" value="<?php echo set_value('item_name'); ?>">
<input type="text" class="form-control" name="item_name[]" value="<?php echo set_value('item_name'); ?>">
<input type="text" class="form-control" name="item_name[]" value="<?php echo set_value('item_name'); ?>">
<button type="submit" class="btn btn-success" value="submit"><span class="icon-checkmark"></span> <?php echo lang('Submit'); ?></button>
</form>
更改控制器中的代码set_rules('item_name',
...
function add_item(){
$this->form_validation->set_rules('item_name', 'Item Name', 'trim|required');
到这个set_rules('item_name[]'
...
function add_item(){
$this->form_validation->set_rules('item_name[]', 'Item Name', 'required');
传递后修剪值