php optgroup from array
<p>I am having a difficult time getting a select box populated with optgroups that then have a listing of records associated with the optgroup. The optgroups come from the array below:</p>

<pre><code>$state_list = array(
'AL'=&gt;"Alabama",
'AK'=&gt;"Alaska",
'AZ'=&gt;"Arizona",
'AR'=&gt;"Arkansas",
'CA'=&gt;"California"
'CO'=&gt;"Colorado", 
'CT'=&gt;"Connecticut",
'DE'=&gt;"Delaware",
'DC'=&gt;"District Of Columbia",
'FL'=&gt;"Florida",
'GA'=&gt;"Georgia",
'HI'=&gt;"Hawaii",
'ID'=&gt;"Idaho",
'IL'=&gt;"Illinois",
'IN'=&gt;"Indiana",
'IA'=&gt;"Iowa",
'KS'=&gt;"Kansas",
'KY'=&gt;"Kentucky", 
'LA'=&gt;"Louisiana",
'ME'=&gt;"Maine",
'MD'=&gt;"Maryland",
'MA'=&gt;"Massachusetts",
'MI'=&gt;"Michigan",
'MN'=&gt;"Minnesota",
'MS'=&gt;"Mississippi",
'MO'=&gt;"Missouri",
'MT'=&gt;"Montana",
'NE'=&gt;"Nebraska",
'NV'=&gt;"Nevada",
'NH'=&gt;"New Hampshire",
'NJ'=&gt;"New Jersey",
'NM'=&gt;"New Mexico",
'NY'=&gt;"New York",
'NC'=&gt;"North Carolina",
'ND'=&gt;"North Dakota",
'OH'=&gt;"Ohio",
'OK'=&gt;"Oklahoma",
'OR'=&gt;"Oregon",
'PA'=&gt;"Pennsylvania",
'RI'=&gt;"Rhode Island",
'SC'=&gt;"South Carolina",
'SD'=&gt;"South Dakota",
'TN'=&gt;"Tennessee",
'TX'=&gt;"Texas",
'UT'=&gt;"Utah",
'VT'=&gt;"Vermont",
'VA'=&gt;"Virginia",
'WA'=&gt;"Washington",
'WV'=&gt;"West Virginia",
'WI'=&gt;"Wisconsin",
'WY'=&gt;"Wyoming",
'VI'=&gt;'US Virgin Islands',
'AB'=&gt;'Alberta',
'BC'=&gt;'British Columbia', 
'MB'=&gt;'Manitoba',
'NB'=&gt;'New Brunswick',
'NL'=&gt;'Newfoundland/Labrador',
'NT'=&gt;'Northwest Territories',
'NS'=&gt;'Nova Scotia',
'NU'=&gt;'Nunavut',
'ON'=&gt;'Ontario',
'PE'=&gt;'Prince Edward Island',
'QC'=&gt;'Quebec',
'SK'=&gt;'Saskatchewan',
'YT'=&gt;'Yukon Territory',
);
</code></pre>

<p>From this I try to populate the select box with the keys matching the values in the database and each of the records that have that state key will be an option for that optgroup.</p>

<p>Here is my code:</p>

<pre><code>// We are going to go through each of the states in the array
foreach($state_list as $key =&gt; $value) {
  // we are going to set the value of the state
  echo '&lt;optgroup label="' . $value. '"&gt;';
  // 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 '&lt;option value="'.$row_market['title'].'"&gt;'.$row_market['title']'&lt;/option&gt;';
  }
  echo '&lt;/optgroup&gt;';
}
</code></pre>

<p>As you can tell I am not getting anywhere with this code and I am stumped on how to solve this. Any help on understanding what I need to do so that the keys will match the states in the table and then populate optgroups with records associated with that key being an option underneath the approprate optgroup.</p>

<p>Thank you for help on this difficult subject for me.</p>
