Print data from a PHP object array

Asked

Viewed 2,484 times

0

I beat myself a little with return of the database using POO and MVC, and in case I’m bringing the data from the database where the registered date is equal to the current date, the problem is that I created an array of objects to store this return, and I have to display the data on another page, which is probably where the error is occurring, follow the code

DAO:

 function buscaEventoPorDia($data, $idUsuario){
        $stm = $this->pdo->prepare('select e.id_evento, u.nome_usuario, e.data_evento, e.descricao_evento, e.local_evento, e.prioridade_evento, e.organizador_evento from evento e inner join usuario u on e.usuario_id_usuario = u.id_usuario where e.data_evento = ? AND e.usuario_id_usuario = ?');

        $stm->bindValue(1, $data);
        $stm->bindValue(2, $idUsuario);
        $stm->execute();

        $max = $stm->rowCount();

        echo $max;

        //for ($i=0;$i < $max; $i++);

        $retornoEventos = [];       
        while ($linha=$stm->fetch(PDO::FETCH_ASSOC)){
            $retornoEvento = new Evento();
            $retornoEvento->setId($linha['id_evento']);
            $retornoEvento->setUsuario($linha['nome_usuario']);
            $retornoEvento->setData($linha['data_evento']);
            $retornoEvento->setDescricao($linha['descricao_evento']);
            $retornoEvento->setLocal($linha['local_evento']);
            $retornoEvento->setPrioridade($linha['prioridade_evento']);
            $retornoEvento->setOrganizador($linha['organizador_evento']);
            $retornoEventos[] = $retornoEvento;
        }

            return $retornoEventos;`
}

Control Code:

$retornoEvento = $userDAO->buscaEventoPorDia($date, $id_user);//$evento)

        //var_dump($retornoEvento);

        echo"<tr>"; 
            echo"<td align='center'>".$retornoEvento->getId();"</td>";
            echo"<td align='center'>".$retornoEvento->getUsuario();"</td>";
            echo"<td align='center'>".$retornoEvento->getDescricao();"</td>";
            echo"<td align='center'>".$retornoEvento->getData();"</td>";
            echo"<td align='center'>".$retornoEvento->getPrioridade();"</td>";
            echo"<td align='center'>".$retornoEvento->getLocal();"</td>";
            echo"<td align='center'>".$retornoEvento->getOrganizador();"</td>";
            echo"</tr>";`

I would like a light for who knows how to tidy the noose go and create the counter or what should be the right one to do.

Excuse my ignorance in case the question pattern is incorrect, or the question is totally dumb. =)

1 answer

1


What’s missing for you is to iterate your array $retornoEvento.

For this use a foreach which provides an easy way to do this:

foreach ($retornoEvento as $obj) {
  echo $obj->getId();
  echo $obj->getUsuario();
  echo $obj->getDescricao();
  echo $obj->getData();
  echo $obj->getPrioridade();
  echo $obj->getLocal();
  echo $obj->getOrganizador();
}
  • Thanks man, I managed to tidy up here with your tip.

  • @Lucascarvalho anything, anything just ask!

Browser other questions tagged

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