高级 SimpleXml / PHP

发布于 2024-11-02 18:08:04 字数 2961 浏览 2 评论 0原文

我目前正在解析由我的乐队网站的在线服务创建的活动的 xml 提要。它包括我需要在网站上显示的所有数据,包括日期、时间、名称等。

但是,我想找到一种按月组织日期的方法,这样它们就会显示如下:

2011 年 4 月

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

2011 年 5 月

  • 日期 4
  • 日期 5

2011 年 6 月

  • 日期 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
}

任何人都可以给我任何指导,或者为我指出如何解决这个问题的方向。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云之铃。 2024-11-09 18:08:04

一种方法是首先迭代数据集并按月对每个项目进行分组。像这样的东西。

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文