Welcome Guest, Not a member yet? Register   Sign In
Displaying all array values in a view. foreach not working for some reason.
#1

[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.
#2

[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;
}
#3

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

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

  return $query->result_array();
}

Why bother with the foreach at all?




Theme © iAndrew 2016 - Forum software by © MyBB