我很难得到一个填充了 optgroup 的选择框,然后有一个与 optgroup 关联的记录列表。optgroups 来自以下数组:
$state_list = array(
'AL'=>"Alabama",
'AK'=>"Alaska",
'AZ'=>"Arizona",
'AR'=>"Arkansas",
'CA'=>"California"
'CO'=>"Colorado",
'CT'=>"Connecticut",
'DE'=>"Delaware",
'DC'=>"District Of Columbia",
'FL'=>"Florida",
'GA'=>"Georgia",
'HI'=>"Hawaii",
'ID'=>"Idaho",
'IL'=>"Illinois",
'IN'=>"Indiana",
'IA'=>"Iowa",
'KS'=>"Kansas",
'KY'=>"Kentucky",
'LA'=>"Louisiana",
'ME'=>"Maine",
'MD'=>"Maryland",
'MA'=>"Massachusetts",
'MI'=>"Michigan",
'MN'=>"Minnesota",
'MS'=>"Mississippi",
'MO'=>"Missouri",
'MT'=>"Montana",
'NE'=>"Nebraska",
'NV'=>"Nevada",
'NH'=>"New Hampshire",
'NJ'=>"New Jersey",
'NM'=>"New Mexico",
'NY'=>"New York",
'NC'=>"North Carolina",
'ND'=>"North Dakota",
'OH'=>"Ohio",
'OK'=>"Oklahoma",
'OR'=>"Oregon",
'PA'=>"Pennsylvania",
'RI'=>"Rhode Island",
'SC'=>"South Carolina",
'SD'=>"South Dakota",
'TN'=>"Tennessee",
'TX'=>"Texas",
'UT'=>"Utah",
'VT'=>"Vermont",
'VA'=>"Virginia",
'WA'=>"Washington",
'WV'=>"West Virginia",
'WI'=>"Wisconsin",
'WY'=>"Wyoming",
'VI'=>'US Virgin Islands',
'AB'=>'Alberta',
'BC'=>'British Columbia',
'MB'=>'Manitoba',
'NB'=>'New Brunswick',
'NL'=>'Newfoundland/Labrador',
'NT'=>'Northwest Territories',
'NS'=>'Nova Scotia',
'NU'=>'Nunavut',
'ON'=>'Ontario',
'PE'=>'Prince Edward Island',
'QC'=>'Quebec',
'SK'=>'Saskatchewan',
'YT'=>'Yukon Territory',
);
从这里我尝试使用与数据库中的值匹配的键填充选择框,并且每个具有该状态键的记录都将成为该选项组的一个选项。
这是我的代码:
// We are going to go through each of the states in the array
foreach($state_list as $key => $value) {
// we are going to set the value of the state
echo '<optgroup label="' . $value. '">';
// for each of the states we are going to find the markets that are associated with each of the states
$market = mysqli_query($link, "select `title` from `ntvb-v4`.`campaigns` where `state` = "' .$key. '" AND `status` = 'ENABLED' AND `live` = '1' AND `title` NOT LIKE '%mail%' AND (`activity` = '' OR `activity` IS NULL)");
while($row_market = mysqli_fetch_assoc($market)) {
// now we need to add option for each of the markets
echo '<option value="'.$row_market['title'].'">'.$row_market['title']'</option>';
}
echo '</optgroup>';
}
正如你所知道的,我对这段代码没有任何帮助,我对如何解决这个问题感到困惑。任何有助于理解我需要做什么,以便键与表中的状态匹配,然后使用与该键关联的记录填充 optgroup,这是适当 optgroup 下的一个选项。
感谢您在这个困难的主题上为我提供的帮助。