2012-12-18 36 views
-2

我目前正在使用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; ?> 

感謝所有幫助傢伙

回答

0

你應該改變你的,如果你以

if(isset($images) && is_array($images)) 

這樣,如果在不確定的$圖像就會n要拋出一個錯誤,它只是跳過

+0

和count &&!empty($ images),它將調用應該顯示爲預期的用戶圖像,除非應該在控制器中完成所有操作,而不是在視圖中有長條件鏈 –

+0

count和!empty對於這兩個條件是沒有必要的...如果count == 0,那麼isset將返回false,如果它是空的,如!empty($ images)那麼isset將返回false。這2個是你需要的唯一2個。是的,邏輯應該在控制器中,但在將視圖作爲失敗保護顯示給用戶之前,視圖中始終存在一些錯誤檢查。抱歉,當我寫這個回覆時,我正在想別的東西。是的,你可以在if語句中添加count。在執行代碼之前,我仍然支持添加最終的回退檢查,以防萬一 – Rob

+0

確實如此,foreach會捕獲計數檢查的缺失,所以您說得對,計數在條件中不是必需的。但isset和is_array都會爲空數組返回true。我傾向於將它指定爲假,除非人口稠密。所以我的條件是if($ images){...}但這些做法都不是特別錯誤,所以它是一種偏好。 –

1

正確我如果我錯了她e,但看起來你從不會將$圖片分配給視圖。您將$image$profileimages分配到index(),但我不能看到$images

+0

感謝所有一起指出了這一點凱我把它放在我的index() - $ images = $ this-> session-> userdata('images'); $ data ['images'] = $ images; –

+0

@Hannah_B然後你應該發佈實際的代碼。 – jeroen

+0

@Hannah_B - 你有沒有嘗試var_dump($圖像),以確保數組中有任何?另外,我不會將圖像數組存儲在會話中。我會把它留給模型來處理。如果您擔心不必要的查詢,則可以緩存庫並將平面文本分配給基於用戶標識的文件名。 –

相關問題