How to receive id's in the controller / model

1

I'm creating a project and I ran into a problem. The idea is to add participants to a specific event. I've created everything right: controllers, models and views.
My question is how do I get the controller / model to receive the id's of each participant I selected in a multi-select view adicionar_participante .

I have a table (attendees_events) whose columns are: id_relação , id_usuario , id_evento .

So for each participant in an event I have a relationship in this table.

VIEW Code:

<h1>Adicionar participantes</h1>

<form action="adicionar-participante-evento" method="post">
  <select multiple class="form-control" name="id_usuario">
	<?php foreach ($usuarios as $usuario) { ?>
	  <option value="<?= $usuario['id_usuario']; ?>"><?= $usuario['nome_usuario']; ?></option>
	<?php
	}
	?>
	</select>
  <button class="btn btn-default" type="submit">Adicionar</button>
</form>

Event Controller Code:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class EventoController extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->view('template/cabecalho');
		$this->load->view('template/footer');
	}

	public function index() {
		$eventos = $this->EventosModel->listarTodos();
		$dadosEventos = array('eventos' => $eventos ); 
		$this->load->view('eventos/index', $dadosEventos);
	}

	public function adicionarParticipante(){
		$usuarios = $this->UsuariosModel->listarTodos();
		$dadosUsuarios = array('usuarios' => $usuarios);
		$this->load->view('eventos/adicionar_participante', $dadosUsuarios);
	}

	public function adicionarParticipanteEvento(){
		$id_usuarios_participantes = array(
			'' => '';
		);

	}
}

Event Model Code:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class EventosModel extends CI_Model {

	public function listarTodos(){
		$eventos = $this->db->get('eventos')->result_array();
		return $eventos;
	}
}
    
asked by anonymous 16.07.2017 / 18:12

1 answer

0

So, to be able to send several selected items you should put them like an array like this:

<select multiple class="form-control" name="id_usuario[]">

I imagine that you are working with routes, not to know, but in your controller in your method that is receiving this data, I imagine that it is the adicionarParticipanteEvento should make you receive it like this:

public function adicionarParticipanteEvento(){

    $id_usuarios_participantes = $this->input->post('id_usuario');

    // somente para ver se esta vindo certo e depois trabalhar com esses dados
    var_dump($id_usuarios_participantes); 
}

And a tip I give and that I do not think is correct is what to do is call those views in __construct (it has nothing to do with your problem, but just a hint anyway).

    
17.07.2017 / 14:12