Print two foreach variables in a table in different columns - codeigniter

Asked

Viewed 499 times

0

I am trying to print a table that accesses the database and inserts the different values of each array in each column. The problem is not printing the values correctly..

Controller:

 private function mostrar_resultados() {

    //vai buscar o resultado da pesquisa
    $query = $this->Oportunidades_model->busca_oportunidade();
    $query1 = $this->Oportunidades_model->busca_funcao();

    //obtem o resultado em forma de array bidimensional
    $data['resultados_pesquisa'] = $query->result_array();
    $data['resultados_funcao']=$query1->result_array();

    //passa as variáveis anteriores para a view
    $this->load->view('verOportunidades', $data);

  }

Model:

class Oportunidades_model extends CI_Model {
  //fazer echo
  //edita um voluntario;
  function busca_oportunidade()
  {
    $interrogacaoVol_sql = "SELECT id_oport, descricao_oport FROM oportunidade";
    $query = $this->db->query($interrogacaoVol_sql);
    return $query;
  }

  function busca_funcao()
  {
    $interrogacaoVol_sql = "SELECT id_fun, descricao_fun FROM funcao";
    $query = $this->db->query($interrogacaoVol_sql);
    return $query;
  }
}

View:

<table class="table">
  <tr>
    <th>Descricao</th>
    <th>Função</th>
  </tr>

  <?php
  foreach ($resultados_pesquisa as $linha)
  {
    $descricao = $linha["DESCRICAO_OPORT"];

    foreach ($resultados_funcao as $key) {
      $funcao = $key["DESCRICAO_FUN"];
      echo "<tr><td>$descricao</td><td>$funcao</td></tr>";        
    }
  }
  ?>        
</table>

Output with 2 registered opportunities:

 Descricao  Função                          
    teste1      resultado1 
    teste1      resultado2
    teste2      resultado1
    teste2      resultado2

Desired:

  Descricao Função                          
    teste1      resultado1 
    teste2      resultado2

1 answer

1


NOTE: I’m responding to what you put in "wanted" in the question. Looking at the rest of your code, it seems to me that you’re not having a safe way of getting the left column to relate to the right one, but to solve that, you don’t just get what was asked.


Supposing $resultados_pesquisa and $resultados_funcao are the same size, that’s it:

<?php
   $count = count( $resultados_pesquisa );
   for( $i = 0; $i < $count; $i++ )
   {
      echo '<tr>';
      echo '<td>'.$resultados_pesquisa[$i]['DESCRICAO_OPORT'].'</td>';
      echo '<td>'.$resultados_funcao[$i]['DESCRICAO_FUN'].'</td>';
      echo '</tr>';        
   }
?>     

For safety, you can do the $count at least data value:

   $count = min( count( $resultados_pesquisa ), count( $resultados_funcao) );

If the data is not in HTML, it is essential to use htmlentities:

   echo '<td>'.htmlentities($resultados_pesquisa[$i]['DESCRICAO_OPORT']).'</td>';

(same thing for next line).

  • It worked, thank you!

  • If in case they don’t have the same size?

  • @Rafael is in the answer, the line with the "min" makes the table catch the smaller size. if you need the larger one and leave the rest blank, just exchange the min for max, and put an if for each echo to not catch non-existent indexes.

Browser other questions tagged

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