更改日历以显示每天多个事件

从昨天开始我就在思考一个问题...

我使用日历代码是为了在我的网站上显示日历。

这是它的 PHP 代码:(我对其进行了一些修改以表示日历中的颜色和时间)

现在我有两种不同类型的约会,它们是从数据库获取的,转换为数组并将其作为变量传递$events:


$events = array();

            foreach ($partEvents as $event) {

                $events[date('Y-m-d', strtotime($event['eventDate']))] = array(

                    'text' => $event['description'],

                    'href' => '/nsl/editGroupCoaching/'.$event['ID'],

                    'time' => date('H:i', strtotime($event['eventDate'])),

                    'color' => '#edb01a'

                );

            }


            foreach ($appointments as $event) {

                $events[date('Y-m-d', strtotime($event['date']))] = array(

                    'text' => $event['description'],

                    'href' => '#',

                    'time' => date('H:i', strtotime($event['date'])),

                    'color' => '#00898d'

                );

            }

所以我用$partEvents一种颜色显示 ,用$appointments另一种颜色显示 。一般来说,这工作正常,并且它们前面显示时间。但是,每当同一天有 2 个事件时,它就不再起作用,因为数组键不再唯一(仅显示 2 个事件中的 1 个)......


我希望一天可以有1、2、3甚至10个事件......


我想到的只是更改数组生成并H:i:s在数组键上添加,但是,然后就根本不再显示约会了......


我需要如何更改日历生成的代码,以便能够每天添加多个事件?$runningDay我尝试使用and进行了很多尝试$cur_date,但我似乎无法让它发挥作用..我必须承认,我并不是最擅长处理多维数组..



不负相思意
浏览 71回答 1
1回答

手掌心

如果您向主要活动索引添加时间,日历将无法直接确定给定日期是否有活动。您可以添加在找到给定日期的事件后使用的辅助索引(添加两个本地解释变量,$date并$time更改它们的名称,以防与代码的其余部分发生冲突):&nbsp; &nbsp; &nbsp; &nbsp; $events = array();&nbsp; &nbsp; &nbsp; &nbsp; foreach ($partEvents as $event) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $date = date('Y-m-d', strtotime($event['eventDate']));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $time = date('H:i', strtotime($event['eventDate']));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $events[$date][$time] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'text' => $event['description'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'href' => '/nsl/editGroupCoaching/'.$event['ID'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'color' => '#edb01a'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; foreach ($appointments as $event) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $date = date('Y-m-d', strtotime($event['eventDate']));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $time = date('H:i', strtotime($event['eventDate']));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $events[$date][$time] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'text' => $event['description'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'href' => '#',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'color' => '#00898d'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }现在您需要更改事件的显示方式(潜在的渲染问题留给您解决 - 它只会直接将单个事件的副本添加到表格单元格中):&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; // Insert an event for this day&nbsp; &nbsp; &nbsp; &nbsp; if ($draw_event) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ksort($events[$cur_date]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($events[$cur_date] as $time => $event) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $calendar .=&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<div class='{$css_cal_event}'>" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<a href='{$event['href']}' style='color:".$event['color'].";'>" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $time.' Uhr: <br/>'.$event['text'] .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "</a>" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "</div>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5