dtrhd2850 2011-04-22 14:23 采纳率: 0%
浏览 9
已采纳

高级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 />
<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>

"; // CLOSE THE LIST ITEM
}

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

  • 写回答

1条回答 默认 最新

  • dongwo6477 2011-04-22 14:32
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录