-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

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 に答える
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 に答える