【问题标题】:How to exclude certain content from external XML with php?如何使用 php 从外部 XML 中排除某些内容?
【发布时间】: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 日 我要求该列表每天只显示迄今为止的第一行。 :(

标签: php xml wordpress


【解决方案1】:

如果我理解正确,您只希望第一个条目显示日期。所以使用一个虚拟计数器:

$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;
}   
}

编辑:好的,所以现在它将仅显示当天第一个出现的条目的日期。

【讨论】:

  • 我不能使用计数器,因为日期在一天结束时改变:(
  • 但是,我有 3 个 XML 差异“BASIC、PREMIUM、HD”,当我向变量添加数字时,它是正确的吗?
【解决方案2】:

嗯,试着注释掉你不需要的行。顺便说一句,我希望我做对了。试试这个例子:

 $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;

// php 将忽略这些行,就像这里你不会有时间和描述不再显示。

【讨论】:

    【解决方案3】:

    将日期存储在一个单独的变量中,当它发生变化时,将其回显出来。

    $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;
        }   
    }
    

    【讨论】:

    • @Lawrence 这将回显第一个日期以及当天的后续项目,但是当日期更改时,它不会打印出新日期。
    • 我需要@Matthew Johnson 说的 :)
    猜你喜欢
    • 1970-01-01
    • 2016-07-05
    • 2013-12-10
    • 2017-03-28
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多