Pows Powya
Pows Powya

Reputation: 1

foreach loop does not print all values in view page from controller

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

Answers (2)

Code Prank
Code Prank

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

linuxeasy
linuxeasy

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

Related Questions