Reputation: 77
I have a simple XML table with dates dates.xml
<schedule> <day> <date>01.03.17</date> <dayname>Thursday</dayname> <htime1>2:00</htime1> <htime2>3:00</htime2> </day> <day> <date>02.03.17</date> <dayname>Friday</dayname> <htime1>1:00</htime1> <htime2>4:00</htime2> </day> <day> <date>03.03.17</date> <dayname>Saturday</dayname> <htime1>0:00</htime1> <htime2>7:00</htime2> </day> ... </schedule>
I want to a load list of the next 10 days, beginning with the current day from today. I've found a way, to import the xml-table by using simplexml_load.
<?php
$xml=simplexml_load_file("dates.xml");
echo '<li>';
echo $xml->date . "<br>";
echo $xml->dayname . "<br>";
echo $xml->htime1 . "<br>";
echo $xml->htime2;
echo '</li>';
?>
Is is possible to start a loop, beginning from the current day? e.g. Today is the 2th of March. The list should be like this:
Do you have any idea, how to do this? Thank you!
Upvotes: 0
Views: 186
Reputation: 77
I've tried to put the list into a loop and stop the loop after 10 days. This works fine for me:
<?php
$xmldata=simplexml_load_file("dates.xml");
$i = 0;
foreach($xmldata->day as $day) {
if($i==10) break;
$i++;
echo "<p>Date: " . $day->date . "</p>";
echo "<p>Dayname: " . $day->dayname . "</p>";
echo "<p>Time 1: " . $day->htime1 . "</p>";
echo "<p>Time 2: " . $day->htime2 . "</p>";
echo "<hr>";
}
?>
But now, I need to ask for the current date, to start the loop from value "date" from today, and continue with the upcoming next 9 days.
The XML table begins from the 1st March. But if it is the 6th March, the loop should skip the first 5 days, and start from the 6th of March.
Upvotes: 0
Reputation: 3763
The value returned by the simplexml_load_file function is an object of type SimpleXMLElement. This properties of this object can be traversed in a loop. For example you should be able to use the following loop:
for ($count = 0; $count < count($xml->schedule>day); $count++) {
$date = $xml->schedule->day[$count]['date'];
$dayname = $xml->schedule->day[$count]['dayname'];
$htime2 = $xml->schedule->day[$count]['htime1'];
$htime1 = $xml->schedule->day[$count]['htime2'];
echo '<li>';
echo $date . "<br>";
echo $dayname . "<br>";
echo $htime1 . "<br>";
echo $htime2;
echo '</li>';
}
Upvotes: 1