我目前正在使用codeigniter開發一個交互式配置文件庫,每個用戶可以上傳他們自己的圖像部分。目前我的視圖頁面正在拋出一個消息:未定義的變量:圖像。儘管我已經在我的模型中有一個get_images函數,但在我的視圖中還有一個if語句,所以如果沒有已經上傳的圖像,就不會嘗試顯示圖像。我該如何解決這個問題?我也試圖存儲上傳到圖庫數據庫中的任何圖像,以便在每個用戶名旁邊顯示他們上傳的任何圖像。這是我到目前爲止的代碼:爲什麼我的畫廊視圖頁面會拋出錯誤?
控制器:
class Gallery extends CI_Controller {
function __construct()
{
// Call the parent construct
parent::__construct();
$this->load->model("profiles");
$this->load->model("gal_model");
$this->load->helper(array('form', 'url'));
$this->gallery_path = 'web-project-jb/assets/gallery';
$this->gallery_path_url = base_url().'web-project-jb/assets/gallery/';
}
function upload()
{
$config = array(
'allowed_types' =>'gif|jpg|jpeg|png',
'upload_path' => $this->gallery_path,
'max_size' => 10000,
'max_width' => 1024,
'max_height' => 768);
$this->load->library('upload', $config);
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data["full_path"],
'new_image' => $this->gallery_path. '/thumbs',
'maintain_ration' => true,
'width' => 150,
'height' => 100
);
$this->load->library("image_lib", $config);
$this->image_lib->resize();
$username = $this->session->userdata('username');
if (! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$username = $this->session->userdata('username');
$viewData['username'] = $username;
$this->load->view('shared/header');
$this->load->view('gallery/galtitle', $viewData);
$this->load->view('shared/nav');
$this->load->view('gallery/galview', $error, $viewData, array('error' => ' '));
$this->load->view('shared/footer');
}
else
{
$file_data = $this->upload->data();
$image = $this->gallery_path.$file_data['file_name'];
$data['image'] = $this->gallery_path.$file_data['file_name'];
$this->username = $this->session->userdata('username');
$this->gal_model->putGalleryImage($username, $image);
$this->session->set_userdata($image);
$viewData['username'] = $username;
$data['gal_model'] = $this->gal_model->get_images($username);
$username = $this->session->userdata('username');
$this->load->view('shared/header');
$this->load->view('gallery/galtitle', $viewData);
$this->load->view('shared/nav');
$this->load->view('gallery/galview', $data, $viewData);
$this->load->view('shared/footer');
}
}
function index()
{
$username = $this->session->userdata('username');
$this->load->library('upload');
$data['profileimages'] = $this->gal_model->get_images($username);
$file_data = $this->upload->data();
$file_data['file_name'] = $this->gal_model->get_images($username);
$image = $this->gallery_path.$file_data['file_name'];
$data['image'] = $file_data['file_name'];
$viewData['username'] = $username;
$this->load->view('shared/header');
$this->load->view('gallery/galtitle', $viewData);
$this->load->view('shared/nav');
$this->load->view('gallery/galview', $viewData, $data, array('error' => ' '));
$this->load->view('shared/footer');
}
}
型號:
class Gal_model extends CI_Model
{
var $gallery_path;
var $gallery_path_url;
function Gal_model()
{
parent::__construct();
$this->gallery_path = 'web-project-jb/assets/gallery';
$this->gallery_path_url = base_url().'web-project-jb/assets/gallery/';
}
function exists($username)
{
$this->db->select('*')->from("gallery")->where('user', $username);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
return true;
/*
echo "user $user exists!";
$row = $query->row();
echo " and his profileimage is $row->profileimage";
*/
}
else
{
return false;
//echo "no such user as $user!";
}
}
function putGalleryImage($username, $image)
{
$record = array('user' => $username, 'galleryimage' => $image);
$this->session->set_userdata($image);
if ($this->exists($username))
{
$this->db->where('user', $username)->update('gallery', $record);
}
else
{
$this->db->where('user', $username)->insert('gallery', $record);
}
}
function get_images($username)
{
$this->db->select('*')->from('gal_model')->where('user', $username);
$files = scandir($this->gallery_path);
$files = array_diff($files, array('.', '..', 'thumbs'));
$images = array();
foreach ($files as $file){
$images[] = array(
'url' => $this->gallery_path_url.$file,
'thumb_url' => $this->gallery_path_url.'thumbs/'.$file
);
}
return $images;
}
}
查看:再次
<?php if (is_array($images) && (count($images)>0)):
foreach($images as $image): ?>
<div class="thumb">
<a href="<?php echo $image['url']; ?>">
<img src ="<?php echo $image['thumb_url']; ?>"/>
</a>
<br>
</div>
<?php endforeach; else: ?>
<div id = "blank_gallery">Please upload an Image</div>
<?php endif; ?>
感謝所有幫助傢伙
和count &&!empty($ images),它將調用應該顯示爲預期的用戶圖像,除非應該在控制器中完成所有操作,而不是在視圖中有長條件鏈 –
count和!empty對於這兩個條件是沒有必要的...如果count == 0,那麼isset將返回false,如果它是空的,如!empty($ images)那麼isset將返回false。這2個是你需要的唯一2個。是的,邏輯應該在控制器中,但在將視圖作爲失敗保護顯示給用戶之前,視圖中始終存在一些錯誤檢查。抱歉,當我寫這個回覆時,我正在想別的東西。是的,你可以在if語句中添加count。在執行代碼之前,我仍然支持添加最終的回退檢查,以防萬一 – Rob
確實如此,foreach會捕獲計數檢查的缺失,所以您說得對,計數在條件中不是必需的。但isset和is_array都會爲空數組返回true。我傾向於將它指定爲假,除非人口稠密。所以我的條件是if($ images){...}但這些做法都不是特別錯誤,所以它是一種偏好。 –