簡體   English   中英

我無法在Codeigniter的視圖中訪問控制器陣列

[英]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;
    }
    }
?> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM