php读取xml数据,图片轮换显示效果

本文介绍了一种使用PHP和XML结合的技术来实现动态图片展示的方法。系统通过解析XML文件中的图片展示时间配置,根据当前时间自动选择合适的图片进行展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 01.xml
-------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<message>

<record>
<id>1</id>
<name>01.jpg</name>
<showtime>08:00:00-09:59:00</showtime>
</record>

<record>
<id>2</id>
<name>02.jpg</name>
<showtime>10:00:00-10:20:00</showtime>
</record>

<record>
<id>3</id>
<name>03.jpg</name>
<showtime>10:28:00-19:00:00</showtime>
</record>

</message>




show_pic.php(我的代码)
--------------------------------------------------------------------------------------------------
<?
$xmlDoc = new DOMDocument();        //创建一个DOMDocument对象
$xmlDoc->load("01.xml");            //调用该类的load()方法,载入xml文件
$xpath = new DOMXPath($xmlDoc);        //创建一个DOMXPath对象 DOMXPath()是XML的SQL语句
$pic_time = $xpath->query("//showtime");    //读取showtime节点
foreach ($pic_time as $node){
    $node_time[$k] = $node->textContent;
    $arr_node_text = explode("-",$node_time[$k]);    //拆分,原格式为08:00:00-09:00:00
      $arr_stime = explode(":",$arr_node_text[0]);//再拆时间段起点
      $arr_etime = explode(":",$arr_node_text[1]);//再拆时间段终点
      $sys_time = date("H:i:s");        //获取系统时间
    $arr_sys_time = explode(":",$sys_time);//再拆
    //计算时间差值,所得到的值为从1970-1-1 00:00:00 GMT到参数时间的总秒数:是整数
    $s1=mktime($arr_sys_time[0],$arr_sys_time[1],$arr_sys_time[2]);    //系统时间相距1970-1-1 00:00:00 GMT的秒数
    $s2=mktime($arr_stime[0],$arr_stime[1],$arr_stime[2]);    //时间段起点,相距1970-1-1 00:00:00 GMT的秒数
    $s3=mktime($arr_etime[0],$arr_etime[1],$arr_etime[2]);    //时间段终点,相距1970-1-1 00:00:00 GMT的秒数
    if($s2-$s1<=0 && $s1-$s3<=0){
        $pic_name = $xpath->query("//record[showtime='$node_time[$k]']");
        $g=0;
        foreach ($pic_name->item(0)->childNodes as $r_node){
            $arr_r[$g] = $r_node->textContent;
            echo "/$arr_r[$g] = ".$arr_r[$g]."<br>";
            $g++;
        }
        echo <<<EOD
              <div><img src="images/{$arr_r[3]}" border=0></div>    <!-- 显示图片 -->
EOD;
    }
    $k++;
}
?>



show_pic2.php(别人的代码)
--------------------------------------------------------------------------------------------------------
<?
$xmlDoc = new DOMDocument();        //创建一个DOMDocument对象
$xmlDoc->load("01.xml");            //调用该类的load()方法,载入xml文件
$xpath = new DOMXPath($xmlDoc);        //创建一个DOMXPath对象 DOMXPath()是XML的SQL语句
$pic_time = $xpath->query("//showtime");    //读取showtime节点
$k=0;
$nnn=time();
foreach ($pic_time as $node)
{
$ttime=explode("-",$node->textContent);
$stime = strtotime($ttime[0]);
$stime2= strtotime($ttime[1]);
if($nnn>=$stime and $nnn<=$stime2){
    echo $node->parentNode->childNodes->item(3)->nodeValue;
}
}
?>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值