Question

I am working with the Yahoo BOSS API to build an image search for motherpipe.co.uk.

I have managed to create a valid request for image listings and have received the response. My problem is that I don't understand how I can use the different elements of that response to build my nice-looking page of image listings.

Ideally I want to loop through the array and display a thumbnail and link for each item in the list but somehow I can't abstract the relevant bits from the $results.

The (example) output from the query with two listings is in this $results:

stdClass Object ( [bossresponse] => stdClass Object ( [responsecode] => 200 [images] => stdClass Object ( [start] => 0 [count] => 2 [totalresults] => 107000 [results] => Array ( [0] => stdClass Object ( [clickurl] => htt://library.thinkquest.org/07aug/01105/Sweden/stockholm.jpg [size] => 191.8KB [format] => jpeg [height] => 586 [refererclickurl] => htt://library.thinkquest.org/07aug/01105/Sweden/sweden_home.html [refererurl] => htt://library.thinkquest.org/07aug/01105/Sweden/sweden_home.html [title] => Stockholm is a beautiful city with Lake Mälaren on it’s WestSide ... [url] => http://library.thinkquest.org/07aug/01105/Sweden/stockholm.jpg [width] => 793 [thumbnailheight] => 118 [thumbnailurl] => htt://ts4.mm.bing.net/th?id=H.4970051277687231&pid=15.1&H=118&W=160 [thumbnailwidth] => 160 ) [1] => stdClass Object ( [clickurl] => http://summerventures.files.wordpress.com/2012/01/429c_stockholm_ch.jpg [size] => 2.3MB [format] => jpeg [height] => 1272 [refererclickurl] => htt://summerventures.wordpress.com/tag/stockholm/ [refererurl] => htt://summerventures.wordpress.com/tag/stockholm/ [title] => stockholm | Summer adventures [url] => htt://summerventures.files.wordpress.com/2012/01/429c_stockholm_ch.jpg [width] => 1800 [thumbnailheight] => 113 [thumbnailurl] => htt://ts2.mm.bing.net/th?id=H.4581116279128437&pid=15.1&H=113&W=160 [thumbnailwidth] => 160 ) ) ) ) ) 

Question:

What approach can I use to simply display a thumbnail picture with a link from each of the listings in this array, using the info in [thumbnailurl] and [refererclickurl] kind of like

<div> <<a href="[refererclickurl]"><img src="[thumbnailurl]" alt="alt text" /> </div>

I think I need a for each approach but after having tried for three days I can't make it work.

Any help much appreciated.

Était-ce utile?

La solution

Try the following. The quoted object in your question is in the $o variable.

$html = '';
$images = $o->bossresponse->images->results;
foreach ($images as $image) {
    $html .= "<a href='{$image->refererclickurl}'><img src='{$image->thumbnailurl}'></a>";
}
echo $html;

Note that if $obj->prop equals 'foo' then "{$obj->prop} is not bar" would equal 'foo is not bar'.

Its also good to note that the html would be better if you also output the image dimensions and may change depending on what html spec you're implementing.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top