Php 我无法在codeigniter中访问视图中的控制器阵列

Php 我无法在codeigniter中访问视图中的控制器阵列,php,arrays,codeigniter,Php,Arrays,Codeigniter,我无法在codeigniter中访问视图中的控制器阵列。当我在我的视图中传递数组时,它会给我未知的变量错误,请帮助我!此代码用于更改配置文件pic 我想在我的视图中显示更改的图片!我可以获取从模型到视图的照片路径,但无法从控制器访问到视图 我的密码在这里 型号: class Upload_model extends CI_Model { public function __construct() { parent::__construct(); $this->load->

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

我想在我的视图中显示更改的图片!我可以获取从模型到视图的照片路径,但无法从控制器访问到视图

我的密码在这里

型号:

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;
    }
    }
?> 


您是否能够从数据库中获取数据???是!!我已通过打印($data)将其签入控制器;是的,你说得对,先生!我已经纠正了这一点,并将$this->load->view('profile\u view',$data)设置为true,但没有结果,先生,如果唯一的条件为false,我想加载视图!当条件为真时,我无法获取数据,但我不知道如何在视图中访问,但是先生-当我在视图中写入$avatar时,我的编辑器显示警告我”变量似乎未初始化cant use$avatar,因为编辑器错误地向我显示未识别的对象打印结果是什么($data)消息:未定义变量:avatar Filename:views/profile_view.php行号:160$data[“avatar”]更改为$data['avatar']