在重构过程中,我正在寻找PHP中时间表(日历)的优化算法。
我有一个事件列表,其中有一个时间戳列表。这必须以表格的方式提出。当前的代码可以工作,但是有一些奇怪的地方,而且( b)非常不灵活。我期待重构这一段,并寻找投入的优化方式。
这里的例子,请注意包含小时的表的标题。以及那一小时内的时间分组在那一栏中。一个事件,在一个小时内有两个时间戳可以忽略(根据技术文档,只能在理论上发生)。
该表是用Drupals 表()构建的,我可以从任何数组构建该表,其中包含每个行的数组。然而,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');
}因此,模式是:抓取(并排序)事件列表。循环处理,对于每个事件,循环遍历时间戳,并创建一个数组,其键为时间戳的小时(按小时分组)。
有没有更好的模式?是否有更通用的方法来实现这一点?任何我可以搜索的关键字(我不知道如何调用这样的模式)?
示例dataset $nodes (未删除的数据)如下所示:
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"
}
}
}发布于 2010-11-03 16:39:40
我对Drupal一点也不熟悉,所以我将根据我自己对您的请求提出建议,在一列中重构电影标题列表,并在适当的时间列中重构开始时间。
从包含电影标题和启动时间戳的一系列电影(或“电影”)开始。我想您可以在某个地方为列标题检索或创建一个小时数组。
下面的示例忽略了您正在处理一个对象,或者该对象包含数组,只是为了展示另一个模式方法。这个模式并不是您正在执行的所有格式和排序,而是对每个电影进行迭代,并使用第二个预测来迭代各个小时列,并测试电影时间是否与小时时间相匹配。
<?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实体。
我认为这是一个可行的模式,它似乎简化了原始模式的逻辑,并且保持了灵活性。希望我没有误解,没有遗漏你问题的一个关键部分。
发布于 2010-11-15 18:35:17
你试过"日历“模块吗?calendar模块可用于将事件作为日历列出。它具有视图、分类学和cck支持。定制也非常容易。
我建议在考虑我们的脚本之前,先找出已贡献的模块。只有在没有满足我们需求的贡献模块的情况下,我们才能考虑我们的脚本。
https://stackoverflow.com/questions/3888595
复制相似问题