Question

After some substantial research on this topic, I have unfortunately not found an answer. I would imagine that this is an exceptionally simple question for a lot of people here.

I'm connected to Last.fm's XML database through their API (Using PHP). On entering an artist that exists, or a blank field, I can echo the correct information with no issues. I cannot however echo anything if the user types in an artist that doesn't exist.

My question is this; how can I echo whatever message I want if that XML element does not exist? My line of thought was as follows:

   foreach ($uk_events as $event){

$venue_city = (string) $event->venue->location->city;
$image = (string) $event->image[2];
$uk_street = (string) $event->venue->location->street;
$uk_postcode = (string) $event->venue->location->postalcode;
$startdate = (string) $event->startDate;
$starttime = (string) $event->startTime;
$uk_venues = (string) $event->venue->name; 
$uk_names = (string) $event->artists->artist;
$website = (string) $event->website;

        if (empty($uk_names)){

    echo "<p class='sorry'>Sorry, but it doesn't look like this artist exists. Either they're exceptionally obscure or they're from
    another realm. Try again, or have a look at the below suggestions.</p>";

    }

After some trial and error the best that I have gotten is for the above to be echoed, but it's echoed regardless of what's searched for. I need the above to appear only if the artist doesn't exist within the XML database.

Much appreciated for anyone who can give me an idea as to how to progress on this one.

Thank you.

Was it helpful?

Solution

If you're using the SDK from GitHub with the call

$uk_events = $artistClass->search($methodVars)

then you can edit your code to read

if ($uk_events === false) {
    echo "<p class='sorry'>Sorry, we couldn't contact the server. Try again later.</p>";             

} elseif (empty($uk_events)) {
    echo "<p class='sorry'>Sorry, but it doesn't look like this artist exists. Either they're exceptionally obscure or they're from another realm. Try again, or have a look at the below suggestions.</p>";             

} else {
   foreach ($uk_events as $event){             
       .... // print list
   }      
} 

If you're using raw API calls, you'll get a response like

// Response to http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=NotARealArtist

<?xml version="1.0" encoding="UTF-8"?>
<lfm status="ok">
  <results xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" for="NotARealArtist">
    <opensearch:Query role="request" startPage="1" searchTerms="NotARealArtist"/>
    <opensearch:totalResults>0</opensearch:totalResults>
    <opensearch:startIndex>0</opensearch:startIndex>
    <opensearch:itemsPerPage>30</opensearch:itemsPerPage>
    <artistmatches>
    </artistmatches>
  </results>
</lfm>

So, you have two ways of identifying that you have no artists:

  1. the "opensearch:totalResults" element contains a value of "0"
  2. the number of child elements inside "artistmatches" is zero

So parse the XML for those values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top