[eluser]Kromack[/eluser]
Hello everyone,
I have a PHP related question.
First, here is my code (it's in a library)
Code:
$query = $this->object->db->query($sql);
foreach($query->result() as $row)
{
$message->idForum = $row->idForum;
$message->idTopic = $row->idTopic;
$message->idUser = $row->idUser;
$message->username = $this->getNameById($row->idUser)->username;
$results[] = $message;
}
return $results;
I have 3 row returned by the sql query.
With this code the $results array contains 3 times the same row. I have checked my query and the foreach, all is ok, but after the $results[] = $message; insctruction, something strange happens...
(my $results is parsed by a foreach instruction in a view).
I have solved the problem by unsetting the $message :
Code:
foreach($query->result() as $row)
{
unset($message);
$message->idForum = $row->idForum;
$message->idTopic = $row->idTopic;
$message->idUser = $row->idUser;
$message->username = $this->getNameById($row->idUser)->username;
$results[] = $message;
}
return $results;
}
With this code, the 3 different rows are in the $results array.
I'm very confused because I can't understand why my first code doesn't work properly...
Anybody have an idea ?
PS : Sorry for my bad english