Hi all..
I am working on a website which require me to handle the image uploading and retrieval.
This website is some kind like an image gallery.
Straight to the point, i am having trouble in retrieving the last image to be displayed on my view.Clearly to be said, i capable of retrieving the first image from my image directory file, but when i want to retrieve the last image, my code fails to works.
This is the code of my controller :
<?php
class Gallery extends CI_Controller{
function index(){
$this->load->model('Gallery_model');
if($this->input->post('upload')){
$this->Gallery_model->do_upload();
}
$data['images'] = $this->Gallery_model->get_images();
$this->load->view('gallery',$data);
}
}
This is my model for retrieving data from database :
<?php
class Gallery_model extends CI_Model{
var $galery_path;
var $gallery_path_url;
function Gallery_model(){
parent::__construct();
$this->gallery_path = realpath(APPPATH . '../events_images');
$this->gallery_path_url = base_url().'events_images/';
}
function do_upload() {
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000
);
$this->load->library('upload',$config);
$this->upload->do_upload();
$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();
}
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;
}
}
And this is the view where i need to display the latest image uploaded by user:
<div id="gallery">
<?php if (isset($images) && count($images)): ;
$count = count($images); // i try to get the last image
?>
<div class="thumb">
<a href="<?php echo $images[$count]['url']; ?>"> // and this is my problem
<img src="<?php echo $images[$count]['thumb_url']; ?>" />
</a>
</div>
<?php else: ?>
<div id="blank_gallery">Please Upload an Image</div >
<?php endif; ?>
</div`>
If you noticed, i am using $count = count($images); to get the number of image so that i could use it to retrieve the last image by writing this :
<img src="<?php echo $images[$count]['thumb_url']; ?>" />
but it turns out to be an error, why is that?
and how could i correct this?
Help me please.
Thank you in advance :)