繁体   English   中英

如何使用php从外部XML中排除某些内容?

[英]How to exclude certain content from external XML with php?

继续这个主题如何使用php在前端使用Wordpress显示远程XML文件? 我被问到以下问题:

<?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>

此代码显示包含以下内容的列表:

  • 日期
  • 时间
  • 描述
  • 图片

有很多,但是我被要求显示日期,只有第一个条目,即:

第一次进入:

  • 日期
  • 时间
  • 描述
  • 图片

第二次入境及更多:

  • 时间
  • 描述
  • 图片

问题在于,当您结束一天时, 日期更改为第二天,因此我不能使用条件式。

有什么建议么?

编辑:

请记住这一点:

每隔一天,第一个程序都会显示日期。 :)

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

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

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

嗯,尝试注释掉不需要的行。 顺便说一句,我希望我做对了。 尝试以下示例:

 $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将忽略这些行,例如,这里您将不再有时间并且不再显示说明。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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