-1

首先我的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>
4

3 回答 3

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 }
于 2013-09-19T13:26:09.890 回答
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>
于 2013-09-19T13:38:28.157 回答
0

如果$land['land']是像'AF,BE,NL,DE'这样的字符串,那么你会得到数组。foreach您可以使用这样的循环数组:

foreach($exp as $val){
  if ($val == 'AF') {/*do something*/}
  if ($val == 'AR') {/*do something*/}
}
于 2013-09-19T13:28:45.207 回答