limit the results and get the correct image size
Submitted by sahertian on Thu, 2009-10-08 08:50.Magic Parser
Hi David,
I want to get images from lastfm with a valid xml.
{link saved}
It gives me a list of images within the sizes tag. How can I get only the medium or small size?? And how can I limt the amount of the images to 5??
Thanks,
Ron
Hello Ron,
To limit results, just use a global counter variable, increment each time in myRecordHandler and return TRUE when it equals the limit you want. To extract the MEDIUM image each time, the best structure in this instance would be a loop, and then break out of the loop when the -NAME attribute is "medium" (or whichever required). For example:
<?phprequire("MagicParser.php");
$counter = 0;
function myRecordHandler($record)
{
global $counter;
$i = 0;
$postfix = "";
while(1)
{
if ($i) $postfix = "@".$i;
// make sure we don't have an infinate loop
if (!isset($record["SIZES/SIZE".$postfix])) break;
if ($record["SIZES/SIZE".$postfix."-NAME"]=="medium")
{
$imageURL = $record["SIZES/SIZE".$postfix];
break;
}
$i++;
}
// medium image URL will be in $imageURL here!
$counter++;
if ($counter == 5) return TRUE;
}
MagicParser_parse("{link saved}","myRecordHandler","xml|LFM/IMAGES/IMAGE/");
?>
Hope this helps!
Cheers,
David.