开发者

PHP / MySQL: Group items by month

开发者 https://www.devze.com 2023-03-05 02:25 出处:网络
Good morning, I am trying to alter the output of a calendar WordPress plugin for a website. I need to be able to group items by month. Ideally, I would have the month and year and below that would be

Good morning,

I am trying to alter the output of a calendar WordPress plugin for a website. I need to be able to group items by month. Ideally, I would have the month and year and below that would be a list of all the objects from that month. I have everything good to go except for the grouping by month and year.

The table is structured like this:

id | event_start | event_end | event_title | event_desc

2  | 1309935600  | 309939200 |        test        | Donec iaculis...

I'm new to PHP and MySQL but here's the code that is currently using (I did not write this, I've modified it for my own use -- most html / css removed):

// 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),$eve开发者_如何学Pythonnt['event_end']).       

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

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

Any help would be greatly appreciated. The current output can be seen here: http://nwtechanddesign.com/jariccodance/calendar/

The desired output (styling apart) can be seen here: http://nwtechanddesign.com/wp-content/blogs.dir/11/calendar.jpg

TIA


I'm not 100% sure what you're doing with that string that you're getting back out, so I can't really answer in a way that you'll just be able to drop in, but, given that you're creating some big string from imploding arrays, I guess you just want to know how to drop a header in, at the right places.

Assuming your array is sorted right, which based on the queries, it looks like it should be coming out in order, you could do something like:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号