2012-12-11 25 views
0

嗨再次:)我試圖讓每個用戶能夠上傳自己的版本的畫廊自己的個人資料,但是當我做同一個畫廊出現在每個配置文件。作爲嘗試這種做同樣的畫廊不僅出現,但我也得到了這個錯誤的結果: 嚴重性:警告我如何讓我的畫廊爲每個用戶看起來不同?

消息:的foreach()

我也瞄準加載每個提供的參數無效庫圖像成一個數據庫和圖像中的每個用戶一個畫廊已經在我的文件上傳目錄

這裏正確貯存是我的畫廊控制器:

class Gallery extends CI_Controller { 



function index() 
{ 
    $username = $this->session->userdata('username'); 
    $image = $this->session->userdata('images'); 

    $this->load->model("gal_model"); 

    if($this->input->post('upload')) 
    { 

     $this->gal_model->do_upload(); 
     $this->gal_model->putGalleryImage($username, $image); 

    } 




    $viewData['username'] = $username; 

    $data['images'] = $this->gal_model->get_images(); 
    //var_dump($data); 

    $this->load->view('shared/header'); 
    $this->load->view('gallery/galtitle', $viewData); 
    $this->load->view('shared/nav'); 
    $this->load->view('gallery/galview', $data); 
    $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 do_upload() 
{ 
    $config = array(
    'allowed_types' => 'gif|jpg|jpeg|png', 
    'upload_path' => $this->gallery_path, 
    'max_size' => 10000  
      ); 


    $this->load->library("upload", $config); 
    $this->upload->do_upload(); 
    $image_data = $this->upload->data(); 


    $image = 'gallery_path'.$image_data['file_name']; 

    $data['image'] = 'gallery_path'.$image_data['file_name']; 


    $username = $this->session->userdata('username'); 

    $this->gal_model->putGalleryImage($username, $image); 

    $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(); 
} 


function get_images() 
{ 
    $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; 
} 



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); 

    } 

    } 

    } 

這裏是我的相冊查看:

<div id="gallery"> 

    <?=if (isset($images)): 
    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; ?> 

<?=form_open_multipart('gallery');?> 
    <?=form_upload("userfile");?> 
    <?=form_submit('upload', 'Upload')?> 
    <?=form_close();?> 

再次感謝您的幫助球員,我只需要知道正確的方式出去這:)。

+0

你的getImages實際上是否返回任何東西? var $ gallery_path; var是一種JavaScript的做事方式,我不是最有知識的PHP傢伙,但我從來沒有見過它在PHP中使用。通常當聲明變量是Public,Private或Protected而不是var時。 –

+0

@RickCalder var'關鍵字用於[聲明PHP 4中的類中的變量](http://docs.php.net/manual/en/keyword.class.php): –

+0

啊,謝謝你。 –

回答

0

Invalid argument supplied for foreach()如果您嘗試對不是數組的變量執行foreach,可能會發生。對於去除警告信息更新圖庫視圖:

<div id="gallery"> 
    <?=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;?> 
    <?php else: ?> 
     <div id = "blank_gallery">Please upload an Image</div> 
    <?php endif; ?> 
<div> 

我不熟悉的CI,但你在哪裏的$images從獲取值?

+0

我說的$文件變量嗎?感謝您的反饋 :)。是否正確地說每個都不是數組,因爲我一次會上傳oen文件? –

+0

我想你是對的。根據CI文檔,如果我們將$ data ['price']傳遞給視圖,我們可以在視圖中使用$ price;如果我們傳遞$ data ['description'],我們可以在視圖中使用$ description等[[link](http://www.ntchosting.com/php/frameworks/codeigniter/#Views))引用它。 var_dump($ data)'的結果是什麼? –

+0

實際上'$ data ['images']'因爲您在'function get_images()'中設置了'$ images = array();',所以總是一個數組,因此您只需檢查數組的數量是否大於零。 –

相關問題