how to traverse each row of an object array and display on the screen (PHP)

Asked

Viewed 31 times

0

I am having a problem displaying a query(Select) of some items on the screen. It is only displaying the first line.

<?php
   require_once 'app/Lib/Database/Connection.php';
   require_once 'app/Controller/ImoveisController.php';
   require_once 'app/Model/Imoveis.php';

   $result =  new ImoveisController;
   $row = $result->selectAll();

   var_dump($row);
?>

<!DOCTYPE html>
<html>
  <head> 
    <title>CRUD PHP</title>
  </head>

  <body>
    <table>
      <thead>
        <tr>
          <th>Cod_imovel</th>
          <th>Proprietario</th>
          <th>estado</th> 
          <th>bairro</th>
          <th>municipio</th>
          <th>cep</th>
          <th>numero</th>
          <th>rua</th>
        </tr>

      </thead> 

      <tbody>

      <?php foreach($row as $value):?>

      <td><?php echo $value->i_imoveis?></td>
      <td><?php echo $value->estado ?></td>
      <td><?php echo $value->bairro ?></td>
      <td><?php echo $value->municipio?></td>
      <td><?php echo $value->cep ?></td>
      <td><?php echo $value->numero?></td>
      <td><?php echo $value->rua?></td>
      <?php endforeach;?>
     </tbody>
   </table>
  </body>
</html>

model :

<?php
   class Imoveis{
      public static function read(){
        $c = Connection::getConn();
        
        $sql = "SELECT * FROM imoveis";
        $sql = $c->prepare($sql);
        $sql->execute();

        $resultado = array();

        while($row = $sql->fetchObject('Imoveis')){
            $resultado[] = $row;
        }
        if(!$resultado){
            throw new Exception("Não foi encontrado nenhum registro");
        }
        return $resultado;
      }          
   }
?>

this model is being called by a controller and until the call $result = new ImoveisController; $row = $result->selectAll(); is everything ok by checking in var_dump, but when you go through this Real Estate Array with `

   <td><?php echo $value->i_imoveis?></td>
   <td><?php echo $value->estado ?></td>
   <td><?php echo $value->bairro ?></td>
   <td><?php echo $value->municipio?></td>
   <td><?php echo $value->cep ?></td>
   <td><?php echo $value->numero?></td>
   <td><?php echo $value->rua?></td>
   <?php endforeach;?>
  </tbody>`

it happens to appear only the first record

Controller:

<?php
   class ImoveisController{
     public function selectAll(){
        $all =  Imoveis::read();               
        return $all;
     }         
   }
?>

  • Could add in question the method selectAll of the controller?

  • added the controller

  • var_dump($row); what this displays?

  • By the way, you noticed that in your table in HTML there are no elements <tr>?

  • In the case var_dump was only serving to verify that the variable was receiving the value correctly. It was missing <tr>. I managed to solve here by doing like this: <?php foreach($Row as $value){ echo "<tr>"; echo "<td>". $value->i_imoveis." </td>";echo "</tr>"; }?> like this by calling html inside php and I couldn’t get it to run correctly. I appreciate the help

  • The fact that HTML is in PHP will not change anything, you are changing know code randomly without understanding what you are doing. The problem was just the lack of <tr>

  • Then it turned out that I was not very clear in the old comment, I forgot to report that there was another mistake. In case the problem of appearing only the first line was solved yes with the use of <tr>, with this appeared the other records that the variable was bringing, but the same was playing all the results in the same line, I tried the use of </br> but without success. Finally I did what I said before, I called the html inside the php tag, yes it worked.

  • From what I understood it was supposed to work both ways, but it only worked this way. Again I appreciate the help and I’m sorry I wasn’t clear. I’m new around here, how can I close that question?

Show 3 more comments
No answers

Browser other questions tagged

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