Randomizing your Flickr to vBulletin Feed

iD Tech in action

Last time I discussed pulling your Flickr feed into a vBulletin installation (or really, any php-based CMS/website). That solution pulled the lastest photos up to some amount and displayed them. But what if you want to randomize them and not always display the pictures in the same order? We'll be pulling a large amount of photos from Flickr and I don't always want only the last ones to show up in the same order. Instead, I'd like to pull the last, say, 200 photos and randomly display 10 of them. Here's the code modification from last week's post:


// Create new phpFlickr object
$f = new phpFlickr("yourapikey");


$limit = 200;
$count = 10;

// Find the NSID of the username inputted via the form
$person = $f->people_findByUsername('iDTechCamp');

// Get the friendly URL of the user's photos
$photos_url = $f->urls_getUserPhotos($person['id']);

// Get the user's first X($limit) public photos
$photos = $f->people_getPublicPhotos($person['id'], NULL, NULL, $limit);

$flickrbox = "<table class='flickrbox'><tr>";
$nonrepeatarray = array();

for ($i = 0; $i < $count; $i++) {
    $rand = rand(0,199);
    while(in_array($rand,$nonrepeatarray)) $rand = rand(0,199);
    array_push($nonrepeatarray, $rand);

    $photo = (array)$photos['photos']['photo'][$rand];
    $flickrbox .= "<td><a class='flickrimg' href=$photos_url$photo[id]>";
    $flickrbox .= "<img border='0' alt='$photo[title]' "
               ."src=" . $f->buildPhotoURL($photo, "Square") . ">";
    $flickrbox .= "</a></td>";

$flickrbox .= "</tr></table>";

You can see I've highlighted the altered sections in red. First, we up the $limit to 200. Then we introduce a new variable, $count. This variable will contain the number of photos we'd like displayed of the 200 we're pulling. Next we change our foreach loop into a regular for loop. We will then randomly generate a number between 0 and 199 (remembering that arrays begin at 0, not 1). We run a while loop to check if that number is already in an array we've been keeping called $nonrepeatarray. If it is there, we draw another number. If it isn't, we exit that loop, add it to the array, and keep moving with the portion of the loop dedicated to building the image display.

Although this may not be the most efficient method, it is simple. Plus, we have caching enabled through the phpFlickr API, so that will help. Other ways of doing the exact same thing include generating an array containing the values 0-199, shuffling the array, and then popping off one element each time. You are guaranteed not to have repeats.

For other methods of generating random, non-repeating numbers, with sample code, see here: http://www.phpbuilder.com/board/showthread.php?t=10329337.

A photo of Ryan

Ryan manages blog content at iD Tech, starting with the company in 2008. He earned his MBA from Santa Clara University after obtaining his Bachelor’s degree from Arizona State. Connect on LinkedIn!

Featured Posts


About iD Tech

iD Tech is the #1 tech camp on the planet, with 150+ locations nationwide. Kids and teens learn to code, design video games, produce videos, mod Minecraft, create with Roblox, engineer robots, model and print 3D characters, work with AI and machine learning, build laptops, learn about cybersecurity, and more!

We've bet our reputation on recruiting the top instructors in the country. Our 8:1 student-to-instructor average ensures customized learning with the smallest class sizes, leading to "a-ha moments" and awesome outcomes.

View all locations

View all courses