我有一个省份下拉列表,其默认值是根据从SQL中获取的用户省份来确定的:
$getProvince = $db->prepare("
SELECT province
FROM users
WHERE userid = :id
");
$getProvince->execute(array(
':id' => $_SESSION["userident"]
));
$province = $getProvince->fetchColumn();
>Alberta
>British Columbia
>Manitoba
>New Brunswick
>Newfoundland & Labrador
>Nova Scotia
>Ontario
>Prince Edward Island
>Quebec
>Saskatchewan
您可以看到这有多丑.有没有一种方法可以对此进行优化,以使它在眼睛上不会那么难?
解决方法:
将您的选项粘贴在数组中,然后循环html输出.
$provinces = array(
'AB' => 'Alberta',
'BC' => 'British Columbia',
'MB' => 'Manitoba',
//...
);
$province = ''; // your query result
然后输出
<?php foreach ($provinces as $key => $value): ?>
selected="selected"<?php endif; ?>><?php echo $value; ?>
标签:drop-down-menu,html,php
来源: https://codeday.me/bug/20191028/1953291.html