jQuery Flickr Loader

I've created an image gallery that pulls in images from Flickr. Initially, I have 5 images loaded and have added a button underneath labelled "Load More". I basically need to adjust my code so that when I click the button it gives me the next 5 images (right now, it just loads the same 5 images). I tried creating a counter variable that adds on 5 each time the button is clicked but couldn't get it to work. Any help would be greatly appreciated. You can check out my code here: http://bit.ly/1aReYxl

Thanks in advance!
LVL 1
AdamAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
leakim971Connect With a Mentor PluritechnicianCommented:
You get all the pictures
As the API did not mention how to get only a part of them, you need to work with your own counter :
http://www.flickr.com/services/feeds/docs/photos_public/

Test page : http://jsfiddle.net/8xyeM/2/
var pics = [];
var counter = 0;
var maxi = 5;

function loadFlickr()  {
    var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
    $.getJSON(flickerAPI, {
        tags: "fart",
        tagmode: "any",
        format: "json"
    })
        .done(function (data) {
            pics = data.items;
            addPicture(counter);
        });
}

loadFlickr();

function addPicture(mycounter) {
    for(var i=0;i<maxi&&pics[i+counter];i++) {
        var item = pics[i+counter];
                var flickrGallery = '<div class="item clearfix">';
                flickrGallery += '<a class="image-link" href="' + item.media.m.replace('_m', '_b') + '">';
                flickrGallery += '<img src="' + item.media.m.replace('_m', '_n') + '" alt="' + item.title + '" /></a><div class="item-meta">' + item.title + '</div></div>';
                $('#container').append(flickrGallery);
                if (i === 5) {
                    return false;
                }
    }
    counter += maxi;
}

$( ".load-more" ).click(function() {
    if(counter<pics.length) addPicture(counter);
});

Open in new window

0
 
RobOwner (Aidellio)Commented:
You have to do the paging yourself.  The feed will return all matches to your search so store the entire response in an array and when you want to load more just show the next 5.
0
 
RobConnect With a Mentor Owner (Aidellio)Commented:
Here's what i mean: http://jsbin.com/uYUmIwi/1/edit.  Scroll down to the end and click the "load more" button.
I added a function to loadmore and modified your loadFlikr function to assign the feed contents to an array.  The loadmore function then just uses this array to load the next items.

var index = 1;
var num = 5;
var photos = [];
function loadFlickr()  {
  var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
  $.getJSON(flickerAPI, {
    tags: "fart",
    tagmode: "any",
    format: "json"
  })
  .done(function (data) {
    photos = data.items;
    
    $.each(data.items, function (i, item) {
      
      var flickrGallery = '<div class="item clearfix">';
      flickrGallery += '<a class="image-link" href="' + item.media.m.replace('_m', '_b') + '">';
      flickrGallery += '<img src="' + item.media.m.replace('_m', '_n') + '" alt="' + item.title + '" /></a><div class="item-meta">' + item.title + '</div></div>';
      $('#container').append(flickrGallery);
      if (i === num) {
        index = i;
        return false;
      }
    });
    
    
  });
}

loadFlickr();

$( ".load-more" ).click(function() {
  loadMore();
});

function loadMore() {
  $.each(photos, function (i, item) {
    if (i >= index) {
    var flickrGallery = '<div class="item clearfix">';
    flickrGallery += '<a class="image-link" href="' + item.media.m.replace('_m', '_b') + '">';
    flickrGallery += '<img src="' + item.media.m.replace('_m', '_n') + '" alt="' + item.title + '" /></a><div class="item-meta">' + item.title + '</div></div>';
    $('#container').append(flickrGallery);
    }
    if (i === (index+num)) {
      index = i;
      return false;
    }
  });
}

Open in new window

0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
RobOwner (Aidellio)Commented:
Leakim971 - Great minds think alike ;)
0
 
leakim971PluritechnicianCommented:
:))
0
 
AdamAuthor Commented:
Thanks fellas. Didn't know who to gave the points to so I split them down the middle. Hope that's cool! Cheers.
0
 
RobOwner (Aidellio)Commented:
That was the fairest thing to do. I'm happy that we didn't cause confusion and both said the same thing!
0
 
AdamAuthor Commented:
This may be a little late but I notice that after hitting "Load more" a few times on @tagit's example, it keeps loading the same images. On @leakim971's example the button just stops working. I'm assuming this has to do with some sort of limit on how many photos you can call from the Flickr API? Or am I overlooking something? If this is a much bigger issue, I can always open up a new question.
0
 
RobOwner (Aidellio)Commented:
That feed only gives you the last 20 uploads for that particular tag.  If you want more than this then you'll need to look at the api and use a server side script to do the rest

http://www.flickr.com/services/api/flickr.photos.search.html
0
 
AdamAuthor Commented:
Crap! Alright, thanks @tagit. I'll have to look into it further.
0
 
RobOwner (Aidellio)Commented:
Yeah i know... you'll need to get an api key, but the server side just needs to relay the request so it would be fairly simple
0
 
AdamAuthor Commented:
Thanks @tagit. Looking at it a bit closer though, it looks as if the way that the Flickr call is constructed using the API key is a bit different than the method I've been using. Unfortunately, I can't seem to get it to work using the approach you guys suggested. I'm also not that advanced with JS so it could be I'm just approaching it all wrong... :(

If you think you'd be able to lend a hand, I can open up a new question to give you some extra points.
0
 
RobOwner (Aidellio)Commented:
Actually you can call the REST api!! : http://jsbin.com/uYUmIwi/3/edit

var index = 0;
var num = 5;
var photos = [];
function loadFlickr()  {
  index++;
  var flickerAPI = "http://api.flickr.com/services/rest/?jsoncallback=?";
  $.getJSON(flickerAPI, {
    tags: "fart",
    tag_mode: "any",
    format: "json",
    method:"flickr.photos.search",
    api_key:"2bfe2a46e8884e2bcd7cb20be75b7db6",
    extras:"url_m",
    page: index,
    per_page: num
  })
  .done(function (data) {
    console.dir(data);
    photos = data.photos.photo;
    
    $.each(data.photos.photo, function (i, item) {
      
      var flickrGallery = '<div class="item clearfix">';
      flickrGallery += '<a class="image-link" href="' + item.url_m + '">';
      flickrGallery += '<img src="' + item.url_m + '" alt="' + item.title + '" /></a><div class="item-meta">' + item.title + '</div></div>';
      $('#container').append(flickrGallery);
    });
    
    
  });
}

loadFlickr();

$( ".load-more" ).click(function() {
  loadFlickr();
});

Open in new window

0
 
RobOwner (Aidellio)Commented:
My last post should do the trick for you - no need for extra points :)
0
 
AdamAuthor Commented:
Wow, that was extremely generous of you! This is fantastic. Definitely does the trick. Thank you so much @tagit. I owe you one... ;)
0
 
RobOwner (Aidellio)Commented:
No problem :) thats what we're here for.  I still suggest you apply for your own api key as the one in the demo may expire at any time and/or be restricted somewhat.
0
 
AdamAuthor Commented:
Already done! I'll replace the one you used with mine. Thanks again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.