2011-04-22 87 views
1

我目前正在解析由在線服務創建的活動的XML feed,並將其發佈到我的樂隊網站上。它包括我需要在我的網站上顯示的所有數據,包括日期,時間,名稱等等等。高級SimpleXml/PHP

但是,我想找到一種按月組織日期的方法,因此它們將顯示爲這樣:

APRIL 2011

  • 日期1
  • 日期2
  • 日期3

2011年5月

  • 日期4
  • 日期5

六月2011

  • 日期6
  • 日期7

我可能會這樣做,如果我顯示的數據來自數據庫,但是,我的PHP技能不是最好的,我很難弄清楚如何使用SimpleXML來完成此操作。

目前我(簡單化)的代碼如下所示:

$shows = new SimpleXMLElement('URL TO XML FILE', null, true); 

foreach($shows as $show) // loop through our books 
{ 
    // Getting Variable from XML and changing them into provence/state codes (reduce space) 
    if(($show->state) != "" AND ($show->country) == "Canada") 
    { 

    if( 
     // CANADIAN PROVENCES 

     // THIS FINDS CANADIAN PROVENCES AND HAS BEEN REMOVED 

    } elseif (($show->state) != "" AND ($show->country) == "United States") { 
    if( 
     // AMERICAN STATES 
     // THIS FINDS AMERICAN STATES AND HAS BEEN REMOVED 


    // IF NONE OF THESE ARE HAPPENING JUST SHOW THE STATE 
    } else { $display_state = "{$show->state}"; }; 

    ///////////// 
    // SETTING HOW WE WANT THE DATE 
    $format = "M jS, Y"; //or something else that date() accepts as a format 
    $display_date = date_format(date_create(($show->date)), $format); 


    ///////////// 
    // CHECKING TO SEE IF ITS A FESTIVAL 
    if(($show->showType) == "Festival/Fair") { 
    // Setting the variable if its a festival or fair 
    $smart_venue_fest_or_building = "{$show->name}"; 
    } else { 
    // If it's not a festival or fair show the club 
    $smart_venue_fest_or_building = "{$show->venueName}"; 
    }; 



    // 
    // RUNNING THE LOOP 
    // 

    echo "<li><p>"; // OPEN THE LIST ITEM 

    echo "$display_date - {$show->city} $display_state - $smart_venue_fest_or_building"; 

    if(($show->venueURI) != "" OR ($show->ticketURI) != "") { 
    echo "<br />\n<span class='ticketLinks'>"; // PUTTING IN THE SURROUNDING SPAN TAG IF THE TICKET AND INFO LINK EXIST 
    } 

    if(($show->venueURI) != "") { // VENUE LINK 
    echo "<a href='{$show->venueURI}' target='_blank' title='Venue information for {$show->venueName}'><small>GET INFO</small></a>"; 
    }; 

    if(($show->venueURI) != "" AND ($show->ticketURI) != "") { 
    echo " - "; // SEPPERATING THE TICKET AND VENUE LINK IF THEY BOTH EXIST 
    } 

    if(($show->ticketURI) != "") { // TICKET LINK 
    echo "<a href='{$show->ticketURI}' target='_blank' title='Buy tickets for {$show->venueName} on $display_date'><small>BUY TICKETS</small></a>"; 
    }; 

    if(!empty($show->ticketURI) OR !empty($show->venueURI)) { 
    echo "</span>"; // CLOSING THE HOLDER TAG 
    } 

    echo "</p></li>\n\n"; // CLOSE THE LIST ITEM 
} 

誰能給我任何指導,或點我到的方向,我怎麼能去這一點。

回答

4

做到這一點的一種方法是首先迭代數據集並按月對每個項目進行分組。像這樣的東西。

$monthShows = array(); 

foreach($shows as $show) // loop through our books 
{ 
    $monthShows[date('F', $show->date)][] = $show; 
} 

現在你應該有一個數組,每個節目按月份分組。

然後,它很容易爲

foreach ($monthShows as $month => $shows) { 
    echo "<h2>{$month}</h2>"; 

    // Your output code 
}