【问题标题】:codeigniter table array variable not definedcodeigniter 表数组变量未定义
【发布时间】:2014-07-07 10:32:47
【问题描述】:

这应该是一个简单的将数组传递给视图。控制器设置为从表中提取所有数据。我已经初始化了表库并使用 $this->load->model(array('Account') 加载了模型代码。我已经构建了完整的 crud 方法。

我的视图页面上出现了一个未定义的变量。

控制器代码是:

        public function index(){
        $this->load->library('table');
        $children = array();                    
        $this->load->model(array('Account'));
        $children = $this->Account->get();      //retrieves all the records in the database
          foreach ($children as $child){
            $account = new Account();
            $account->load($account->id);
            $children[] = array(
                $account->id,
                $account->familyName,
                $account->addr1,
                $account->city,
                $account->state,
                $account->zip,
                $account->phone1,
                $account->email,
                $account->parent_only,  
            );
          }


        $this->load->view('main_view');
        $this->load->view('body_content_ma', array(
            'body_content_ma' => $children,

        ));
     }

在视图页面上,我收到一个错误,指出未定义的变量子项。您认为我在这里缺少什么:

查看代码为:

           <?php 
       $this->table->set_heading('ID', 'Family Name', 'City', 'State', 'Zip', 'Phone', 'Email', 'Parent only Pickup');
       echo $this->table->generate($children);
      ?>

回答:

这是最终奏效的编码。

我不得不重复几次才能找到丢失的东西。

     public function index(){
        $this->load->library('table'); 
        $children = array();
        $this->load->model(array('Account'));

        $account = $this->Account->get();      //retrieves all the records in the database
          foreach ($account as $c){
            $account = new Account();
            $account->load($account->id);
            $children[] = array(
                $c->id,
                $c->familyName,
                $c->addr1,
                $c->city,
                $c->state,
                $c->zip,
                $c->phone1,
                $c->email,
                $c->parent_only,    
            );
          }

        //echo '<tt><pre>' . var_export($children, TRUE) . '</pre></tt>';

        $this->load->view('main_view');
        $this->load->view('body_content_ma', array(
            'body_content_ma' => $children
            ));

     }

我有一种感觉,我多次使用 $children 并覆盖了变量。我是正确的。一旦我声明了变量。我不应该再次使用它。

当我将变量 $children 重新分配给 $children = $this->Account->get(); 我想我有效地覆盖了之前声明的数组。因此,即使数组被填充,数组引用也被破坏了。

我确实在视图代码中使用了 $body_content_ma 来生成表格。 现在一切都在渲染。

开始分页!

【问题讨论】:

    标签: php codeigniter view


    【解决方案1】:

    您缺少的只是简单的东西 $children 变量被映射到 $body_content_ma 只需将 $children 替换为 $body_content_ma 即可。

    echo $this->table->generate($children);
    

    替换为

    echo $this->table->generate($body_content_ma );
    

    【讨论】:

    • 我希望它如此简单。我理解它应该如何工作,但我一定是在某个地方错过了一个设置。是否有一些我缺少的配置设置?
    【解决方案2】:

    您可以使用此代码。希望对您有用。

    public function index(){
        $this->load->library('table');
        $children = array();                    
        $this->load->model(array('Account'));
        $data['children'] = $this->Account->get();     //retrieves all the records in the database
    
        $this->load->view('main_view');
        $this->load->view('body_content_ma',$data));
     }
    

    在视图中,您可以使用 $children 作为数组来打印值。

    【讨论】:

    • 尝试了上面的代码,但我仍然收到错误消息 undefined variable: children 以及如果我更改 echo $this->table->generate($data)。我得到未定义的变量:数据。就好像路径不对,数据传不过去。
    【解决方案3】:

    这是我的职责;

        public function index()
        {        
            $this->load->library('table');
            $this->load->model('Account_model');
    
            $data['children']  = $this->Account_model->get();
    
            foreach ($data['children'] as $child) 
            {
                // Your foreach code...
            }
    
            $this->load->view('main_view');
            $this->load->view('body_content_ma', $data);
      }
    

    【讨论】:

      【解决方案4】:

      您的view

      <?php 
         $this->table->set_heading('ID', 
                                   'Family Name', 
                                   'City', 
                                   'State', 
                                   'Zip', 
                                   'Phone', 
                                   'Email', 
                                   'Parent only Pickup');
      
         echo $this->table->generate($body_content_ma);
      ?>
      


      说明:

      您将 body_content_ma 从您的控制器传递到您的视图,而不是 children 因此给您一个 undefined error


      编辑:

      下面是生成表格行的数组格式。
      array 应包含 array of rows

      $data = array(
               array('Name', 'Color', 'Size'),
               array('Fred', 'Blue', 'Small'),
               array('Mary', 'Red', 'Large'),
               array('John', 'Green', 'Medium')   
               );
      
      echo $this->table->generate($data);
      


      文档: http://ellislab.com/codeigniter/user-guide/libraries/table.html

      【讨论】:

      • 感谢您的建议,但仍然不对。现在我的左侧菜单消失了,是的,我检查了我的语法。我所做的只是将 $body_content_ma 添加到生成语句中。我检查了我的数组的内容,它不为空。我可以将数组显示到屏幕上。没事。解释有帮助。
      • 你能用print_r($body_content_ma);显示打印出来的数组吗?
      • 我的错误,我没有松开我的菜单。我做了错误消息:未定义的变量:body_content_ma 文件名:views/body_content_ma.php
      • 我在那个数组中得到了内容
      • 在您看来,print_r($body_content_ma); 完成此操作后您会获得内容,对吧?
      猜你喜欢
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多