Welcome Guest, Not a member yet? Register   Sign In
Paginate on Array
#1

[eluser]jay2003[/eluser]
Hi,

I am new to code igniter and am building a photo gallery for a client. So far it uploads multiple files using uloadify, creates a unique directory to upload the files to, creates a thumbnail and watermarks.

I how have a gallery page which loads a gallery and does so by doing the following;

Code:
public function get_images($dir_name)
{
  $files = scandir('./uploads/' . $dir_name);
  $files = array_diff($files, array('.', '..', 'thumbs'));
  
  $images = array();
  
  foreach ($files as $file)
  {
  $images[] = array (
  'url' => '/uploads/' . $dir_name . '/' . $file,
  'thumb_url' => '/uploads/' . $dir_name . '/thumbs/' . $file,
  'file_name' =>  $file
  );
  }
  
  return $images;  
  
}

This is all working fine and the gallery looks great but am now stuck at pagination. Ive seen the pagination built into code igniter used when interacting with databases but as I am just loading an array based on contents of a directory how would i go about adding pagination?

Any help would be much appreciated.

Thanks

Jason
#2

[eluser]InsiteFX[/eluser]
Basic Image Gallery With CodeIgniter
#3

[eluser]jay2003[/eluser]
Thanks very much
#4

[eluser]jay2003[/eluser]
Hi,

I created a photo gallery with pagination based on this code recommended by InsiteFX but have got a problem. It doesn't seem to tho show the files in any particular order.

Is it possible to get the images to display in alphabetical order?

The controller that does the work is as follows

Code:
$c_paginate['base_url'] = base_url(). 'gallery/index';
        $c_paginate['per_page'] = '21';
        $finish = $start + $c_paginate['per_page'];
  
  $this->data['dir']['thumb'] = 'uploads/' . $this->session_code  . '/thumbs/';
        $this->data['dir']['original'] = '/uploads/' . $this->session_code  . '/';
  
        if (is_dir($this->data['dir']['thumb']))
        {
            $i = 0;
            if ($dh = opendir($this->data['dir']['thumb'])) {
                while (($file = readdir($dh)) !== false) {
                    // get file extension
                    $ext = strrev(strstr(strrev($file), ".", TRUE));
                    if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png') {
                        if ($start <= $this->data['total'] && $this->data['total'] < $finish) {
                            $this->data['images'][$i]['thumb'] = $file;
                            $this->data['images'][$i]['original'] = str_replace('thumb_', '', $file);
                            $i++;
                        }
                        $this->data['total']++;
                    }
                }
                closedir($dh);
            }
        }

        $c_paginate['total_rows'] = $this->data['total'];

        $this->pagination->initialize($c_paginate);
  
  $this->load->model('gallery_model');
  $this->data['gall_info'] = $this->gallery_model->retrieve_gallery($this->session_code);
    
  $this->load->view('templates/header', $data);
  $this->load->view('gallery_view/gallery_view', $this->data);
  $this->load->view('templates/footer');

Any help would be much appreciated.

Thanks

Jason

#5

[eluser]InsiteFX[/eluser]
You would do that in your model.

You would use the active record order_by with asc or desc as the second parameter.
#6

[eluser]jay2003[/eluser]
There is no model as the controller is reading the contents of the folder in the controller

There is no reference to the individual images in the db ... so this is where im stuck.

Ive looked around and believe i need to use ksort but cant figure out where/how

Thanks

Jason
#7

[eluser]InsiteFX[/eluser]
SEE: PHP.NET - Sorting Arrays
#8

[eluser]jay2003[/eluser]
I've tried

Code:
$this->data['images'] = ksort($this->data['images']);

just before the paginate line and it just causes my page not to load ...

Any ideas?

Thanks

Jason
#9

[eluser]InsiteFX[/eluser]
Use sort, ksort is for associated arrays which you are not using.

Try this
Code:
$this->data['images'] = sort($this->data['images']);

You may need to adjust it with sort_flags
#10

[eluser]jay2003[/eluser]
I just get a blank screen when I try that too...




Theme © iAndrew 2016 - Forum software by © MyBB