0

ディレクトリ内のすべてのxmlファイルを見つけて日付を取得するこのコードがあります。しかし、代わりに、ディレクトリ内の各xmlファイルの各行をエコーし​​たいです。xmlファイルは次のようになります

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

そして私のphpコード

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

}

たとえば、見つかったファイルごとに、次のようなものが出力されます

 <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 に答える