Return name by id in view. PHP and Codeigniter

0

Hello

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

No model in function get_demandas I have the following:

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

And in view it displays 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 $demanda->us_id is written, instead of displaying the code, I need to display the user name.

In the model, in the get_demandas function, I get the id and the nome of the user to return through another call of another model and put in $data['demandas'] , but I do not know how to put it in the view, through of function index of controller .

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

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

Thank you

    
asked by anonymous 23.06.2016 / 17:02

1 answer

2

I think 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. It might be worth looking at the documentation

    
23.06.2016 / 17:08