I was wrote this function in my model :
public function get_writers()
{
$this->db->select('writer');
$query = $this->db->get('news');
return $query->result_array();
}
And I want to run a foreach
loop on result of this array in my view file :
<label for="writer">Writer</label>
<select name="writer" id="writer">
<?php foreach (??? as $each): ?>
<option value="<?php echo $each['id'] ?>"><?php echo $each['writer'] ?></option>
<?php endforeach; ?>
</select>
But I don't know what should I write in the first parameter in foreach
loop.Can anybody help me with this ? Thanks
You should call the function get_writers() in controller like :-
$writers = $this->your_model->get_writers();
Then you need to assign this $result variable data in view section like :-
$this->view->load('view_page',array('writers'=>$writers));
After that you can use that variable data in form in view section.
<label for="writer">Writer</label>
<select name="writer" id="writer">
<?php foreach ($writers as $each): ?>
<option value="<?php echo $each['id'] ?>"><?php echo $each['writer'] ?></option>
<?php endforeach; ?>
</select>
Get the value on controller and pass to view like this:
MODEL
public function get_writers()
{
$this->db->select('writer');
$query = $this->db->get('news');
return $query->result_array();
}
CONTROLLER (inside of your method)
$this->view->load('your_page_in_view_path', array('result' => $this->model->get_writers()));
VIEW
<label for="writer">Writer</label>
<select name="writer" id="writer">
<?php foreach ($result as $each): ?>
<option value="<?php echo $each['id'] ?>"><?php echo $each['writer'] ?></option>
<?php endforeach; ?>
</select>