在表中选择一个介于2个逗号之间的单词php不工作
首先,我的mysql表是:NL、BE、DE、AF 如果分解的单词等于选项值,则首先分解,然后选择该选项在表中选择一个介于2个逗号之间的单词php不工作,php,mysql,Php,Mysql,首先,我的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 = ex
$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>
$resultxx=mysql\u query(“从页面中选择*,页面id='$page\u id')或die(mysql\u error());
$number=mysql\u num\u行($resultxx);
而($land=mysql\u fetch\u数组($resultxx)){
$exp=爆炸(“,”,$land['land']);
}
$count=0;
//检查子字符串中是否存在空格
foreach($exp作为$code){
if(strpos(trim($code),“”)=false){//strpos更适合检查存在性
$count++;
$land=常数(国家/地区代码);
?>
试试这个
<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 }
如果$land['land']
是一个类似于'AF,BE,NL,DE'的字符串,则得到数组。您可以使用foreach
循环数组,如下所示:
foreach($exp as $val){
if ($val == 'AF') {/*do something*/}
if ($val == 'AR') {/*do something*/}
}
也许这可以做到?
只需确保您在一个数组中定义了所有国家/地区。$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>
什么是不工作
意思?你应该研究一下在准备语句中使用MySQLi或PDO,旧的mysql_uuuu函数已经被弃用。不使用selected。我想选择所有de选项都等于分解词。它是一个多选下拉列表,是COUNTRY_uaf等字符串?或者你在哪里设置它们?还有使用第二个while循环,您将为$exp中的每个值打印3个选项。这并不是说这有什么区别,而是您可以使用foreach循环,而不是第二个。请避免使用mysql,因为它已经不推荐使用,并且将在较新版本的php中删除。请改用mysqli或pdo。