简体   繁体   中英

How to exclude certain content from external XML with php?

Continuing this theme How to display remote XML file with Wordpress in frontend using php? i was asked the following issue:

<?php
$xmlhd = wp_remote_get('http://www.myurl.com/api/channel.php?type=hd');
$xmlparseado = simplexml_load_string($xmlhd['body']);

$content = '';
echo "<ul>";
$rows = $xmlparseado->channel->row;
foreach($rows as $key=>$row){   
    if($key =='row'){
        $row_string = '<li>';
        $row_string.= '<span>'.$row->date.'</span>';
        $row_string.= '<span>'.$row->time.'</span>';
        $row_string.= '<span>'.$row->description.'</span>';
        $row_string.= '<span>'.$row->imagethumb.'</span>';
        $row_string.= '</li>';
        $content.=$row_string;
    }   
}
echo $content;
echo "</ul>";
?>

The XML returns:

<programations>
    <channel name="KCBS HD">
        <row>
            <date>july, 23</date>
            <time>06:00</time>
            <title><![CDATA[ WKCBS Action News ]]></title>
            <description><![CDATA[ Action News, hosted by: Jenn Doe ]]></description>
            <imagethumb/>
        </row>
        <row>
            <date>July, 23</date>
            <time>06:35</time>
            <title><![CDATA[ KCBS Sports Center ]]></title>
            <description><![CDATA[ The best scoreS from the Sportscenter stadium, hosted by: Fernando Sobalaprieta ]]></description>
            <imagethumb/>
        </row>
    </channel>
</programations>

This code displays a list that consists of the following:

  • date
  • time
  • description
  • image

There are many, but I was asked to show the date, only the first entry, ie:

First entry:

  • date
  • time
  • description
  • image

Second entry and more:

  • time
  • description
  • image

The problem is that when you get the end of the day, the date changes to the next day so I can not use conditional.

Any suggestions?

Edit:

Please remember this:

Every days, the first program display the date. :)

If I understood correctly, you want just the first entry to have the date displayed. So use a dummy counter:

$count = 0;
$first_date = "";
foreach($rows as $key=>$row){   
if($key =='row'){
    $row_string = '<li>';
    if($count == 0 ){ 
    $first_date = $row->date; 
    $row_string.= '<span>'.$row->date.'</span>';
    }
    $row_string.= '<span>'.$row->time.'</span>';
    $row_string.= '<span>'.$row->description.'</span>';
    $row_string.= '<span>'.$row->imagethumb.'</span>';
    $row_string.= '</li>';
    $content.=$row_string;
    if( $count == 0 || strcmp( $row->date, $first_date ) == 0 ) 
    $count++;
    else $count = 0;
}   
}

Edit: okay, so now it will display the date only for the first appearing entry of that day.

Hm, try commenting out lines that you don't need. Btw, I hope I got this right. Try this for example:

 $row_string = '<li>';
 $row_string.= '<span>'.$row->date.'</span>';
 //$row_string.= '<span>'.$row->time.'</span>';
 //$row_string.= '<span>'.$row->description.'</span>';
 $row_string.= '<span>'.$row->imagethumb.'</span>';
 $row_string.= '</li>';
 $content.=$row_string;

With // php will ignore those lines, like here you won't have time and description displayed no more.

Store the day in a separate variable, and when it changes, echo it out then.

$dateHolder = "";
foreach($rows as $key=>$row){   
    if($key =='row'){         
        $row_string = '<li>';
        if($dateHolder=="" || $dateHolder != $row->date) {
            $row_string.= '<span>'.$row->date.'</span>';
            $dateHolder = $row->date;
        }
        $row_string.= '<span>'.$row->time.'</span>';
        $row_string.= '<span>'.$row->description.'</span>';
        $row_string.= '<span>'.$row->imagethumb.'</span>';
        $row_string.= '</li>';
        $content.=$row_string;
    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM