2010-10-08 66 views
2

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

你能舉一個例子數據集嗎? – Gordon 2010-10-08 08:53:49

+1

關閉。剛剛做到了。謝謝。 – berkes 2010-10-08 09:34:39

+0

是來自節點負載還是僅來自一個節點的數據? – 2010-11-15 10:16:07

回答

0

我不熟悉使用Drupal可言,所以基於我要提出一個建議我用自己的方法來重構一列中電影標題列表的顯示以及適當時間欄中的開始時間。

您正在開始一系列包含電影標題和開始時間戳的電影(或'電影')。我假設你可以在某個地方爲列標題檢索或創建一個小時陣列。

下面的例子將忽略您正在處理一個對象,或者說,對象包含陣列,僅表現出一個替代圖案的方法。這種模式不是對所有格式和排序進行顯示,而是對每部電影進行迭代,並使用第二個foreach遍歷小時欄,並測試電影時間是否與小時匹配。

<?php foreach($movie_array as $movie){ 
    echo '<td>'.$movie['title'].'</td>'; 
    foreach($hours as $hour){ 
     if(date('H',$movie['starts_at']) == date('H',$hour)){ 
      echo '<td>'.date('H:i',$movie['starts_at']).'</td>'; 
     }//endif 
     else{ 
      echo '<td></td>'; 
     }//endelse 
    }//endforeach 
}//endforeach 
?> 

我已經跳過設定完表格,當然,你可以用,但是你想設置表取代我的HTML實體。

我認爲這是一種可行的模式,似乎可以簡化原始模式的邏輯,並保持靈活性。希望我沒有誤解和省略你的問題的一個關鍵部分。

0

您是否嘗試過「Calendar」模塊。日曆模塊可用於將事件列爲日曆。它有意見,分類和cck支持。定製也很容易。

我建議在思考我們的腳本之前找出貢獻的模塊。只有當沒有符合我們要求的貢獻模塊時,我們才能考慮我們的腳本。

+1

我很清楚日曆和它是一團糟:)客戶需要一個非常具體的解決方案,集成在一個自定義的XMLRPC後端。有 - 確定 - 沒有Drupal模塊。我知道,因爲我正在建造它:) – berkes 2011-01-08 16:23:34