我无法在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>
答案 0 :(得分:0)
对于上面的控制器,你在if子句中定义了$ data [“avatar”],但你在else部分加载了视图。怎么会......
这就是它作为未被变换的变量给出的方式..
检查你的控制器。
答案 1 :(得分:0)
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;
}
}
?>