douqinlu4217 2012-05-21 00:50
浏览 27
已采纳

如果XML元素不存在,则通过PHP回显消息

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.

  • 写回答

1条回答 默认 最新

  • dongxian4531 2012-05-21 03:35
    关注

    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.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部