我在我的模型中写了这个函数:
public function get_writers()
{
$this->db->select('writer');
$query = $this->db->get('news');
return $query->result_array();
}
我想在我的视图文件中对此数组的结果运行foreach
循环:
<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>
但是我不知道我应该在foreach
循环的第一个参数中写什么。可以有人帮我这个吗?
感谢
答案 0 :(得分:0)
你应该在控制器中调用函数get_writers(),如: -
$writers = $this->your_model->get_writers();
然后你需要在视图部分中分配这个$ result变量数据,如: -
$this->view->load('view_page',array('writers'=>$writers));
之后,您可以在视图部分的表单中使用该变量数据。
<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>
答案 1 :(得分:0)
获取控制器上的值并传递给这样的视图:
<强> MODEL 强>
public function get_writers()
{
$this->db->select('writer');
$query = $this->db->get('news');
return $query->result_array();
}
CONTROLLER (在方法内)
$this->view->load('your_page_in_view_path', array('result' => $this->model->get_writers()));
查看强>
<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>