我正在尝试将数据从我的控制器发送到视图。这是我的做法: 在控制器中:
function getAllProjects()
{
$where= array('id_user'=>$this->session->userdata('id_user'));
$result=$this->expenses_model->get_all('projets', $where);//get_all returns an array
echo json_encode ($result);
return $result;
}
function index (){
$data['all_projects']= $this->getAllProjects();
$this->session->set_userdata('id_user', 28);
$this->load->view('layout/public/header');
$this->load->view('content/public/profil/modules/expenses',$data);
$this->load->view('layout/public/footer');
}
echo json_encode $data 在控制器中返回正确的结果),但我不知道如何在视图中显示它。我试过了:(在视图中)
<label><strong>Sur le projet</strong></label>
<select class="span4" id="projet" name="projet">
<?php
if (isset($all_projects) && ! empty($all_projects))
{ echo "there is some projects"; //is not printed
foreach ($all_projects as $project){
echo "<option value=".$project['id_projet'].">".$project['titre']."</option>"; }
else {
echo "<option value='0'>No projects</option>";
}
但它在下拉列表“项目”中没有显示任何内容我做错了什么?有人可以帮我吗?