Reputation: 88
So I was trying to pull all the names from all the users from the database but I only get one, in this case the first result it finds in the table. Here's the code i'm trying:
public function getAllUsers(){
$query = $this->db->get('user');
$result = $query->result();
foreach ($result as $record)
{
return $record->name;
}
}
I've already tried a lot of convinations but to no avail. I'm using a var_dump to display it on the view. I've already tried to loop it in the vewi too but nothing happens. Any help?
EDIT[SOLVED] : The foreach in this case will loop and bring all the records but once it's done all the data is lost so in order to keep the data, I was suggested to create an empty array first, then set the data to the array within the loop and then return that array after the loop is done. Thanks!
Upvotes: 1
Views: 144
Reputation: 22532
Your foreach loop runs only one time and return data. You need to store your data into array and result
$data=array(); // define empty array
foreach ($result as $record)
{
$data[]= $record->name;// assign name to array
}
return $data;// return data
Upvotes: 4
Reputation: 4829
try this:
public function getAllUsers(){
$query = $this->db->get('user');
$result = $query->result();
$name = array();
foreach ($result as $record)
{
$name[] = $record->name;
}
return $name ;
}
Upvotes: 2