Reputation: 4318
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.
Upvotes: 1
Views: 1111
Reputation: 164752
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>
Upvotes: 1
Reputation: 12433
It looks like your single/double quotes around $key
are wrong/backward -
$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)");
^^ ^^
try switching to -
$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)");
Upvotes: 1