如果我有多个相同的输入,我希望能够将它们插入到数据库中而不用写第一批。我已经给了它一个但没有运气。我使用codeigniter作为我的框架。
我如何能够拥有相同的多个输入,但仍然会在桌面上创建新行。
我需要为所有人保留相同的横幅ID。
如果您在问题上投票,请解释为何选择
模型
public function add_banners_image($banner_id = 0) {
$data = array(
'banner_id' => $banner_id,
'link' => $this->input->post('link'),
);
$this->db->set($data);
$this->db->insert_id();
$this->db->insert($this->db->dbprefix . 'banner_image');
//return $this->db->insert_id();
}
控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Banner_add extends Admin_Controller {
public function __construct() {
parent::__construct();
$this->load->model('admin/design/model_add_banners');
}
public function index() {
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Banner Name');
$this->form_validation->set_rules('status', 'Banner Status');
if ($this->form_validation->run($this) == FALSE) {
return $this->load->view('design/banner_form.tpl', $data);
} else {
$banner_id = $this->model_add_banners->add_banners();
$banner_image_id = $this->model_add_banners->add_banners_image($banner_id);
$this->model_add_banners->admin_banners_banner_image_description($banner_image_id, $banner_id);
redirect('admin/design/banners');
}
}
}
查看
<?php echo Modules::run('admin/common/header/index');?><?php echo Modules::run('admin/common/column_left/index');?>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?php echo $this->load->view('flashdata/flashdata.tpl');?>
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<?php echo form_open_multipart('admin/design/banners/add', array('id' => 'banner_add', 'class' => 'form-horizontal'));?>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-banner-name">Banner Name</label>
<div class="col-sm-10">
<input type="text" name="name" value="" placeholder="Banner Name" id="input-banner-name" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-status">Banner Status</label>
<div class="col-sm-10">
<select name="status" id="input-status" class="form-control">
<option value="1">Enable</option>
<option value="0">Disabled</option>
</select>
</div>
</div>
<table id="banners" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td class="text-center">Title</td>
<td class="text-center">Url Link</td>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" name="title" value="" placeholder="Banner Title" id="" class="form-control" />
</td>
<td>
<input type="text" name="link" value="" placeholder="Banner Link" id="" class="form-control" />
</td>
</tr>
<tr>
<td>
<input type="text" name="title" value="" placeholder="Banner Title" id="" class="form-control" />
</td>
<td>
<input type="text" name="link" value="" placeholder="Banner Link" id="" class="form-control" />
</td>
</tr>
</tbody>
</table>
<?php echo form_close();?>
</div>
</div>
<?php echo Modules::run('admin/common/footer/index');?>
答案 0 :(得分:0)
首先,您应该更改标题的名称属性,并链接到title[]
和link[]
。这允许在表单提交时在同一名称上发送多个值。
然后,您必须分别读取所有值并相应地处理它们。以下是您模型的更改代码:
public function add_banners_image($banner_id = 0) {
$links = $this->input->post('link');
foreach ($links as $link) {
$data = array(
'banner_id' => $banner_id,
'link' => $link,
);
$this->db->set($data);
$this->db->insert_id();
$this->db->insert($this->db->dbprefix . 'banner_image');
//return $this->db->insert_id();
}
}
以下是处理codeigniter中“POST”中收到的数组值的引用: Getting data from post array in CodeIgniter