0

If I have multiple inputs that are identical I would like to be able to still insert them into the database with out over writing the first lot.I have gave it a go but no luck. I use codeigniter for my framework.

How am I able to have multiple inputs that are the same but still creates a new row for both on table.

I need the keep same banner id for all though.

IF YOU DOWN VOTE ON QUESTION PLEASE EXPLAIN WHY CHEERS

Model

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

} 

Controller

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

}

}

}

View

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

1 Answers1

0

First thing, you should change the name attribute of title and link to title[] and link[]. This allows sending of multiple values on the same name on form submission.

Then, you have to read all the values separately and process them accordingly. Here is the changed code for your model:

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();
}

} 

Here is a reference to processing array values received in "POST" in codeigniter: Getting data from post array in CodeIgniter

Community
  • 1
  • 1
Rohit Aggarwal
  • 650
  • 7
  • 18