Pentru generarea mai multor înregistrări dintr-un multiselect se poate folosi varianta următoare ce foloseşte CodeIgniter şi un model CRUD. Exemplul preia mai multe servicii cu tot cu tarife.
Models
class Orders_model extends Crud_model {
public $table = 'orders';
public function add($data)
{
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
}
class Services_model extends Crud_model {
public $table = 'services';
public function get_service()
{
$query = $this->db->get($this->table);
$data =array();
foreach($query->result_array() as $row){
$data[$row['service_id']] = $row['service_name'];
}
return $data;
}
public function get_value($service_details)
{
$this->db->select('value');
$this->db->where('service_id', $service_details);
$query = $this->db->get($this->table);
return $query->row()->value;
}
}
View
<?php echo form_open($form_action); ?>
<fieldset>
<?php echo form_multiselect('service[]', $services, $this->input->post('service')); ?>
</fieldset>
<?php echo form_close(); ?>
Controller
public function add()
{
$this->form_validation->set_rules('service', 'Serviciu', 'required');
$this->form_validation->set_rules('price', 'Tarif', 'numeric');
if ($this->form_validation->run() == TRUE)
{
foreach ($this->input->post('service') as $service_details) {
$order = array(
'client_id' => $this->input->post('client'),
'service_id' => $service_details,
'price' => $this->services_model->get_value($service_details),
);
$this->orders_model->add($order);
}
redirect('nume_controller');
}
$this->data['form_action'] = 'nume_controller/add/';
$this->data['services'] = $this->services_model->get_service();
$this->layout->render('nume_controller/add', $this->data);
}