[英]Passing value from controller to view
I have a query in my model using joins and returned the query results as array 我的模型中有一个查询使用联接,并以数组形式返回查询结果
result_array();
I then returned that result to my controller where i called the model. 然后,我将该结果返回给我的控制器,我在其中调用了模型。
$data = $this->Model->show();
foreach($data as $val)
{
$arr[$val['recipename']] = $val['componentname'];
}
if($data != null)
{
$this->load->view('Admin\success',$arr);
}
I didnt originally had $arr value. 我原本没有$ arr的价值。 I just added it up there to make the array clearer.
我只是将其添加到此处以使数组更清晰。 Anyhow, with or without.
无论如何,不管有没有。
var_dump($data)
and var_dump($arr)
or even if i $recipename
or $componentname
stll null. var_dump($data)
和var_dump($arr)
,即使我$recipename
或$componentname
$recipename
$componentname
null。
returns null. 返回null。 says they are undefined.
说他们是不确定的。
I don't know what went wrong. 我不知道出了什么问题。
I read this other question. 我读了另一个问题。 that is why i made the $arr so i could make it a single array and so when it transfers it will be extracted and tried to echo them but to no avail.
这就是为什么我制作了$ arr以便可以将其制成单个数组的原因,因此当它传输时,它将被提取并尝试回显它们,但无济于事。
Codeigniter passing data from controller to view Codeigniter将数据从控制器传递到视图
EDIT: 编辑:
query works fine, it returns values. 查询工作正常,它返回值。
$sample = $this->db->select('recipe.recipename,component.componentname')->from('recipe')
->join('recipecomponent','recipe.recipeid = recipecomponent.recipeid')
->join('component','component.componentid = recipecomponent.componentid')
->group_by('recipe.recipeid')
->get()
->result_array();
return $sample;
$data
is most likely empty and you are iterating an empty array. $data
很可能为空,并且您正在迭代一个空数组。
A simple way to debug is found below: 下面是一种简单的调试方法:
echo '<div style="padding:15px; background-color:white;"><pre>'.print_r($data, true).'</pre></div>';
or 要么
echo '<div style="padding:15px; background-color:white;"><pre>'.print_r($this->Model->show(), true).'</pre></div>';
You should also turn on error reporting in your __construct
like so: 您还
__construct
像这样在__construct
打开错误报告:
error_reporting(E_ALL);
If the print_r()
shows you an empty array then your model is returning no data and the answer to your question is probably that you need to fix your query. 如果
print_r()
向您显示一个空数组,则您的模型未返回任何数据,并且问题的答案可能是您需要修复查询。
Once you pass an array of data to your view the array keys are turned into variables . 将数据数组传递到视图后, 数组键将变为变量 。 Say for example after your
foreach
loop your $arr
variable is an array like this 例如,在您的
foreach
循环之后,您的$arr
变量就是这样的数组
array [
'cheese' => 'brie'
'cookie' => 'chocolate chips'
]
You would then access $arr['cheese']
by doing the following in the view 然后,您可以通过在视图中执行以下操作来访问
$arr['cheese']
echo $cheese;
Trying to access $data
or $arr
in the view won't work because they are not in scope 尝试在视图中访问
$data
或$arr
无效,因为它们不在范围内
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.