CodeIgniter Forums
Displaying all array values in a view. foreach not working for some reason. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Displaying all array values in a view. foreach not working for some reason. (/showthread.php?tid=2234)



Displaying all array values in a view. foreach not working for some reason. - El Forum - 07-24-2007

[eluser]jtotheb[/eluser]
i have a model:

Code:
function contact_values()
{
  $query = $this->db->get('column_data');
  
  foreach($query->result_array() as $col_list)
  {
   return $col_list;
  }
}

I have a controller:

Code:
function index()
{
  $this->load->model('Testmodel');
  $data['con_options']= $this->Testmodel->contact_values();
  $this->load->view('selection', $data);
}

And i have a view:

Code:
<ul>
&lt;?php foreach($con_options as $item):?&gt;
<li>&lt;?=$item;?&gt;</li>
&lt;?php endforeach;?&gt;
</ul>

And it only shows the first value in the view.

Can anyone tell me where i'm going wrong?

I think it must be in the controller, that i'm not passing the model result as an array properly to the $data array.

If anyone can help i would be very grateful.

Thanks.


Displaying all array values in a view. foreach not working for some reason. - El Forum - 07-24-2007

[eluser]pr0digy[/eluser]
Return is used only once, to return the function output. Here is what you should do:

Code:
function contact_values()
{
  $query = $this->db->get('column_data');
  
  $items = array();

  foreach($query->result_array() as $col_list)
  {
   $items[] = $col_list;
  }
  
  return $items;
}



Displaying all array values in a view. foreach not working for some reason. - El Forum - 07-24-2007

[eluser]jtotheb[/eluser]
Great, thank you very much.


Displaying all array values in a view. foreach not working for some reason. - El Forum - 07-24-2007

[eluser]deviant[/eluser]
Code:
function contact_values()
{
  $query = $this->db->get('column_data');

  return $query->result_array();
}

Why bother with the foreach at all?