简体   繁体   中英

I cant access my controller array in my view in 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>

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.

in 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();                        
        }
        else   
        {       
            $source = base_url().'user/'.$u.'/Thumb/'.$filename;
            $data['avatar'] = $this->upload_model->update_photo($source);       
            $this->load->view('profile_view',$data);
        }  
    }     
}     
}

try this in view

<div id="showimage" name='showimage'>

<?php 
    isset($avatar){
    foreach ($avatar as $row)
    {    
    echo $row;
    }
    }
?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM