代码之家  ›  专栏  ›  技术社区  ›  berkes

事件列表,带有时间戳列表,在PHP中按小时分组,以表格形式显示。

  •  2
  • berkes  · 技术社区  · 15 年前

    Example here ,请注意带有小时的表的标题。以及在那一小时内被归在那一列的时间。一个事件,在一小时内有两个时间戳可以忽略(根据技术文档,理论上可能只发生)

    这张桌子是用德鲁帕尔做的 theme_table() ,我可以从任何数组中建立,每个数组的方位数组

    当前代码(片段):

    <?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');
    }
    

    所以,模式是:获取(并排序)事件列表。循环遍历,对于每个事件,循环遍历时间戳并创建一个数组,键是时间戳的小时(按小时分组)。

    有更好的模式吗?有没有更通用的方法来实现这一点?我可以搜索任何关键字(我不知道如何调用这样的模式)?

    示例数据集 $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"
        }
      }
    }
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   kevtrout    15 年前

    我对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实体。

        2
  •  0
  •   Dhanesh Haridas    15 年前

    Calendar “模块。日历模块可用于将事件列为日历。它有视图、分类法和cck支持。定制也很容易。

    我建议在考虑我们的脚本之前先找出贡献的模块。只有在没有满足我们需求的贡献模块时,我们才能考虑我们的脚本。

    推荐文章