简体   繁体   中英

how to wrap optgroup labels around php array values in dropdown list

I have an array called $selection that contains ascending time values from 09:00 to 18:00 in 15 minute intervals.

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

Some intervals could be missing, because this array is generated from some prior code which determines which times are available from google calendar (amongst other things). 09:00 isn't always the start time, sometimes the entire morning could be booked out.

I am using this array to create a drop down list.

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
}

Problem

I am trying to add

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

and

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

Not sure how to do this. I have come to the conclusion that I need a while loop. Perhaps even adding extra information into the array before running the foreach loop.. not sure!

Would appreciate any help

For sorted array :

$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>'; } 

If the array can't be sorted before, simply store to 2D array while loop and print after.

$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>'; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM