需要在PHP文件中为div分配唯一ID

时间:2014-06-18 15:17:38

标签: javascript php jquery html

我输出的日历最终会显示为最终用户的一系列按钮。每个按钮都有事件的名称。当您点击按钮时,它将向下切换以显示简短描述和事件的日期/时间。

到目前为止,我已经有了所有的事件按钮。但是,当您单击任何按钮时,仅显示最顶层事件的描述和日期/时间。那时我需要帮助的是弄清楚如何为每个事件实例分配一个唯一的div ID。我在这里尝试了一些其他的例子,但是因为下面的代码是用PHP编写的,所以只创建一个var = blank;抛出处理错误。

  public function event_block($title, $desc, $start, $end, $where = NULL, $img = NULL, $span = 6)
{
$start_arr = date_parse($start);
$start_stamp = mktime($start_arr['hour'], $start_arr['minute'], $start_arr['second'],    $start_arr['month'], $start_arr['day'], $start_arr['year']);
$start_date_int = date('Ymd', $start_stamp);

$end_arr = date_parse($end);
$end_stamp = mktime($end_arr['hour'], $end_arr['minute'], $end_arr['second'], $end_arr['month'], $end_arr['day'], $end_arr['year']);
$end_date_int = date('Ymd', $end_stamp);

$today_date_int = date('Ymd');

$time_range = date('g:i a', $start_stamp) . ' to ' . date('g:i a', $end_stamp);


if ($start_date_int == $today_date_int) {
  $date = 'Today';
  if ($start_date_int < $end_date_int) {
    $time_range = 'Until ' . date('l, F jS', $end_stamp);
  }
} else {
  $date = date('l, F jS', $start_stamp);
  if ($start_date_int < $end_date_int) {
    $time_range = 'From' . date('l, F jS', $start_stamp) . ' through ' . date('l, F jS', $end_stamp);
  }
}


$output = '

    <!-- Event Block -->
    <div class="span' . $span . ' event">
    <div class="panel-group id="accordion">
      <div class="panel-heading">
        <a data-toggle="collapse" data-parent="#accordion" href="#addMore" class="btn btn-primary btn-lg">' . $title . '</a>';

if (trim($desc) && $date == 'Today') {
  $output .=  '<div id="addMore" class="panel-collapse collapse">
        <div class="panel-body">' .  trim($desc) .  '</div>';
}
$output .= '<p style="padding-left: 5px;">';
if (trim($desc) && $date != 'Today') {
  $output .= '<div id="addMore" class="panel-collapse collapse"> 
  <div class="panel-body">' .trim($desc) . '</div>';
  $output .= '<span style="color: #E8D0A9;">Date:</span> ' . $date . '<br />';
}
$output .= '<span style="color: #E8D0A9;">Time:</span> ' . $time_range . '</p></div>';

return $output;

}

这是我之前看过的有用帖子:Create Dynamic div with unique IDs - jQuery

以下是我的calendar.php上的代码,该代码使用上面的数据生成事件日历:

<?php

            error_reporting(E_ALL); 
            ini_set('display_errors', TRUE); 
            ini_set('display_startup_errors', TRUE);

                require_once('include/event_signage.php');
                $sign = new Signage;

                // Collect and format all the data

                $hero_content = array(
                  'heading' => '',
                  'text'    => '',
                );

                $events_now_arr = $sign->databoard_get('URL');
                $events_today_arr = $sign->databoard_get('URL');
                $events_week_arr = $sign->databoard_get('URL');

                // Build the sign
                $signage = $sign->header();
                //$signage .= $sign->masthead($masthead_content);
                $signage .= '<div class="container-fluid"> <!-- container -->';

                $signage .= $sign->hero_unit($hero_content);
                $event_ids = array();

                // Happnening Now
                if (count($events_now_arr)) {
                  $signage .= '<div><h2>Right now!</h2></div>';
                  foreach ($events_now_arr as $events_arr) {
                    if (!in_array($events_arr['id'], $event_ids) && $events_arr['cal_id'] != 'community') {
                      $blocks[] = $sign->event_block($events_arr['title'], $events_arr['description'], $events_arr['start'], $events_arr['end']);
                    }
                    $event_ids[] = $events_arr['id'];
                  }
                  $signage .= $sign->gather_blocks($blocks);
                  unset($blocks);
                }

                // Happening today
                if (count($events_today_arr)) {
                  $signage .= '<div><h2>Today</h2></div>';
                  foreach ($events_today_arr as $events_arr) {
                    if (!in_array($events_arr['id'], $event_ids) && $events_arr['cal_id'] != 'community') {
                    $blocks[] = $sign->event_block($events_arr['title'], $events_arr['description'], $events_arr['start'], $events_arr['end']);
                    }
                    $event_ids[] = $events_arr['id'];
                  }
                  $signage .= $sign->gather_blocks($blocks);
                  unset($blocks);
                }


                // Happening this week
                if (count($events_week_arr)) {
                  $current_hour = date('G');
                  $cutoff = 39;
                  if ($current_hour <= 16) {
                    $cutoff = 27;
                    if ($current_hour <= 14) { $cutoff = 24; }
                    if ($current_hour <= 13) { $cutoff = 21; }
                    if ($current_hour <= 12) { $cutoff = 18; }
                    if ($current_hour <= 10) { $cutoff = 15; }
                  }

                  $signage .= '<div><h2>This Week</h2></div>';
                  $i = 0;
                  foreach ($events_week_arr as $events_arr) {
                    if (!in_array($events_arr['id'], $event_ids) && $events_arr['cal_id'] != 'community' && $i < $cutoff) {
                      $blocks[] = $sign->event_block($events_arr['title'], $events_arr['description'], $events_arr['start'], $events_arr['end'], NULL, NULL, 4);
                      $i++;
                    }
                    $event_ids[] = $events_arr['id'];
                  }

                  $signage .= $sign->gather_blocks($blocks, 3);
                  unset($blocks);
                }

                $signage .= '</div> <!-- /container -->';
                $signage .= $sign->footer();



                // Output the sign

                print $signage;
                ?>

1 个答案:

答案 0 :(得分:0)

你错了。您并不需要为任何div分配唯一的ID。您只需要一些标准的DOM操作。 e.g

<div>
   calendar entry #1
   <div>more information that's hidden for entry #1</div>
   <button onclick="showMore(this);">click here for more</button>
</div>
<div>
   calendar entry #2
   <div>more information that's hidden for entry #2</div>
   <button onclick="showMore(this);">click here for more</button>
</div>

由于DOM中知道中的每个元素都位于树中,因此您不需要在第一个容器div上使用特定ID,您只需要知道点击了哪个按钮这就是将this传递给showMore()函数的原因:

function showMore(obj) {
    obj.previousSibling().style.display = 'block'; // the more info div
}

当然,这段代码并不特别便携&#34;。它假设&#34;更多信息&#34; div总是紧挨着#34;切换&#34;按钮,但这应该足以给你一般的想法。