首页 文章

将值从控制器传递到视图

提问于
浏览
0

我在模型中使用连接进行查询,并将查询结果作为数组返回

result_array();

然后我将结果返回给我的控制器,我称之为模型 .

$data = $this->Model->show();

        foreach($data as $val)
        {
        $arr[$val['recipename']] = $val['componentname'];
        }

        if($data != null)
        {
        $this->load->view('Admin\success',$arr);
        }

我原本没有$ arr值 . 我只是将它添加到那里以使阵列更清晰 . 无论如何,无论有没有 .

var_dump($data)var_dump($arr) 或即使i $recipename$componentname stll为null .

返回null . 说他们是不确定的 .

我不知道出了什么问题 .

我读了这个问题 . 这就是为什么我做了$ arr所以我可以把它变成一个单独的阵列,所以当它转移时它将被提取并试图回应它们但无济于事 .

Codeigniter passing data from controller to view

编辑:

查询工作正常,它返回值 .

$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;

2 回答

  • 0

    将数据数组传递给视图后 array keys are turned into variables . 例如,在 foreach 循环后,您的 $arr 变量就是这样的数组

    array [
        'cheese' => 'brie'
        'cookie' => 'chocolate chips'
    ]
    

    然后,您可以通过在视图中执行以下操作来访问 $arr['cheese']

    echo $cheese;
    

    尝试在视图中访问 $data$arr 将不起作用,因为它们不在范围内

  • 0

    $data 很可能是空的,并且您正在迭代一个空数组 .

    一种简单的调试方法如下:

    调试数组或对象:

    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>';
    

    您还应该在_160146中打开错误报告,如下所示:

    error_reporting(E_ALL);
    

    如果 print_r() 显示一个空数组,那么您的模型没有返回数据,您的问题的答案可能是您需要修复查询 .

相关问题