Setelah beberapa waktu lalu kita bermain dengan fungsi upload, selanjutnya adalah untuk menampilkan data tersebut.
Wah karena masih nuibe, mohon maaf kalo masih bingung ya π semoga tidak tambah bingung π
Sekarang Ubahlah controller gallery yang telah dibuat sebelumnya, sehingga menjadi seperti berikut :
load->model('MGallery'); if($this->input->post('upload')){ $this->MGallery->do_upload(); } $data['images'] = $this->MGallery->get_images(); $this->load->view('gallery_view',$data); } }
Controller di atas ditambahkan baris untuk memanggil fungsi get_images setelah proses upload. Sedangkan fungsi get_images pada model Mgallery adalah sebagai berikut :
gallery_path = realpath(APPPATH . '../images'); $this->gallery_path_url = base_url().'images/'; } function do_upload(){ ... //fungsi do_upload yang sudah dibuat terdahulu ... } 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; } }
Sedangkan pada view gallery_view dimodifikasi untuk menampilkan data dari fungsi get_images :
Gallery With CI <!-- #gallery, #upload{ border: 1px solid #ccc; margin: 10px auto; width: 600px; padding: 10px; } #blank_gallery{ font_family: Arial; font-size: 18px; font-weight: bold; text-align: center; } .thumb{ float: left; width: 160px; height: 120px; padding: 10px; margin: 10px; background-color: #ddd; } .thumb:hover{ outline: 1px solid #999; } img{ border:0; } #gallery:after{ content: "."; visibility:hidden; display: block; clear: both; height: 0; font-size: 0; } --> <div id="gallery"> <?php foreach($images as $img){ ?> <div class="thumb"> <a href=" <?php echo $img['url']; ?> "> <img src=" <?php echo $img['thumb_url']; ?>" alt="" /> </div> <?php } ?> </a> </div> <div id="upload"> </div>
Sekarang cobalah aplikasi tersebut, semoga berhasilβ¦
Jika ingin melihat versi video nya, silahkan lihat di :
http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-file-uploading-and-image-manipulation/