如何从列表菜单中排除某些国家/地区?现在我的代码将列出数据库中的所有国家/地区名称。
示例我想从列表菜单中排除阿尔巴尼亚国家/地区。如何根据这些代码实现它。
代码
<?php $select_country=mysql_query("SELECT * FROM tbl_country ORDER BY country_name ASC")?>
<select name="country" onchange="return get_country(this.value);">
<option value=""><?=SELECT_COUNTRY?></option>
<? while($country=mysql_fetch_array($select_country)) {?>
<option <? if($_SESSION['getcountry']==$country['id']){ ?> selected="selected"<? }?> value="<?=$country['id']?>">
<?=$country['country_name']?></option>
<? } ?>
</select>
Mysql的
id country_name
1 Afghanistan
2 Aland Islands
3 Albania
4 Algeria
5 American Samoa
6 Andorra
答案 0 :(得分:6)
从结果集中排除该行:
SELECT * FROM tbl_country WHERE country_name != "Albania" ORDER BY country_name ASC
或者你用PHP跳过它:
<?php
while($country=mysql_fetch_array($select_country)) {
if ($country['country_name'] == 'Albania') {
continue;
} ?>
<option <?php if($_SESSION['getcountry']==$country['id']){ ?> selected="selected"<? }?> value="<?=$country['id']?>"><?=$country['country_name']?></option>
<?php } ?>
答案 1 :(得分:2)
"SELECT * FROM tbl_country WHERE country_name <> "Albania" ORDER BY country_name ASC"
“&lt;&gt;”相当于“!=”并且它们都意味着“不等于”,理论上应该从表中选择country_name不等于定义的“阿尔巴尼亚”的所有记录。
我认为以下作品也是:
"SELECT * FROM tbl_country WHERE country_name NOT IN("Albania","other_country_name", "another_country_name") ORDER BY country_name ASC"
答案 2 :(得分:1)
如果我理解了你的问题:
mysql_query("SELECT * FROM tbl_country WHERE country_name <> 'Algeria' ORDER BY country_name ASC")
答案 3 :(得分:1)
您可以设置要排除的国家/地区的数组...
<?php
$excludeArray = array('Albania','United States') ;
$select_country=mysql_query("SELECT * FROM tbl_country ORDER BY country_name ASC");
echo <<<HTML
<select name="country" onchange="return get_country(this.value);">
<option value=""><?=SELECT_COUNTRY?></option>
HTML;
while($country=mysql_fetch_array($select_country)) {
if (!in_array($country['country_name'],$excludeArray)) {
$selected = ($_SESSION['getcountry']==$country['id'] ? 'selected="selected"' : '');
echo "<option value='" . $country['id'] . "' " . $selected . ">" . $country['country_name'] . "</option>" ;
}
echo "</select>";
?>
答案 4 :(得分:1)
<?php $select_country=mysql_query("SELECT * FROM tbl_country ORDER BY country_name ASC")?>
<select name="country" onchange="return get_country(this.value);">
<option value=""><?=SELECT_COUNTRY?></option>
<?php
$exclude = array('Afghanistan', 'Andorra');
while($country=mysql_fetch_array($select_country)) {
if(!in_array($country['name'], $exclude){
?>
<option <? if($_SESSION['getcountry']==$country['id']){ ?> selected="selected"<? }?> value="<?=$country['id']?>"><?=$country['country_name']?></option>
<? }} ?>
</select>
答案 5 :(得分:-1)
为什么不在数据库上创建状态呢?例如SELECT FROM country WHERE status='show'