Linking tables with Code Igniter

0

I have the following problem, I have a customer table that stores the personal data, login, password among others, and another invoice table, which has all the invoices of all the clients, however I need to search the data of the customer with a user ID stored in the invoice table, how do I do this using Code Igniter?

Here is part of the code that searches for invoices:

   $query = $this->admin->get_all('faturas')->result();
            foreach ($query as $linha) {
                echo '<tr>';
                printf('<td>%s %s</td>', $linha->nome, $linha->sobrenome);
                printf('<td>%s</td>', $linha->numero);
                printf('<td>%s</td>', $linha->valor);
                printf('<td>%s</td>', ($linha->status==0) ? 'Não':'Sim');
                printf('<td class="text-center">%s%s%s</td>', 
                    anchor("admin/cliente/alterar/$linha->id",' ', array('class'=>'table-actions table-edit', 'title'=>'Editar')),
                    anchor("admin/cliente/alterar_senha/$linha->id",' ', array('class'=>'table-actions table-pass', 'title'=>'Alterar Senha')),
                    anchor("admin/cliente/excluir/$linha->id",' ', array('class'=>'table-actions table-delete deletareg', 'title'=>'Excluir'))
                    );
                echo '</tr>';

Solved like this:

$query = $this->admin->get_all('faturas')->result();
            foreach ($query as $linha) {
                $cliente = $this->admin->get_byid('clientes', $linha->id_cliente)->row();
                echo '<tr>';
                printf('<td>%s %s</td>', $cliente->nome,  $cliente->sobrenome);
                printf('<td>%s</td>', $linha->numero);
                printf('<td>%s</td>', $linha->valor);
                printf('<td>%s</td>', ($linha->status==0) ? 'Não':'Sim');
                printf('<td class="text-center">%s%s%s</td>', 
                    anchor("admin/cliente/alterar/$linha->id",' ', array('class'=>'table-actions table-edit', 'title'=>'Editar')),
                    anchor("admin/cliente/alterar_senha/$linha->id",' ', array('class'=>'table-actions table-pass', 'title'=>'Alterar Senha')),
                    anchor("admin/cliente/excluir/$linha->id",' ', array('class'=>'table-actions table-delete deletareg', 'title'=>'Excluir'))
                    );
                echo '</tr>';
            }
    
asked by anonymous 16.03.2014 / 23:55

1 answer

1

Possible with INNER JOIN , CodeIgniter can be done this way:

<?php 

// application/models/admin.php

public function get_faturas()
{
    $this->db->select('faturas.numero, faturas.valor, faturas.status, cliente.nome, cliente.sobrenome');
    $this->db->from('faturas');
    $this->db->join('cliente', 'faturas.id_cliente = cliente.id');

    $query = $this->db->get();
    $result = $query->result();

    return $result;
}

?>
    
17.03.2014 / 01:53