PHP / MySQL:按月分组项目

时间:2011-05-09 16:41:16

标签: php mysql date plugins

早上好,

我正在尝试更改网站日历WordPress插件的输出。我需要能够按月对项目进行分组。理想情况下,我会在月份和年份以及下面列出该月份的所有对象。除了月份和年份的分组外,我还有一切都很好。

表的结构如下:

id | event_start | event_end | event_title |的 EVENT_DESC

2 | 1309935600 | 309939200 |测试| Donec iaculis ......

我是PHP和MySQL的新手,但这里是目前正在使用的代码(我没有写这个,我已经修改了它供我自己使用 - 大多数html / css已删除):

// Add the shortcode for displaying the event on pages
function displayevents( $atts ) {
global $wpdb;
setlocale(LC_ALL, get_locale());
$table_name = $wpdb->prefix . "simple_events";

// VARIATIONS: EXPIRED  /  ALL  /  UPCOMING
if($atts['age']) {
    $age = $atts['age'];
    if($age == "expired") {
        $range = "event_end <= " . time();
    } elseif($age == "all") {
        $range = "event_end > 946706400"; // timestamp for jan 1st 2000 - assuming no event will be creted before that date
    } else {
        $range = "event end > " . time();
    }
}

if($atts['label']) $label = strtolower($atts['label']);
if($atts['limit'] > 0) { $limit = "LIMIT 0, " . $atts['limit']; } else { $limit = ""; }

if( $age && $label ) {
    $allevents = $wpdb->get_results(" SELECT * FROM $table_name WHERE event_label = '$label' AND $range ORDER BY event_start $limit", "ARRAY_A");
} elseif($age) {
    $allevents = $wpdb->get_results(" SELECT * FROM $table_name WHERE $range ORDER BY event_start $limit", "ARRAY_A");
} elseif($label) {
    $currentTime = time();
    $allevents = $wpdb->get_results(" SELECT * FROM $table_name WHERE event_label = '$label' AND event_end >= $currentTime ORDER BY event_start $limit", "ARRAY_A");
} else {
    $currentTime = time();
    $allevents = $wpdb->get_results(" SELECT * FROM $table_name WHERE event_end >= $currentTime ORDER BY event_start $limit", "ARRAY_A");
}

foreach ($allevents as $event) {
    // decide if the year needs to be mentioned
    if(date('Y',$event['event_start']) == date('Y',time())) {
        $eventtime = strftime( __('%l:%M',SE_TEXTDOMAIN),$event['event_start']);
    } else {
        $eventtime = strftime( __('%l:%M',SE_TEXTDOMAIN),$event['event_start']);
    }

    $the_events[] =  
    strftime( __('%d',SE_TEXTDOMAIN),$event['event_end']).
    stripslashes($event['event_title']).

    stripslashes($event['event_desc']).

    $eventtime.
    ' to '.
    strftime( __('%l:%M',SE_TEXTDOMAIN),$event['event_end']).       

    $evt_loc.
    $evt_url;
} // end foreach ($allevents as $event)

$items = implode($the_events);
return($items);
}

非常感谢任何帮助。可以在此处看到当前输出:http://nwtechanddesign.com/jariccodance/calendar/

可以在此处看到所需的输出(样式分开):http://nwtechanddesign.com/wp-content/blogs.dir/11/calendar.jpg

TIA

1 个答案:

答案 0 :(得分:0)

我不是100%肯定你正在做的那个字符串你正在退出,所以我不能真的 回答的方式是你能够进入,但是,鉴于你正在创造一些大字符串 从爆炸数组中,我想你只想知道如何在正确的位置删除标题。

假设您的数组是正确排序的,基于查询,它看起来应该来了 按顺序排列,你可以这样做:

$curMonth = "";
$curYear = "";

foreach($allEvents as $event)
{
    // If we encounter a new month or year, change our curMonth/curYear values
    // and output a new header.
    if ((date('Y',$event['event_start']) != $curYear) || (date('M',$event['event_start']) != $curMonth))
    {
        $curMonth = date('M',$event['event_start']);
        $curYear = date('Y',$event['event_start']);

        // Outputs in 'Jan.2011' style
        // You'll just have to deal with this somehow, like you deal with the rest of the stuff
        $eventString = $curMonth.".".$curYear;
    }
    else
        $eventString = "";

    // Then instead of:
    // $the_events[] = 
    // use
    $eventString .=

    ... rest of your code ...

    $the_events[] = $eventString;
}