2010-10-08 59 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