douchen4915 2015-11-28 06:19
浏览 36

我无法在codeigniter中查看我的控制器数组

I can't access my controller array in my view in codeigniter. When I pass array in my view it give me unidentified variable error, please help me! This code is for change profile pic.

I want to display changed pic to my view! I can get photo path from my model to my view but can't access it to my view from my controller

My code is here

Model:

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

Controller:

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

View - Could not access $avatar:

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

2条回答 默认 最新

  • dongqie2028 2015-11-28 06:41
    关注

    for in above controller you are defining $data["avatar"] in if clause true part, but you are load the view in else part. how it will take....

    thats way it given as undifened variable..

    once check your controller.

    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程