在代码点火器中,我无法访问视图中的控制器阵列


I cant access my controller array in my view in codeigniter

我无法访问codeigniter中视图中的控制器数组。当我在视图中传递数组时,它会给我一个未识别的变量错误,请帮帮我!此代码用于更改配置文件图片。

我想将更改后的图片显示到我的视图中!我可以获得从我的模型到我的视图的照片路径,但不能从我的控制器访问它

我的代码在这里

型号:

class Upload_model extends CI_Model {  
 public function __construct() {
  parent::__construct();
  $this->load->database();
  }   
 public function update_photo($source) { 
  $username = $_SESSION['username'];
  $pass = array('avatar'=> $source);
  $this->db->update('users',$pass,array('username'=>$username));
  $query =     $this->db->get_where('users',array('avatar'=>$source,'username'=>$username),1    );    
  foreach ($query ->result() as $row)
  {
   return $row->avatar;  
  }   
 }
}

控制器:

class Upload_photo extends CI_Controller {
 public $upload_model = "";
 function __construct()
 {
  parent::__construct();
  $this->load->helper(array('form', 'url'));   
  $this->load->model('upload_model');               
 }
 public function index() {      
  session_start();
  if(isset($_FILES['file1']))
  {     
   $u = $_SESSION['username'];        
   $config['upload_path'] = 'user/'.$u;         
   $config['allowed_types'] = 'gif|jpg|png';
   $config['max_size']  = '30000';
   $config['max_width']  = '1024';
   $config['max_height']  = '768';             
   $this->load->library('upload',$config);    
   $filename = $_FILES['file1']['name'];
   if(!$this->upload->do_upload('file1'))
   {
    echo "Error". $this->upload->display_errors();
    return;           
   }                       
   $config = array();
   $config['image_library'] = 'gd2';
   $config['source_image']  = 'user/'.$u.'/'.$filename ;                
   $config['new_image']= FCPATH . 'user/'.$u.'/Thumb/';
   $config['create_thumb'] = TRUE;
   $config['maintain_ratio'] = FALSE;
   $config['width'] = 110;
   $config['height'] = 110;          
   $this->load->library('image_lib',$config);                     
   if(!$this->image_lib->resize())
   {
    echo $this->image_lib->display_errors();                        
   }
   $source = base_url().'user/'.$u.'/Thumb/'.$filename;
   $data["avatar"] = $this->upload_model->update_photo($source);  
  }
  else   
  {       
   $this->load->view('profile_view',$data);     
  }       
 }     
}

查看-无法访问$avatar:

<div id="showimage" name='showimage'>
 <?php 
  foreach ($avatar as $row)
  {    
   echo $row;
  }
 ?>    
</div>

对于上面的控制器,您在if子句true部分定义$data["avatar"],但在else部分加载视图。它将如何。。。。

这就是它作为无分变量给出的方式。。

一旦检查您的控制器。

在控制器中

 class Upload_photo extends CI_Controller{
public $upload_model = "";
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));   
$this->load->model('upload_model');               
}
public function index()
{      
    session_start();
    if(isset($_FILES['file1']))
    {     
        $u = $_SESSION['username'];        
        $config['upload_path'] = 'user/'.$u;         
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '30000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';             
        $this->load->library('upload',$config);    
        $filename = $_FILES['file1']['name'];
        if(!$this->upload->do_upload('file1'))
        {
            echo "Error". $this->upload->display_errors();
            return;           
        }  
        $config = array();
        $config['image_library'] = 'gd2';
        $config['source_image']  = 'user/'.$u.'/'.$filename ;                
        $config['new_image']= FCPATH . 'user/'.$u.'/Thumb/';
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = FALSE;
        $config['width'] = 110;
        $config['height'] = 110;          
        $this->load->library('image_lib',$config);
        if(!$this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();                        
        }
        else   
        {       
            $source = base_url().'user/'.$u.'/Thumb/'.$filename;
            $data['avatar'] = $this->upload_model->update_photo($source);       
            $this->load->view('profile_view',$data);
        }  
    }     
}     
}

在视图中尝试此操作

<div id="showimage" name='showimage'>
<?php 
    isset($avatar){
    foreach ($avatar as $row)
    {    
    echo $row;
    }
    }
?>