Search code examples
phpxmlrsssimplexml

Advanced SimpleXml / PHP


I'm currently parsing an xml feed of events created by an online service to my band's website. It includes all the data I need to display on my website, including date, time, name, etc etc etc.

However, I would like to find a way to organize the dates by month, so they would display like this:

APRIL 2011

  • date 1
  • date 2
  • date 3

May 2011

  • date 4
  • date 5

June 2011

  • date 6
  • date 7

I could probably do this if the data I was displaying was from a database, however, my php skills aren't the best, and i'm having a hard time figuring out how I could accomplish this using SimpleXML.

Currently my (dumbed down) code looks like this:

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

Can anyone give me any guidance, or point me into the direction as to how I could go about this.


Solution

  • One way to do it, is to first iterate over the data set and group each item by month. Something like this.

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

    Now you should have an array with every show grouped by month.

    Then it's easy as

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