我正在制作一个函数,其中我以逗号分隔的单词形式从 mysql 表中获取结果。我使用 explode 将它们分开,然后尝试在 for 循环的 if 和 else 语句中进行条件检查。但是条件检查不起作用我试图回显来自数据库的结果,结果是V
并且在我的函数中有条件检查V
但它在这里不起作用是我的代码
function partner_diet($page,$user_id)
{
$getrow=mysql_query("SELECT * from partner_prefrences where uid='$user_id'");
$results=mysql_fetch_array($getrow);
$string=$results['diet'];
$string = preg_replace('/\.$/', '', $string); //Remove dot at end if exists
$array = explode(',', $string); //split string into array seperated by ', '
//loop over values
?>
<select name="mipdiet" class="mipdiet" multiple="multiple">
<option <?php foreach($array as $value) { if($value=='V') {?> checked <?php } } ?> name="pdiet" value="V"> Vegetarian </option>
<option <?php foreach($array as $value) { if($value=='N') {?> checked <?php } } ?> name="pdiet" value="N"> Non Vegetarian </option>
<option <?php foreach($array as $value) { if($value=='J') {?> checked <?php } } ?> name="pdiet" value="J"> Jain </option>
<option <?php foreach($array as $value) { if($value=='E') {?> checked <?php } } ?> name="pdiet" value="E"> Eggetarian </option>
</select>
<?php
}