Reputation: 1
how to print all values using foreach loop.i used the loop but it print only last values from the array values.
query=$this->Common_model->getTableData('table name', array('field name' => $param))->result();
foreach($query as $rows)
{
$user=$rows->userby;
$data['user_status']=$rows->status;
$data['user']=$user;
$status=$this->Common_model->getTableData('table name', array('id' =>$data['user_status']))->row();
$data['status']=$status->name;
}
Upvotes: 0
Views: 931
Reputation: 4250
The reason that you are getting only last values is that you are assigning the values to same index which overrides the previous value. you should try this
foreach($query->result() as $rows) // correction here.
{
$user=$rows->userby;
$data['user_status'][]=$rows->status;
$data['user'][]=$user;
$status=$this->Common_model->getTableData('table name', array('id' =>$data['user_status']))->row();
$data['status'][]=$status->name;
}
Upvotes: 1
Reputation: 6499
It should had been:
$query=$this->Common_model->getTableData('table name', array('field name' => $param)); //remove ->results()
foreach($query->result() as $rows) // correction here.
{
$user=$rows->userby;
$data['user_status']=$rows->status;
$data['user']=$user;
$status=$this->Common_model->getTableData('table name', array('id' =>$data['user_status']))->row();
$data['status']=$status->name;
}
The result()
function is called everytime, to traverse through rows one-by-one (sequentially).
Upvotes: 1