Welcome Guest, Not a member yet? Register   Sign In
Next/Previous Photo
#1

[eluser]JamieBarton[/eluser]
Hello,

I'm in the middle of creating a photo gallery in CI.

I'm wanting to have a link to view the next and previous photo in the album. Obviously all of the photos will not have IDs that are a number below/above the current, so it is not as easy as adding or subtracting 1 from the current ID to move around in the album.

Could somebody explain to me how I'd go about doing this? I think I could use limits..

I'm not 100% sure.
#2

[eluser]jedd[/eluser]
[quote author="Jamie B" date="1260684904"]
I'm in the middle of creating a photo gallery in CI.

I'm wanting to have a link to view the next and previous photo in the album. Obviously all of the photos will not have IDs that are a number below/above the current, so it is not as easy as adding or subtracting 1 from the current ID to move around in the album.
[/quote]

I think you'd need to calculate the next ID by taking the current one, multiply by 27, subtract your birthday (if it's a prime number only - otherwise subtract only half). Calculating the prev ID will be a bit trickier, and depends on whether you're in the northern or southern hemisphere.

Quote:Could somebody explain to me how I'd go about doing this? I think I could use limits..

Seriously .. you need to explain your schema, how you are indexing your photos, what 'next' and 'previous' actually mean in this context - that is, how do you know what the next photo should be - and then we'll use that heuristic to come up with an algorithm for your application to work out how to identify the next photo.
#3

[eluser]megabyte[/eluser]
Although I like Jedd's answer better (because too many people are way too serious, and I hate serious) this is what I'd do.

Think of it like this.

You do a db query and get a result set with 25 photos. The next photo will have an offset of 1, then 2, then 3. So you just need to use an offset and limit in your db query, and use the url to store the current offset.
#4

[eluser]jedd[/eluser]
You're too kind. Smile

[quote author="megabyte" date="1260692805"]
The next photo will have an offset of 1, then 2, then 3. So you just need to use an offset and limit in your db query, and use the url to store the current offset.[/quote]

I gather the problem here is because Jamie revealed <sic> this:
[quote author="Jamie B" date="1260684904"]
Obviously all of the photos will not have IDs that are a number below/above the current, so it is not as easy as adding or subtracting 1 from the current ID to move around in the album.
[/quote]

Obviously the problem is that what is obvious to Jamie, who has the schema, the application design, etc in front of him and in his head - is not necessarily obvious to those of us who are not Jamie.

Why you can't find the next number by adding one to the current number is indeed a mystery at the moment. Until that's explained it's hard to come up with an algorithm that provides the same effect.

FWIW I only popped in here because I already have a solution to a comparable problem. My situation is probably slightly different - I have a set of photo information that is pulled into my app on every page load as two similar-looking arrays. The primary key in the array is an image ID, which is a 10-character hex string - this makes some things easier, but means I can't easily do next/prev just by changing an index (array key) value. The two arrays are a full set of all images, and a subset of images. The subset is determined by zero or more filters. Filters apply to tags. A photo can have zero or more tags. Users can nominate zero or more filters. I need to display a set of x (default 8) thumbnails that surround the current photo, and provide next/prev button for the current photo. Clearly it's not rocket science, but there's a couple of patterns you could apply here. I'm happy with my way (despite it not being hugely elegant) because it's a low-load site on a reasonably powerful server, I'll likely never have more than a couple of thousand images being tracked, my relevant data is always available in memory (rather than having to be ascertained from IO calls).
#5

[eluser]eokorie[/eluser]
Hi Jamie,

I recently had the same issue and came up with this solution. Seems to work very well especially when your id's are not numbered sequentially.

In my model, I have this:

Code:
function _display_next_photo($cat_id, $id_pho)
{
    //$user = trim($user);            
    $gk_DetailPhoto_id_pho = 0;                    
    $this->db->select('MIN(id_pho) as id_pho, MAX(id_pho) as LastRecord');
    $this->db->where('id_pho > ',$id_pho);
    $this->db->where('catid_pho',$cat_id);
    $query = $this->db->get('ks_photos');    
    if ($query->num_rows() === 1)
    {
        return $query->row();
    } else {
        return false;
    }
}
        
function _display_previous_photo($cat_id, $id_pho)
{
    //$user = trim($user);            
    $gk_DetailPhoto_id_pho = 0;            
    $this->db->select('MIN(id_pho) as id_pho, MAX(id_pho) as PrevRecord');
    $this->db->where('id_pho < ',$id_pho);
    $this->db->where('catid_pho',$cat_id);            
    $query = $this->db->get('ks_photos');            
    if ($query->num_rows() === 1)
    {
            return $query->row();
    } else {
            return false;
    }
}

Then in my Controller, I define the required navigation like this:

Code:
// Photo Detail Page Navigation
$data['first_photo'] = $this->photos_model->_display_previous_photo($this->uri->segment(5), $this->uri->segment(7))->id_pho;
$data['next_photo'] = $this->photos_model->_display_next_photo($this->uri->segment(5), $this->uri->segment(7))->id_pho;
$data['previous_photo']    = $this->photos_model->_display_previous_photo($this->uri->segment(5), $this->uri->segment(7))->PrevRecord;
$data['last_photo'] = $this->photos_model->_display_next_photo($this->uri->segment(5), $this->uri->segment(7))->LastRecord;

Finally in my view, I use the following code to display the navigation that I want:

Code:
&lt;?php if ($previous_photo != '') { echo anchor(base_url().'photos/category/'.$this->uri->segment(3).'/list/'.$this->uri->segment(5) .'/photo/'.$previous_photo, '&laquo; Previous Photo'); } ?&gt;
&lt;?php if ($next_photo != '') { echo anchor(base_url().'photos/category/'.$this->uri->segment(3).'/list/'.$this->uri->segment(5) .'/photo/'.$next_photo, 'Next Photo &raquo;'); } ?&gt;


Hope this helps.
#6

[eluser]jedd[/eluser]
Come on Jamie - spill!

(I'm consumed with curiosity.)




Theme © iAndrew 2016 - Forum software by © MyBB