dthtvk3666 2017-02-23 15:52
浏览 18
已采纳

如何根据当天循环列表?

I have a simple XML list with dates inside (chronological order). I have found a way loop the list 7 times (1 week).

<?php
    $xmldata=simplexml_load_file("dates.xml");
    $i = 0;
    foreach($xmldata->day as $day) {
    if($i==7) break;
    $i++;
    echo "<p>Date: " . $day->date . "</p>";
    echo "<p>Dayname: " . $day->content . "</p>";
    }
    ?> 

Now I have the problem, that the loop always starts at the first line of the XML file and ends after 7 times. But I want to start the loop, when

 $xmldata->day->date = $currentday;
 $currentday = date('d.m.y');

and continue with the next 7 items (days) in the list.

The XML (dates.xml) looks like this:

<schedule>
  <day>
    <date>01.03.17</date>
    <information>Lorem ipsum</information>
  </day>
  <day>
    <date>02.03.17</date>
    <information>Lorem ipsum</information>
  </day>
  <day>
    <date>03.03.17</date>
    <information>Lorem ipsum</information>
  </day>
  ...
</schedule>

Thank you!

  • 写回答

1条回答 默认 最新

  • duanfan8360 2017-02-23 16:17
    关注

    Solution after commnets:

    <?php
    $xmldata=simplexml_load_file("dates.xml");
    $i = 0; 
    $startfound = false;
    foreach($xmldata->day as $day) {
        if(!$startfound && $day->date!=date('d.m.y')) continue;
        $startfound = true;
        if($i==7) break;
        $i++;
        echo "<p>Date: " . $day->date . "</p>";
        echo "<p>Dayname: " . $day->content . "</p>";
    }
    ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程