【问题标题】:Displaying data which was passed from Controller to View in codeigniter在 codeigniter 中显示从 Controller 传递到 View 的数据
【发布时间】:2018-08-08 10:47:49
【问题描述】:

我在显示从数据库表中检索到的数组时遇到问题 这就是我的数组的外观。

注意我是从我的数据库表中选择的

"WHERE pro_cat_id = 36" ;

Array
(

 Array
  (
    [0] => Array
    (
        [pro_cat_id] => 1
        [cat_name] => men
    )

[1] => Array
    (
        [pro_cat_id] => 2
        [cat_name] => women
    )

[2] => Array
    (
        [pro_cat_id] => 3
        [cat_name] => Gilamlar
    )

[3] => Array
    (
        [pro_cat_id] => 4
        [cat_name] => kids
    )

[4] => Array
    (
        [pro_cat_id] => 5
        [cat_name] => others
    )

[5] => Array
    (
        [pro_cat_id] => 27
        [cat_name] => game
    )

)

这是我的控制器的索引代码

    public function index()
    {
        $res = $this->Categories_model->getCategory();
        //  ;
        // print_r($data);
        $data = $res;

            echo "<br><br><br><pre>";
            print_r($data);
            echo "</pre>";
        $this->load->view('categories',$data);
    }

其中 getCategory 是一个模型函数,用于从数据库表中检索所有行

我试过echo它是($pro_cat_id and $cat_name )

如何在视图中使用这些数据?

如何传递多维数组在里面查看和使用?

【问题讨论】:

    标签: php codeigniter codeigniter-3 codeigniter-query-builder


    【解决方案1】:

    希望对您有所帮助:

    你的index 方法应该是这样的:

    public function index()
    {
        $data['categories'] = $this->Categories_model->getCategory();
        // print_r($data);
        $this->load->view('categories',$data);
    }
    

    在您的categories 视图页面中,像这样使用foreach 循环:

    <?php 
      if (! empty($categories))
      {
        foreach ($categories as $category)
        { 
          echo $category['pro_cat_id'];
          echo $category['cat_name'];
        }
      }
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-15
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多