Return database data in table

Asked

Viewed 75 times

1

Good morning, my people. I’m starting in PHP and I did this function below in order to return the data of two tables of my database in a table, I just can not make it work in any way. Can someone help me?

MODEL:

public static function listar(){
    $sql = "SELECT p.id, p.canhoto, m.nome, p.id_motorista FROM canhotos_saida p INNER JOIN motoristas m ON p.id_motorista = m.id";
    $conexao = new Conexao();
    $result = $conexao->consultar($sql);

    $lista = new ArrayObject();

    while ( list($id, $canhoto,$motorNome, $motorId) = mysqli_fetch_row( $result ) ){

        $driver = new Motoristas($motorNome, $motorId);

        $cs = new CanhotosSaida($id, $canhoto, $driver);

        $lista->append($cs);        
    }
    return $lista;
}

Table code:

<?php
                include_once './model/clsCanhotosSaida.php';

                $lista = CanhotosSaida::listar();

                foreach ($lista as $nota) {

                echo '<tr>';
                echo '    <td>' .$nota->id. '</td> ';
                echo '    <td>' .$nota->canhoto. '</td> ';
                echo '    <td>' .$nota->motorista->nome. '</td> ';
                echo '</tr>';

                }

                ?>

With this code there, returns me the following error:

Notice: Trying to get property of non-object in C:\xampp\htdocs\teste\notas.php on line 39

Line 39 is exactly in this snippet of table code:

echo '    <td>' .$nota->motorista->nome. '</td> ';

Detail: The note->id and the left-hander are appearing on the table, only the driver’s name that does not...

  • try something like $nota->['motorista']->nome;

  • Sorry for the delay. Unfortunately I’ve done everything and I still can’t figure out what’s wrong...

  • give a var_dump($nota->motorista); and show the result please.

1 answer

0

Hello!!

try to give an append also on the driver object, just as you did on the other:

$lista->append($cs); 

Browser other questions tagged

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