重構時,我正在尋找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