【问题标题】:I cant pass variable from controller to view in Codeigniter我无法从控制器传递变量以在 Codeigniter 中查看
【发布时间】:2017-03-15 05:02:01
【问题描述】:

我收到错误提示:

遇到了 PHP 错误

严重性:通知

消息:未定义变量:数据

文件名:views/businessinfo_view.php

我的模特:

function getPosts(){
   $query = $this->db->query("SELECT * FROM customer WHERE 
   customer_id=12;");           
   return $query->result_array();
}

我的控制器:

 function ind() {
   $data['customerinfo'] = $this->user_model->getPosts(); 
   $this->load->view('businesssinfo_view', $data); 
 }

我的看法:

<?php foreach($data as $d){?>
 <tr>
     <td><?php echo $d->customer_id;?></td>
     <td><?php echo $d->first_name);?></td>
  </tr>     
<?php }?>  

我尝试了 100 种不同的方式,仍然可以传递变量,我知道我的查询是正确的,我可以在控制器中获取和回显数据,我不能只是将它传递到视图中。有人请帮助我!

【问题讨论】:

  • 变量作用域问题,$data只存在于函数内部
  • &lt;?php foreach($customerinfo as $d) { ?&gt;

标签: php codeigniter model-view-controller


【解决方案1】:
function controller() {   
   $data['customerinfo'] = $this->user_model->getPosts();    
   $this->load->view('businesssinfo_view', $data); 
}

每当您在视图中传递变量时,请尝试使用视图内的键访问它。 这个$customerinfo 变量将包含您的所有数据。

对于 EX。 $customerinfo 因为你的实际变量是$data['customerinfo']

如果 var 名称为 $data['extraVal'],则通过 $extraVal 访问视图。

【讨论】:

【解决方案2】:

试试这个:

我的模特:

function getPosts(){
   $query = $this->db->query("SELECT * FROM customer WHERE 
   customer_id = '12' ");           
   return $query->result();
}

我的控制器:

function ind() {
   $data['customerinfo'] = $this->user_model->getPosts(); 
   $this->load->view('businesssinfo_view', $data); 
 }

我的看法:

    <?php 
if(!empty($customerinfo))
{
foreach($customerinfo as $d){?>
     <tr>
         <td><?php echo $d->customer_id;?></td>
         <td><?php echo $d->first_name;?></td>
      </tr>     
    <?php }
}
else{
echo 'Some problem with the variable !! :(';
}
?> 

【讨论】:

    【解决方案3】:

    如果您有$data['customerinfo'],请尝试在您将使用的视图上,例如$customerinfo

    http://www.codeigniter.com/user_guide/general/views.html#creating-loops

    控制器

    function ind() {
      $data['customerinfo'] = array();
      $data['customerinfo'] = $this->user_model->getPosts(); 
      $this->load->view('businesssinfo_view', $data); 
    }
    

    型号

    function getPosts(){
       $this->db->where('customer_id', '12');
       $query = $this->db->get('customer');
       return $query->result_array();
    }
    

    查看

    <?php foreach($customerinfo as $d){?>
     <tr>
         <td><?php echo $d['customer_id'];?></td>
         <td><?php echo $d['first_name'];?></td>
      </tr>     
    <?php }?>  
    

    当模型函数返回 result_array() &lt;?php echo $d['first_name'];?&gt;

    当模型函数返回result()时&lt;?php echo $d-&gt;first_name;?&gt;

    找到示例here

    【讨论】:

    • 我得到同样的错误!遇到 PHP 错误 严重性:通知消息:未定义变量:customerinfo 文件名:views/businessinfo_view.php
    • 已更新为 $data['customerinfo'] = array();在控制器上
    • 公共函数 ind() { $data['customerinfo'] = $this->user_model->getPosts(); // 调用 Post 模型方法 getPosts() $this->load->view('businesssinfo_view', $data['customerinfo'] = array()); // 加载视图文件,我们将 $data 数组传递给视图文件 } 是你说的吗?
    • @Eya 使用 $this-&gt;load-&gt;view('businesssinfo_view', $data); 而不是 $this-&gt;load-&gt;view('businesssinfo_view', $data['customerinfo'] = array());
    • @wolfgang1983 感谢您抽出宝贵时间!但我仍然遇到同样的错误!我根据您的 cmets 修复了它!但仍然是同样的错误
    猜你喜欢
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 2017-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多