Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9156423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:46:25+00:00 2026-06-17T12:46:25+00:00

I am having a difficult time getting a select box populated with optgroups that

  • 0

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:

$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',
);

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.

Here is my code:

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

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.

Thank you for help on this difficult subject for me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:46:26+00:00Added an answer on June 17, 2026 at 12:46 pm

    Here’s an example of parameter binding using PDO because I can’t stand the binding functions in MySQLi

    <?php
    ini_set('display_errors', 'On');
    error_reporting(E_ALL);
    
    $pdo = new PDO(/* connection string */);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $stmt = $pdo->prepare("
        SELECT `title` FROM `btvb-v4`.`campaigns`
        WHERE `state` = :state
        AND `status` = 'ENABLED'
        AND `live` = 1
        AND `title` NOT LIKE '%mail%'
        AND (`activity` = '' OR `activity` IS NULL)
        ORDER BY `title`
        ");
    $stmt->bindParam('state', $state);
    
    ?>
    
    <select name="campaign">
    
    <?php foreach ($state_list as $state => $value) : ?>
    
        <optgroup label="<?= htmlspecialchars($value, ENT_QUOTES) ?>">
    
        <?php $stmt->execute(); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) : ?>
            <option value="<?= htmlspecialchars($row['title'], ENT_QUOTES) ?>">
                <?= htmlspecialchars($row['title'] ?>
            </option>
        <?php endwhile ?>
    
        </optgroup>
    
    <?php endforeach ?>
    
    </select>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm having a really difficult time getting this to work... Basically - I have
I have downloaded the predicate builder and am having a difficult time getting it
I'm having a difficult time locating an HTML parser that works with JRuby. I've
I am still pretty new to C# and am having a difficult time getting
I'm having a difficult time getting my head around this logic. (I've completely rewritten
I'm having an extremely difficult time getting a private method with arguments to be
I am having a difficult time getting the right positional css for a layout
I'm having a difficult time getting into using mod_rewrite. I've been at this for
I am having a difficult time getting a seemingly simple Regexp. I am trying
I'm having a difficult time getting answers to a few very basic PHP questions.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.