How to receive id’s in the controller/model

Asked

Viewed 463 times

1

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

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

So for each participant in an event I have a relation 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>

Code of the Event Controller:

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

	}
}

Code of the Model Events:

<?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;
	}
}

  • 2

    Edit the question and add the controller codes and database templates. Does Codeigniter have native support for model relations? In fact, as it is new here, I suggest you do the [tour] to learn the basics of how the site works. It is always useful.

  • 1

    I assume you’re sending this data through a form in your view, and if so, are you able to get that data from the controller? not only enter the codes of your controller and model, but also your view tbm.

  • @Marcelodiniz already added the codes. They are basic, I’m learning now PHP and Codeigniter.

  • @Andersoncarloswoss already added.

1 answer

0

So in order to be sent multiple selected items you should put as an array like this:

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

I imagine you’re working with routes, not to know, but in your controller’s way that you’re getting this data, I imagine it’s the adicionarParticipanteEvento you must see to it that he receives it thus:

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 that I give and that I don’t think is right what you’re doing is calling these views in __Construct (it has nothing to do with your problem, but only a hint).

  • Marcelo, I was able to receive the ids the way you wrote. Thank you! Without wanting to abuse, you could tell me how to call the view(header and footer) without being by __Construct.

  • And another thing, how do, for example, to receive the data (Cpf, email, name) of each of the users whose Ids I received by form. I created a function in Userssmodel public function dadosUsuario($id_usuario){&#xA; $this->db->where('id_usuario', $id_usuario);&#xA; $usuario = $this->db->get('usuarios')->result_array();&#xA; return $usuario;&#xA; }&#xA;} How would pro controller pass each of the Ids separately to the function and how to receive the isolated information from each of the users.

  • then Gabriel, when the call of the views pq I think wrong to leave in __Construct is that every time any controller method is called will always run the constructor of the class, basic principles ok. You can put these views in the ways they need it, or even create a template for it. E to pass the user id to the model, one way you can do this is by a loop (foreach)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.