Reputation: 793
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:
There are many, but I was asked to show the date, only the first entry, ie:
First entry:
Second entry and more:
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. :)
Upvotes: 0
Views: 101
Reputation: 1027
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.
Upvotes: 1
Reputation: 5155
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;
}
}
Upvotes: 0
Reputation: 928
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.
Upvotes: 0