首先我的mysql表格是:NL,BE,DE,AF 我想首先爆炸比我想要爆炸的单词,如果它等于选项值而不是选择那些选项
$resultxx = mysql_query("SELECT * FROM page where page_id = '$page_id'") or die(mysql_error());
$number=mysql_num_rows($resultxx);
while($land = mysql_fetch_array($resultxx)){
$exp = explode(',', $land['land']);
}
$count = 0;
//Check if space exists in substrings
foreach ($exp as $code) {
if (strpos(trim($code), ' ') == false) { //strpos better for checking existance
$count++;
$land = constant(COUNTRY_.$code);
?>
<option selected value="<?php echo $code; ?>"><?php echo $land; ?></option>
<?php
}}
?>
</select>
答案 0 :(得分:1)
试试这个
<select class="multi" multiple="multiple" id="my-select" name="my-select[]">
<?php
$resultxx = mysql_query("SELECT * FROM page where page_id = 1") or
die(mysql_error());
$number=mysql_num_rows($resultxx);
while($land = mysql_fetch_array($resultxx)){
$exp = explode(',', $land['land']);
?>
<option <?php if(in_array("AF",$exp)) echo "selected='selected'"; ?> value="AF"><?php echo COUNTRY_AF; ?></option>
<option <?php if(in_array("NL",$exp)) echo "selected='selected'"; ?> value="NL"><?php echo COUNTRY_NL; ?></option>
<option <?php if(in_array("DE",$exp)) echo "selected='selected'"; ?> value="DE"><?php echo COUNTRY_DE; ?></option>
</select>
<?php }
答案 1 :(得分:0)
如果$land['land']
是一个像'AF,BE,NL,DE'这样的字符串,那么你得到数组。您可以使用foreach
循环遍历数组:
foreach($exp as $val){
if ($val == 'AF') {/*do something*/}
if ($val == 'AR') {/*do something*/}
}
答案 2 :(得分:0)
也许这可以做到吗?
只需确保定义数组$countries
中的所有国家/地区。你怎么做并不重要,但由于我不知道你在哪里获得国家名单,我把它定在最顶层。
<select class="multi" multiple="multiple" id="my-select" name="my-select[]">
<?php
$countries = array('AF', 'NL', 'DE', 'BE');
$resultxx = mysql_query("SELECT * FROM page where page_id = 1") or
die(mysql_error());
$number=mysql_num_rows($resultxx);
while($land = mysql_fetch_array($resultxx)){
$exp = explode(',', $land['land']);
for($i = 0; $i <= count($countries); $i++){
?>
<option<?php echo in_array($countries[$i], $exp) ? ' selected' : '' ?> value="<? print $countries[$i]; ?>">
<?php echo constant('COUNTRY_'.$countries[$i]); ?>
</option>
<?php
}
}
?>
</select>