gpt4 book ai didi

PHP/MySQL : Group items by month

转载 作者:行者123 更新时间:2023-11-29 03:45:38 24 4
gpt4 key购买 nike

早上好

我正在尝试更改网站日历 WordPress 插件的输出。我需要能够按月对项目进行分组。理想情况下,我会有月份和年份,下面是那个月所有对象的列表。除了按月和年分组外,我已经准备好了一切。

表格结构如下:

编号 | event_start | event_end | 事件标题 | 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

最佳答案

我不是 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;
}

关于PHP/MySQL : Group items by month,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5939895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com