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

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?