【发布时间】:2013-07-25 20:48:01
【问题描述】:
继续这个主题How to display remote XML file with Wordpress in frontend using php? 我被问到以下问题:
<?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>";
?>
XML 返回:
<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>
此代码显示一个包含以下内容的列表:
- 日期
- 时间
- 说明
- 图片
有很多,但我被要求显示日期,只有第一个条目,即:
第一个条目:
- 日期
- 时间
- 说明
- 图片
第二次参赛及更多:
- 时间
- 说明
- 图片
问题是当你得到一天结束时,日期更改为第二天所以我不能使用条件。
有什么建议吗?
编辑:
请记住这一点:
每天,第一个程序显示日期。 :)
【问题讨论】:
-
为什么不能使用条件语句?如果日期更改,则将其添加到条件...
-
因为我不控制 XML 中的数据。你有: 7 月 23 日 7 月 23 日 7 月 23 日 7 月 23 日 7 月 24 日 7 月 24 日 我要求该列表每天只显示迄今为止的第一行。 :(