Foreach in an HTML table - PHP - CODEIGNITER

Asked

Viewed 758 times

0

Hello, I’m doing the following:

Model:

    public function getLocatarios() {
    $this->db
  ->select('*')
  ->from('tbl_locatario');
  return $query = $this->db->get()->result();
    }

Already on the controller:

$data = array("tabelaLocatarios" => $this->Locatario_Model->getLocatarios());
$this->load->view('teste', $data);

Already in HTML

<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
    <thead>
        <tr>
            <th>Nome</th>
            <th>E-mail</th>
            <th>Telefone</th>
            <th>Opções</th>
        </tr>
    </thead>

    <tbody>
      <?php if (!empty($tabelaLocatarios)):
        foreach ($tabelaLocatarios as $row): ?>
        <td>
          <?php echo $row->NOME;?>
        </td>
        <td>
          <?php echo $row->EMAIL;?>
        </td>
        <td>
          <?php echo $row->TELEFONE1;?>
        </td>
        <td>
          Opções arrumar
        </td>

      <?php endforeach; ?>
    <?php else: {
      echo "<td colspan='5' align = 'center'>
      Você ainda não possui nenhum locatário cadastrado...
      </td>";
    } ?>
  <?php endif; ?>
  </tbody>

</table>

However this returning me in just one row, I would like it to return in one table with one row below the other.

inserir a descrição da imagem aqui

What am I doing wrong?

  • 1

    each row of a table is one <tr></tr> lacked it in your code

1 answer

0


You must do it this way:

<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
    <thead>
        <tr>
            <th>Nome</th>
            <th>E-mail</th>
            <th>Telefone</th>
            <th>Opções</th>
        </tr>
    </thead>
    <tbody>
        <?php if (!empty($tabelaLocatarios)){
            foreach ($tabelaLocatarios as $row){ ?>
        <tr>
            <td>
                <?php echo $row->NOME;?>
            </td>
            <td>
                <?php echo $row->EMAIL;?>
            </td>
            <td>
                <?php echo $row->TELEFONE1;?>
            </td>
            <td>
                Opções arrumar
            </td>
        </tr>
            <?php } ?>
        <?php  } else { ?>
            <tr>
                <td>Você ainda não possui nenhum locatário cadastrado...</td>
            </tr>
        <?php } ?>
    </tbody>
</table>

Missed line option inside your loop.

Browser other questions tagged

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