How to view data from three tables in a view?

Asked

Viewed 150 times

1

I have three tables: Process, Progress and Costs: how do I display the data of the three in a View, and the tables Tempo and Custas have as a foreign key, the primary Process key?

I made a foreach for each table, but it didn’t work, keeps duplicating the data.

        <div class="row">                         
            <strong>Sentença/Acórdão:</strong>
            <?php echo $processo[0]->sentenca; ?>         
        </div>
        <br>
        <div class="row">                         
            <p><font size="3" face="helvetica"><strong>Andamentos</strong></font></p>             
        </div>     
        <div class="row">        
            <div class="table-responsive">                                   
                <button type="submit" class="btn btn-primary hidden-print" data-toggle="modal" data-target=".addandamento"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span></button>  
                <table class="table table-striped">                                         
                    <?php foreach ($processo as $proc) { ?>
                        <tr class="row">                                
                            <td width="14%"><font color="#000000" size="2" face="tahoma"><b><?= $proc->dtandamento; ?></b></font></td>
                            <td width="78%"><font color="#000000" size="2" face="tahoma"><b><?= $proc->descricao; ?></b></font></td>                            
                        </tr>  
                    <?php } ?>                    
                </table>
            </div>             
        </div>                  
        <div class="row">        
        <div class="table-responsive">                                   
            <button type="submit" class="btn btn-primary hidden-print" data-toggle="modal" data-target=".addandamento"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span></button>  
            <table class="table table-striped">                                         
                <?php foreach ($processo as $proc) { ?>
                    <tr class="row">                                
                        <td width="14%"><font color="#000000" size="2" face="tahoma"><b><?= $proc->dtcustas; ?></b></font></td>
                        <td width="78%"><font color="#000000" size="2" face="tahoma"><b><?= $proc->descricao_custas; ?></b></font></td>                            
                    </tr>  
                <?php } ?>                    
            </table>
        </div>                       
    </div>

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • How did you pass the data to view? Enter your code!

  • @Andrébaill put the view code in the question.

  • Ok and the controller and model code?

1 answer

1

I don’t know if I got it right, but this you solve with the models, making a select that makes the JOINS between the three tables, something like:

SELECT * FROM Processo INNER JOIN Andamento ON Andamento.id_processo = Processo.id INNER JOIN Custas ON Custas.id_processo = Processo.id ...;

This using the functions of $this->db of codeigniter, as:

    $this->db->from('Processo');
    $this->db->join('Andamento', 'Andamento.id_processo=Processo.id');
    $this->db->join('Custas', 'Custas.id_processo=Custas.id');

That will return an array, which you should iterate in your view.

  • @Ipfranz I had already done the joins, the problem is that the view shows the data of both tables (Tempo and Custas) in duplicate form. I think it has to do with the foreach, but I still don’t see the problem.

  • This looks like it can be solved with a GROUP BY, nay?

  • @Ipfranz group_by solved for the Progress table, but now the Costs table only displayed the first record :(

  • How are the relationships between these tables? Are there Multiple Movements/Costs for each process? You would need to see the bank to help better

  • I’ll post the tables' print on the question, it may be?

  • Model Code: Function get_processos_like() { $term = $this->input->post('search'); $this->db->Join('progress', 'fkcodprocess=codprocess', 'left'); $this->db->Join('costs', 'fkcodproc=codprocess', 'left'); $this->db->select('*'); $this->db->Where('rocenpsso', $term); $this->db->order_by ("str_to_date(tempo.dtandamento, '%d-%m-%Y')", 'DESC', FALSE); $this->db->group_by ('andamento.dtandamento'); Return $this->db->get('processo')->result(); }

Show 1 more comment

Browser other questions tagged

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