Return name by id in view. PHP and Codeigniter

Asked

Viewed 905 times

0

Hello!

In the controller I have the following:

public function index() {
    $data['demandas'] = $this->demandas_model->get_demandas();

    $data['main_view'] = 'demandas/index';
    $this->load->view('layouts/main', $data);
}

In the model in function get_demandas I have the following:

public function get_demandas() {
    $query = $this->db->get('demandas');
    return $query->result();
}

And in the view I display as follows:

    <?php foreach($demandas as $demanda): ?>
                    <tr>
    <?php echo "<td><a href='". base_url()."index.php/demandas/edit_view/".           $demanda->da_id ."'>".$demanda->da_id."</a></td>" ?>
        <?php echo "<td>".$demanda->us_id."</td>" ?>
        <?php echo "<td>".$demanda->da_descricao."</td>" ?>
        <?php echo "<td>".$demanda->da_data."</td>" ?>
       // mais código

Where it is written $demanda->us_id, instead of displaying the code, I need to display the user name.

In the model, in the function get_demandas, return me the id and the nome user need to return through another call from another model and put in the $data['demandas'], but I don’t know how to put in the view, through the function index of controller.

This is the function which returns the user data, from model:

public function get_usuario($id) {
    $this->db->where('us_id', $id);
    $query = $this->db->get('usuarios');
    return $query->result();
}

Thank you

  • $demanda->nome?

  • I forgot to put another detail, id is a foreign key and through it I need to return the name of another query

  • Make a Join there.

  • Right, this Join I do in get_demands()? if it were in sql I would, but I’m using codeigniter and getting a little confused

1 answer

2


I believe the solution is to make a JOIN:

$this->db->select('*');
$this->db->from('demandas');
$this->db->join('outra_tabela', 'outra_tabela.campo_chave = demandas.id');
$result = $this->db->get();

It’s been a long time since I’ve used Codeigniter. Maybe it’s worth looking at documentation

  • thanks! I will see the documentation

  • +1 for not using the IC, it is good practice :D

  • not using the IC is good practice?

  • @Newbie was a joke, however there are better options than CI.

  • Which recommends me?

  • 1

    Laravel, Symfony, Lumen, Silex. Have some good options.

Show 1 more comment

Browser other questions tagged

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