iD Tech Camps

Save money on camp with this special offer

Search Our Site

The World's #1 Tech Camp

iD News and Blog

Request a Brochure

Randomizing your Flickr to vBulletin Feed

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:

require_once("includes/phpFlickr.php");

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

$f->enableCache("db","mysql://user:password@server/database");

$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.

May 7th, 2009 | Tags: , , , , , ,

Posted in: iD Tech Bloggers

3 Responses to Randomizing your Flickr to vBulletin Feed

  1. Just wanted to introduce myself, been a lurker on here for a while. Are there any subjects that we can’t talk about on here?

  2. hildDiedy on May 13th, 2009 at 6:11 pm
  3. Hi, Welcome hildDiedy. We are a kid’s technology company, so anything appropriate for children and teens is fine.

  4. KenK on May 19th, 2009 at 8:31 am
  5. Hey Ken,

    Since the launch of vb4 the way hooks used to work has changed. I am not able to get this plugin to work. Can you tell what all do I need to read to do it myself.

    I am trying to get these two to work
    http://www.vbulletin.org/forum/showthread.php?t=119488&highlight=flickr
    http://www.vbulletin.org/forum/showthread.php?t=210763&highlight=flickr

  6. RHL on January 2nd, 2010 at 10:52 am

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This entry was posted on Thursday, May 07th at 11:08 am and is filed under iD Tech Bloggers. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Blog Categories

ACCED-I Meeting Exelence On CampusThe World's Best Summer Camps

Blog Archives

CEO's Blog

Tech Bloggers