PHP 数组:<select> 框和 <optgroup>
PHP Array: <select> box and <optgroup>
我正在尝试编写一个脚本,让您可以生成每个国家/地区的城市。城市应通过 PHP 数组按国家/地区分组。
所以这里的这个例子,在 HTML 中,这是我想要的,但在 PHP 数组中。
<select>
<optgroup label="Country X">
<option value="City1">City 1</option>
<option value="City2">City 2</option>
<option value="City3">City 3</option>
<option value="City4">City 4</option>
<option value="City5">City 5</option>
</optgroup>
<optgroup label="Country Y">
<option value="City6">City 6</option>
<option value="City7">City 7</option>
<option value="City8">City 8</option>
<option value="City9">City 9</option>
<option value="City10">City 10</option>
</optgroup>
<optgroup label="Country Z">
<option value="City11">City 11</option>
<option value="City12">City 12</option>
<option value="City13">City 13</option>
<option value="City14">City 14</option>
<option value="City15">City 15</option>
</optgroup>
</select>
这是我到目前为止用 PHP 所做的代码...这个的主要问题是每个国家都有所有城市。我只想要不同国家的 5 个城市。
<?php
$countries = array(
'Country X',
'Country Y',
'Country Z',
);
$cities = array(
'City 1',
'City 2',
'City 3',
'City 4',
'City 5',
'City 6',
'City 7',
'City 8',
'City 9',
'City 10',
'City 11',
'City 12',
'City 13',
'City 14',
'City 15',
);
?>
<select name="places">
<?php foreach($countries as $key): ?>
<optgroup label="<?php echo $key; ?>">
<?php foreach($cities as $key => $value): ?>
<?php echo '<option value="'.$key.'">'.$value.'</option>'; ?>
<?php endforeach; ?>
</optgroup>
<?php endforeach; ?>
</select>
分块分割城市:
$cities = array_chunk($cities, 5);
$counter = 0;?>
<select name="places">
<?php foreach($countries as $key): ?>
<optgroup label="<?php echo $key; ?>">
<?php foreach($cities[$counter++] as $key => $value): ?>
<?php echo '<option value="'.$key.'">'.$value.'</option>'; ?>
<?php endforeach; ?>
</optgroup>
<?php endforeach; ?>
</select>
我正在尝试编写一个脚本,让您可以生成每个国家/地区的城市。城市应通过 PHP 数组按国家/地区分组。
所以这里的这个例子,在 HTML 中,这是我想要的,但在 PHP 数组中。
<select>
<optgroup label="Country X">
<option value="City1">City 1</option>
<option value="City2">City 2</option>
<option value="City3">City 3</option>
<option value="City4">City 4</option>
<option value="City5">City 5</option>
</optgroup>
<optgroup label="Country Y">
<option value="City6">City 6</option>
<option value="City7">City 7</option>
<option value="City8">City 8</option>
<option value="City9">City 9</option>
<option value="City10">City 10</option>
</optgroup>
<optgroup label="Country Z">
<option value="City11">City 11</option>
<option value="City12">City 12</option>
<option value="City13">City 13</option>
<option value="City14">City 14</option>
<option value="City15">City 15</option>
</optgroup>
</select>
这是我到目前为止用 PHP 所做的代码...这个的主要问题是每个国家都有所有城市。我只想要不同国家的 5 个城市。
<?php
$countries = array(
'Country X',
'Country Y',
'Country Z',
);
$cities = array(
'City 1',
'City 2',
'City 3',
'City 4',
'City 5',
'City 6',
'City 7',
'City 8',
'City 9',
'City 10',
'City 11',
'City 12',
'City 13',
'City 14',
'City 15',
);
?>
<select name="places">
<?php foreach($countries as $key): ?>
<optgroup label="<?php echo $key; ?>">
<?php foreach($cities as $key => $value): ?>
<?php echo '<option value="'.$key.'">'.$value.'</option>'; ?>
<?php endforeach; ?>
</optgroup>
<?php endforeach; ?>
</select>
分块分割城市:
$cities = array_chunk($cities, 5);
$counter = 0;?>
<select name="places">
<?php foreach($countries as $key): ?>
<optgroup label="<?php echo $key; ?>">
<?php foreach($cities[$counter++] as $key => $value): ?>
<?php echo '<option value="'.$key.'">'.$value.'</option>'; ?>
<?php endforeach; ?>
</optgroup>
<?php endforeach; ?>
</select>