重构时,我正在寻找PHP中的时间表(日历)的优化算法。带有时间戳列表的事件列表,以表格形式显示,按小时分组,以PHP表示。
我有一个事件列表的时间戳列表。这必须以表格的方式呈现。目前的代码工作,但有一个)相当一些quircks和b)非常不灵活。我期待重构这一块并寻找优化方式的输入。
Example here,请注意表格的标题轴承小时。那一小时内的时间被分组在那一栏中。一个事件,在一个小时内有两个时间戳可以忽略(可能只发生在理论上,根据技术文档)
该表是用Drupals theme_table()构建的,我可以从任何数组构建, row。 Drupal的一部分,但是,是没有多大兴趣在这里:)
当前代码(段)的:
<?php
//inside function that builds the content for the page linked to above:
$this_morning = _playdates_get_start_of_day_with($timestamp);
$this_night = _playdates_get_end_of_day_with($timestamp);
$nodes = _playdates_load_all_in_range($types, $this_morning, $this_night);
usort($nodes, '_playdates_cmp_titles');
//we now have a list of "nodes", being the events listed in the first column, which have
// a list of playdates, the timestamps. $node->nid is a unique id for each event.
foreach ($nodes as $node) {
$times = $dates = array();
if (is_array($node->starts_at)) {
foreach ($node->starts_at as $starts_at) {
$date = getdate($starts_at);
$dates[$starts_at] = $date;
if (!isset($active) && ($timestamp <= ($date[0] + $timezone))) {
$active = _playdates_get_idx_hour($date);
}
}
$times = _playdates_group_by_hour($dates);
foreach ($times as $key => $value) {
$header_times[$key] = $key;
}
$header = array_merge($header, $header_times);
$entries[$node->nid] = $node;
$entries[$node->nid]->times = $times;
}
}
function _playdates_group_by_hour($timestamps, $playdate = NULL) {
$indexes = array();
foreach ($timestamps as $key => $value) {
$indexes[_playdates_get_idx_hour($value)] = theme('playdates_format_time', $value, $playdate);
}
ksort($indexes);
return $indexes;
}
function _playdates_get_idx_hour($date) {
return playdates_format_date($date[0], 'custom', 'H:00');
}
因此,模式是:抢(并序)事件列表。循环播放,对于每个事件,遍历时间戳并创建一个数组,其中键为时间戳的小时(按小时分组)。
有更好的模式吗?有没有更通用的方法来实现这一目标?我可以搜索的任何关键字(我不知道如何调用这种模式)?
例如数据集 的$节点(unnessecary数据删除)是这样的:
array(15) {
[1]=>
object(stdClass)#48 (6) {
["title"]=>
string(19) "Cosa voglio di più"
["type"]=>
string(4) "film"
["nid"]=>
string(4) "2823"
["premiere"]=>
object(stdClass)#49 (1) {
["starts_at"]=>
string(10) "1286550000"
}
["starts_at"]=>
array(2) {
[0]=>
string(10) "1286550000"
[1]=>
string(10) "1286559000"
}
["ends_at"]=>
array(2) {
[0]=>
string(1) "0"
[1]=>
string(1) "0"
}
}
[12]=>
object(stdClass)#46 (6) {
["title"]=>
string(5) "Tirza"
["type"]=>
string(4) "film"
["nid"]=>
string(4) "2813"
["premiere"]=>
object(stdClass)#47 (1) {
["starts_at"]=>
string(10) "1286550000"
}
["starts_at"]=>
array(3) {
[0]=>
string(10) "1286550000"
[1]=>
string(10) "1286558100"
[2]=>
string(10) "1286566200"
}
["ends_at"]=>
array(3) {
[0]=>
string(1) "0"
[1]=>
string(1) "0"
[2]=>
string(1) "0"
}
}
[14]=>
object(stdClass)#36 (6) {
["title"]=>
string(47) "Vision - aus dem Leben der Hildegard von Bingen"
["type"]=>
string(4) "film"
["nid"]=>
string(4) "2783"
["premiere"]=>
object(stdClass)#39 (1) {
["starts_at"]=>
string(10) "1286541900"
}
["starts_at"]=>
array(1) {
[0]=>
string(10) "1286541900"
}
["ends_at"]=>
array(1) {
[0]=>
string(1) "0"
}
}
}
你能举一个例子数据集吗? – Gordon 2010-10-08 08:53:49
关闭。刚刚做到了。谢谢。 – berkes 2010-10-08 09:34:39
是来自节点负载还是仅来自一个节点的数据? – 2010-11-15 10:16:07