Welcome Guest, Not a member yet? Register   Sign In
foreach outputting array?
#1

[eluser]Th3FalleN[/eluser]
Okay i have another noobish question what im attempting to do is a foreach statement from a query and have it outputted as an array or some way that i could use the results with the form_dropdown() function in CI any ideas?
#2

[eluser]d1a8lo24[/eluser]
Example on your view file

Code:
<select name="">
  &lt;?php foreach($array_result as $row): ?&gt;
  <option value="&lt;?php echo $row->value; ?&gt;">&lt;?php echo $row->value; ?&gt;</option>
  &lt;?php endforeach; ?&gt;
</select>

I hope it helps
#3

[eluser]osci[/eluser]
in your model

Code:
function get_combo()
{
   $query = $this->db->from('table')->select('id, displayfield')->get();

   if($query->num_rows() > 0) {
   foreach ($query->result() as $row) {
      $data[$row->id] = $row->displayfield;
   }
   return $data;
   }
}

in your controiller
Code:
//......
$data['rs_combo'] = $this->your_model->get_combo();
$this->load->view('view', $data);

in your view

Code:
&lt;?php echo form_dropdown('name', $rs_combo); ?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB