Pacific/Kosr...">

Simplify Multiple Echoes

I have a complete list of time zones in the selected menu, for example:

<option value="Pacific/Kosrae"> Pacific/Kosrae( +11:00 GMT ) </option>
  <option value="Pacific/Kwajalein"> Pacific/Kwajalein( +12:00 GMT ) </option>
  <option value="Pacific/Majuro"> Pacific/Majuro( +12:00 GMT ) </option>
  <option value="Pacific/Marquesas"> Pacific/Marquesas( -09:30 GMT ) </option>
  <option value="Pacific/Midway"> Pacific/Midway( -11:00 GMT ) </option>

the list goes on forever.

I want to change each of the parameters in this format:

if($_SESSION['timezone'] == 'Africa/Abidjan') {
echo '<option selected="selected" value="Africa/Abidjan"> Africa/Abidjan( +00:00 GMT ) </option>'; 
} else {
echo '<option value="Africa/Abidjan"> Africa/Abidjan( +00:00 GMT ) </option>';
}

How can I use php to not copy the paste and not edit each of the parameters manually?

+3
source share
4 answers

Store the data in some data structure and use a loop. For example, using a map with the time zone name for the offset:

$timezones = array(
    'Pacific/Kosrae' => '+11:00',
    'Pacific/Kwajalein' => '+12:00',
    ...
);

foreach($timezones as $name => $offset) {
    echo "<option value=\"$name\"" . ($name == $_SESSION['timezone'] ? " selected" : "") . ">$name( $offset GMT ) </option>\n";
}
+6
source

Well, imagine that you have a variable containing the form above, call it $formand another variable containing ie 'Africa/Abidjan'-$timezone.

$pattern = '/="'.str_replace('/', '\/', $timezone).'"/'; # /="Africa\/Abidjan"/
$replacement = '="'.$timezone.'" selected="selected"'; # ="Africa/Abidjan" selected="selected"
$output_form = preg_replace($pattern, $replacement, $form);

, .

0
$cur_timezone = 'Africa/Abidjan';
$timezones_arr = array ('Pacific/Kosrae','Pacific/Kwajalein',...);
$times_arr = array ('+11:00 GMT', '+12:00 GMT',...);
for ($i = 0; $i < count ($timezones_arr); $i ++) {
  if ($timezones_arr[$i] == $cur_timezone) {
    echo '<option selected="selected" value='$timezones_arr[$i]'>$timezones_arr[$i]($times_arr[$i]) </option>';
  }
  else {
    echo '<option value='$timezones_arr[$i]'>$timezones_arr[$i]($times_arr[$i]) </option>';
  }
}

$cur_timezone. $timezones_arr $times_arr.

0

, DOM, , , DOM:

function timezoneHelper($selected = NULL)
{   
    $dom = new DOMDocument;
    $dom->formatOutput = TRUE;
    $dom->loadXML('<select/>');
    $dom->documentElement->setAttribute('name', 'timezone-selector');
    $timezones = DateTimeZone::listIdentifiers();
    if(!is_numeric($selected)) {
        $selected = array_search($selected, $timezones);
    }
    foreach($timezones as $id => $timezone) {
        $option = $dom->createElement('option', $timezone);
        $option->setAttribute('value', $id);
        if($id == $selected) {
            $option->setAttribute('selected', 'selected');
        }
        $dom->documentElement->appendChild($option);
        unset($option);
    }
    return $dom->saveXML($dom->documentElement);
}

(w\out the GMT diff). . DateTimeZone::listAbbreviations, ) <option> <select>. , , value, , DateTimeZone::listIdentifiers(); . , ,

echo timezoneHelper('551');
// or
echo timezoneHelper('Zulu');

 <option value="551" selected="selected">Zulu</option>

selected .

0

Source: https://habr.com/ru/post/1752088/


All Articles