PHP help

PPLdude

Expert Member
Joined
Oct 3, 2011
Messages
1,716
Reaction score
663
Location
South
Hey guys,

Can anyone please tell me whats wrong with this code:

foreach($results['data'] as $item){
$image_url = $item['images']['low_resolution']['url'];
echo '<img src="'.$image_url.'" /> <br/>';
savePicture($image_url);
}


I have absolutely no idea why its wrong.

Full code Here
 
alternatively, start your loop as such:

PHP:
foreach ($results['data'] as $dataKey => $item) {
$image_url = $item['images']['low_resolution']['url'];
//rest of code here
}

EDIT: and print_r everything until you figure out whats happening
 
I think something is wrong with the quotation marks there,i don't know, something seems confusing, try

Code:
foreach($results['data'] as $item){
$image_url = $item['images']['low_resolution']['url'];
echo "<img src='.$image_url.' /> <br/>";
savePicture($image_url);
}
OR alternatively, the safe way...

Code:
foreach($results['data'] as $item){
$image_url = $item['images']['low_resolution']['url'];
echo '<img src=\".$image_url.\" /> <br/>';
savePicture($image_url);
}
 
You are using a associative array by the looks of it. You cannot use one the key for a foreach loop. You need to use the object array for the foreach loop.

thus you get an error the moment you say: "foreach($result['data]". Use the hole object not just one value from the associative array.

A good example can be found at: http://www.w3schools.com/php/php_arrays.asp
 
do a print_r($results) and post what that displays.

you can also use
Code:
if(sizeof($results['data]) == 0) {
echo ' No results';
}
 
Do a " var_dump ('<pre>', $results, '</pre>'); " to inspect the array layout on the screen.

This is always my first step in debugging issues with arrays, or when I assume it's an array but is actually an object.
 
Top
Sign up to the MyBroadband newsletter
X