高级 SimpleXml / PHP
我目前正在解析由我的乐队网站的在线服务创建的活动的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是首先迭代数据集并按月对每个项目进行分组。像这样的东西。
现在您应该有一个数组,其中每个节目都按月份分组。
然后就很容易了
One way to do it, is to first iterate over the data set and group each item by month. Something like this.
Now you should have an array with every show grouped by month.
Then it's easy as