0

我在显示存储在服务器 (/uploads) 中的图像时遇到问题,并且字段的名称存储在数据库中并与用户 ID 链接。

我的控制器:

function do_upload()
{
if($this->session->userdata('is_logged_in'))
{
$id = $this->session->userdata('id');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '100';
$config['max_width']  = '1024';
$config['max_height']  = '768';
$config['overwrite'] = false;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view('upload_form', $error);
}else
{    
$data = $this->upload->data();
$file_array = $this->upload->data('file_name');
$profile['profile_picture'] = $file_array['file_name'];
$this->db->where('id', $id);
$this->db->update('users',  $profile);
$data['img']= base_url().'/uploads/'.$data['file_name'];
$this->load->view('upload_success', $data);
}


public function members(){
if($this->session->userdata('is_logged_in')){
   $this->load->model('loginmodel');
   $data = $this->loginmodel->getImage();
   print('/uploads/'.$data);
   $img = base_url().'/uploads/'.$data;
   $this->load->view("site_header");
   $this->load->view("site_nav");
   $this->load->view("members");
   $this->load->view("site_footer");

}else{.....

模型

function getImage()
{
$id = $this->session->userdata('id');
$this->db->select("*");
$this->db->from('users');
$this->db->where('id', $id);
$query = $this->db->get();
if($query->num_rows()==0)
echo("Picture not found!");
else{
$data = $query->row_array();
return $data['profile_picture'];
 }
}

看法:

<img src="<?php echo $img; ?>" border="0" width="10" height="10">

我收到此错误:遇到 PHP 错误 严重性:通知消息:未定义变量:img 文件名:views/members.php 行号:19 "border="0" width="10" height="10">

4

2 回答 2

0

将数据传递给视图。

$data = array('img' => $img);
$this->load->view("members", $data);
于 2013-04-15T14:39:06.600 回答
0

您的查询可能不会返回数据,因为可能没有匹配项尝试在您的视图中使用 php 的 isset 函数

于 2013-04-15T14:44:24.527 回答