[英]can't send data from controller to view in a codeIgniter project
I'm trying to send data from my controller to the view. 我正在尝试将数据从控制器发送到视图。 Here is how I do: In the controller:
我是这样做的:在控制器中:
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 returns a correct result) in the controller but I don't know how to do to display it in the view. 在控制器中回显json_encode $ data返回正确的结果),但我不知道如何在视图中显示它。 I tried:(in the view)
我尝试过:(在视图中)
<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>";
}
But it does not show anything in the drop down list "projects" What I am doing wrong? 但是在“项目”下拉列表中没有显示任何内容。我在做什么错? Could anyone help me please?
有人可以帮我吗?
mysql_query()
returns a resource and you're handing that to $this->load->view();
mysql_query()
返回一个资源 ,您将其交给$this->load->view();
as the second parameter. 作为第二个参数。 If you dig into the underlying CodeIgniter source, you'll find that CodeIgniter does not know how to deal with Resources as this parameter - only Arrays and Objects.
如果深入研究底层CodeIgniter源,您会发现CodeIgniter不知道如何将Resource作为此参数来处理-仅数组和对象。
Please refrain from using mysql_query()
and it's friends. 请不要使用
mysql_query()
及其朋友。 As per PHP official documentation : 根据PHP官方文档 :
Use of this extension is discouraged.
不鼓励使用此扩展名。 Instead, the MySQLi or PDO_MySQL extension should be used.
相反,应使用MySQLi或PDO_MySQL扩展。
More importantly than this, if you're using CodeIgniter on-top of PHP, use the Database Class in conjunction with the Active Record Class to perform database queries. 更为重要的是,如果在PHP上使用CodeIgniter,则将数据库类与Active Record类结合使用以执行数据库查询。 Hell, even use Datamapper ORM - please don't use
mysql_query()
or friends. 地狱,甚至使用Datamapper ORM-请不要使用
mysql_query()
或朋友。
I changed the foreach like this and it worked : 我像这样更改了foreach并且它起作用了:
foreach ($all_projects as $project)
{
echo "<option value=".$project->id_projet.">".$project->titre."</option>";
}
Thank you for your answers 谢谢您的回答
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.