在视图/控制器codeigniter中显示数组

时间:2018-05-21 20:24:12

标签: arrays codeigniter model-view-controller

当我在控制器和我的视图中使用var_dump数组时,我没有相同的结果。这是我的控制者:

    public function index()
{
    $getFestivals = $this->festival_model->getAllFestivals();
    if (!empty($getFestivals)) {
        foreach ($getFestivals as $festival) {
            $countFestivalEdition = $this->festival_model->countFestivalEditionByFestivalID($festival->fID);
            $festival->fCountEdition = $countFestivalEdition;
        }
    }

    $this->data['getFestivals'] = $getFestivals;
    @var_dump($this->data['getFestivals']);die;
    $this->render_layout('festivals/index', $this->data);
}

var_dump(最后一行之前的juste)返回我:

      0 => 
    object(stdClass)[24]
      public 'fID' => string '6' (length=1)
      public 'fName' => string 'Festival2' (length=17)
      public 'fPicture' => string '3df2c152d9dec657380c53b679d0b92b.jpg' (length=36)
      public 'fCity' => string 'City2' (length=9)
      public 'fCountry' => string 'France' (length=6)
      public 'fCountEdition' => int 2
  1 => 
    object(stdClass)[25]
      public 'fID' => string '8' (length=1)
      public 'fName' => string 'Festival1' (length=19)
      public 'fPicture' => string '73a267e6c047d507b66b3a1ab1fc0059.jpg' (length=36)
      public 'fCity' => string 'City1' (length=16)
      public 'fCountry' => string 'France' (length=6)
      public 'fCountEdition' => int 1

在这个var_dump上,我有我的' fCountEdition'。当我在我的视图上创建var_dump时,我有这个(var_dump($ getFestivals); die;):

      0 => 
    object(stdClass)[24]
      public 'fID' => string '6' (length=1)
      public 'fName' => string 'Festival2' (length=17)
      public 'fPicture' => string '3df2c152d9dec657380c53b679d0b92b.jpg' (length=36)
      public 'fCity' => string 'City2' (length=9)
      public 'fCountry' => string 'France' (length=6)
  1 => 
    object(stdClass)[25]
      public 'fID' => string '8' (length=1)
      public 'fName' => string 'Festival1' (length=19)
      public 'fPicture' => string '73a267e6c047d507b66b3a1ab1fc0059.jpg' (length=36)
      public 'fCity' => string 'City1' (length=16)
      public 'fCountry' => string 'France' (length=6)

为什么他不解析我的数据fCountEdition?

对于信息,我的render_layout看起来像这样:
$ this-> load-> view($ view,$ this-> data);

2 个答案:

答案 0 :(得分:0)

您可以尝试这种简单的方式将数组发送到视图,如下面的示例所示。

<强>控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {

    public function index()
    {       
        $data=$this->db->query("SELECT * FROM table_name");
        $result=$data->result_array();

        $headerData = array(
        "pageTitle" => "Home",
        "stylesheet" => array("home.css")
    );
    $footerData = array(
        "jsFiles" => array("home.js")
    );
    $viewData = array(
        "viewName" => "home",
        "viewData" => array('result'=>$result),
        "headerData" => $headerData,
        "footerData" => $footerData 
    );
    //Sendig all Result Data to view by Storing into array as $result
    $this->load->view('template',$viewData);
}

查看

<?php 

   var_dump($result);

?>

答案 1 :(得分:0)

这是因为您没有引用$getFestivals,而是将其值传递给$festival并尝试为其分配一个新变量。

基本上,传递值可以让你“复制”数组/对象/等而无需修改原始文件。

引用原始文件允许直接在循环中修改它。

按照您编写代码的方式执行操作,您需要引用原始代码。

foreach ($getFestivals as &$festival) {   <-- add ampersand before $
        $countFestivalEdition = $this->festival_model->countFestivalEditionByFestivalID($festival->fID);
        $festival->fCountEdition = $countFestivalEdition;
    }