开发者

PHPFlickr script...could this be cleaner/leaner?

OK, here's my dilemma:

I've read all over about how many guys want to be able to display a set of images from Flickr using PHPFlickr, but lament on how the API for PhotoSets does not put individual photo descriptions. Some have tried to set up their PHP so it will pull the description on each photo as the script assembles the gallery on the page. However, the method has shown how slow and inefficient it can be.

I caught an idea elsewhere of creating a string of comma separated values with the photo ID and the description. I'd store it on the MySQL database and then call upon it when I have my script assemble the gallery on the page. I'd use explode to create an array of the photo ID and its description, then call on that to fill in the gaps...thus less API calls and a faster page.

So in the back-end admin, I have a form where I set up the information for the gallery, and I hand a Set ID. The script would then go through and make this string of separated values ("|~|" as a separation). Here's what I came up with:

include("phpFlickr.php");
$f = new phpFlickr("< api >");

$descArray = "";

// This will create an Array of Photo ID from the Set开发者_开发百科 ID.
// $setFeed is the set ID brought in from the form.
$photos = $f->photosets_getPhotos($setFeed);
foreach ($photos['photoset']['photo'] as $photo) {
    $returnDesc = array();
    $photoID = $photo['id'];
    $rsp = $f->photos_getInfo($photoID);
    foreach ($rsp as $pic) {
        $returnDesc[] = htmlspecialchars($pic['description'], ENT_QUOTES);
    }
    $descArray .= $photoID."|~|".$returnDesc[0]."|~|";
}

The string $descArray would then be placed in the MySQL string that puts it into the database with other information brought in from the form.

My first question is was I correct in using a second foreach loop to get those descriptions? I tried following other examples all over the net that didn't use that, but they never worked. When I brought on the second foreach, then it worked. Should I have done something else?

I noticed the data returned would be two entries. One being the description, and the other just an "o"...hence the array $returnDesc so I could just get the one string I wanted and not the other.

Second question is if I made this too complicated or not. I like to try to learn to write cleaner/leaner code, and was looking for opinions.

Suggestions on improvement are welcome. Thank you in advance.


I'm not 100% sure as I've just browsed the source for phpFlickr, and looked the the Flickr API for the getInfo() call. But let me have a go anyway :)

First off, it looks like you shouldn't need that loop, like you mention. What does the output of print_r($rsp); look like? It could be that $rsp is an array with 1 element, in which case you could ditch the inner loop and replace it with something like $pic = $rsp[0]; $desc = $pic['description'];

Also, I'd create a new "description" column in your database table (that has the photo id as the primary key), and store the description in their on its own. Parsing db fields like that is a bit of a nightmare. Lastly, you might want to force htmlspecialchars to work in UTF8 mode, cause I don't think it does by default. From memory, the third parameter is the content encoding.

edit: doesn't phpFlickr have its own caching system? Why not use that and make the cache size massive? Seems like you might be re-inventing the wheel here... maybe all you need to do is increase the cache size, and make a getDescription function:

function getDescription ($id)
{
    $rsp = $phpFlickr->photos_getInfo ($id);
    $pic = $rsp[0];
    return $pic['description'];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜