【问题标题】:how to wrap optgroup labels around php array values in dropdown list如何在下拉列表中围绕 php 数组值包装 optgroup 标签
【发布时间】:2012-10-29 20:53:31
【问题描述】:

我有一个名为 $selection 的数组,其中包含从 09:00 到 18:00 以 15 分钟为间隔递增的时间值。

例如:$selection = array(09:00, 09:15, 09:30, 09:45, 10:00, 10:15, 10:30);

某些时间间隔可能会丢失,因为该数组是从一些先前的代码生成的,该代码确定哪些时间可从谷歌日历中获得(除其他外)。 09:00 并不总是开始时间,有时整个上午都可能被预订一空。

我正在使用这个数组来创建一个下拉列表。

foreach($selection as $slot) {
  if ( $slot == $selection[0] ) {
    print '<option value="'.$slot.'" selected="selected">'.date('H:i',  strtotime($slot)).'</option>';
  } else {
    print '<option value="'.$slot.'">'.date('H:i', strtotime($slot)).'</option>';
  } // end else
}

问题

我正在尝试添加

<optgroup label="Morning"> .. </optgroup>

<optgroup label="Afternoon"> .. </optgroup>

不知道该怎么做。我得出的结论是我需要一个while循环。甚至可能在运行 foreach 循环之前将额外信息添加到数组中......不确定!

不胜感激

【问题讨论】:

    标签: php arrays foreach


    【解决方案1】:

    对于排序数组

    $interval = '';
    $prev_interval = '';
    
    foreach($selection as $slot) 
    {
        // Check if selected option
    
        $sel = ( $slot == $selection[0] ) ? ' selected = "selected" ' : '';
    
        // Set optgroup interval; 
        // for HH:MM we can use basic string alphabet comaparsion
    
        if ($slot > '22:00')
        {
           $interval = 'Night';
        } 
        else if ($slot > '18:00')
        {
           $interval = 'Evening';
        }
       ...
    
        // check if interval has changed
    
        if ($prev_interval != $interval)
        {  
           // check if previous intrval was set
    
           if ($prev_interval!='') {  echo "</optgroup>" };
    
           printf('<optgroup label="%s">',$interval); 
        } 
    
        printf('<option value="%s"%s>%s</option>',$slot,$sel,date('H:i',  strtotime($slot)))  ;
    
        $prev_interval = $interval;
    }
    
    // last close element
    
    if ($interval!='') { echo '</optgroup>'; } 
    

    如果数组之前无法排序,只需在while循环中存储到二维数组,然后打印即可。

    【讨论】:

    • 嘿伙计,你的代码一开始没有用。然而,这是一个很好的起点,基本上解决了我的问题。非常感谢
    • 啊。选中的条件中缺少撇号。对不起。
    【解决方案2】:
    $interval = '';
    $prev_interval = '';
    
    foreach($selection as $slot) {
    // Check if selected option
    
    if ( $slot == $selection[0] ) {
    
      $sel = "selected = \"selected\"";
    }
    
    else { $sel = '';}
    
    //$sel = ( $slot == $selection[0] ) ? ' selected = "selected" : '';
    
    // Set optgroup interval; 
    // for HH:MM we can use basic string alphabet comaparsion
    
    if (date('H:i', strtotime($slot)) > '18:30') {
       $interval = 'Evening';
    }
    
    else if (date('H:i', strtotime($slot)) > '15:30') {
       $interval = 'Late Afternoon';
    }
    
    else if (date('H:i', strtotime($slot)) > '12:00') {
       $interval = 'Early Afternoon';
    }
    
    else if (date('H:i', strtotime($slot)) > '10:30') {
       $interval = 'Late Morning';
    }  
    
    else if (date('H:i', strtotime($slot)) > '09:00') {
       $interval = 'Early Morning';
    }
    
    // check if interval has changed
    if ($prev_interval != $interval) {  
       // check if previous intrval was set
    
       if ($prev_interval!='') {
        echo "</optgroup>";
      }
    
       printf('<optgroup label="%s">',$interval); 
    } 
    
    printf('<option value="%s"%s>%s</option>',$slot,$sel,date('H:i',  strtotime($slot)))  ;
    
    $prev_interval = $interval;
    }
    
    // last close element
    
    if ($interval!='') { echo '</optgroup>'; } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多