Foreach in an HTML table - PHP - CODEIGNITER

0

Hello, I'm doing the following:

Model:

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

Already in 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>

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

What am I doing wrong?

    
asked by anonymous 29.11.2018 / 23:05

1 answer

0

You should 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>

The line option was missing within your loop.

    
01.12.2018 / 19:06