0

I have this code that finds every xml file in a directory and then grabs the dates. But I want it instead to echo each line in each xml file in the directory. the xml files look like this

<event> 
<day>11</day> 
<month>01</month> 
<year>2013</year> 
<link>link</link> 
<title>Title</title> 
<description></description>
</event>

and my php code

$events = array();

// open each xml file in this directory
foreach(glob("*.xml") as $filename) {   
    // get the contents of the the current file
    $xml_file = file_get_contents($filename, FILE_TEXT);

    // create a simplexml object from the contents of the current file
    $xml = simplexml_load_string($xml_file);

    // create a day, link, description, etc.
    $eventDay = (int)$xml->day;
    $eventMonth = (int)$xml->month;
    $eventYear = (int)$xml->year;
    $eventLink = (string)$xml->link;
    $eventDesc = (string)$xml->description;

    if($eventMonth == $month && $eventYear == $year)        
        $events[$eventDay] = array($eventLink,'linked-day');
}
return $events;

}

so for example for each file found it would print out something like

 <div class="event"><p>Date: month/day/year</p><p>Title: the xml title</p><p>description: the xml description</p></div>
4

1 回答 1

0

作为一个简单的解决方案,如果您想将事件作为循环的一部分回显,您可以这样做:

$events = array();

// open each xml file in this directory
foreach(glob("*.xml") as $filename) {   
    // get the contents of the the current file
    $xml_file = file_get_contents($filename, FILE_TEXT);

    // create a simplexml object from the contents of the current file
    $xml = simplexml_load_string($xml_file);

    // create a day, link, description, etc.
    $eventDay = (int)$xml->day;
    $eventMonth = (int)$xml->month;
    $eventYear = (int)$xml->year;
    $eventLink = (string)$xml->link;
    $eventDesc = (string)$xml->description;
    $eventTitle = (string)$xml->title;        

    if($eventMonth == $month && $eventYear == $year)        
        $events[$eventDay] = array($eventLink,'linked-day');

    echo "<div class=\"event\">
            <p>Date: {$eventMonth}/{$eventDay}/{$eventYear}</p>
            <p>Title: {$eventTitle}</p>
            <p>description: {$eventDesc}</p>
          </div>";

}
return $events;

唯一的补充是$eventTitleecho部分。

于 2013-02-01T03:28:53.587 回答