首页 文章

我无法将变量从控制器传递到Codeigniter中的视图

提问于
浏览
0

我收到错误说:

遇到PHP错误严重性:通知消息:未定义的变量:data文件名: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种不同的方法,仍然可以传递变量,我知道我的查询是正确的,我可以在控制器中获取和回显数据,我不能只是将它传递给视图 . 有人请帮帮我!

3 回答

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

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

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

    如果var name是 $data['extraVal'] ,则在视图中通过 $extraVal 进行访问 .

  • 0

    尝试使用 $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 }?>
    

    当model函数返回result_array() <?php echo $d['first_name'];?>

    当model函数返回result() <?php echo $d->first_name;?>

    找到的示例here

  • 1

    试试这个:

    我的模特:

    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 !! :(';
    }
    ?>
    

相关问题