来自数组的php optgroup

时间:2013-01-21 02:17:35

标签: php optgroup

我很难获得一个填充了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',
);

由此我尝试使用与数据库中的值匹配的键填充选择框,并且具有该状态键的每个记录将是该optgroup的选项。

这是我的代码:

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

正如你所知,我没有得到这个代码的任何地方,我很难过如何解决这个问题。有关理解我需要做什么的任何帮助,以便密钥与表中的状态匹配,然后使用与该密钥相关联的记录填充optgroups作为适当的optgroup下面的选项。

感谢您对我这个困难主题的帮助。

2 个答案:

答案 0 :(得分:1)

以下是使用PDO进行参数绑定的示例,因为我无法忍受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>

答案 1 :(得分:1)

看起来$key周围的单/双引号是错误的/向后的 -

$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)");
                                                                                         ^^        ^^        

尝试切换到 -

$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)");