我想为一个动态生成的下拉菜单编写一个IF语句,但是我不断地得到错误,或者它不仅仅起作用,我要做的是比较两个变量,看看它们是否与数据库中的数据匹配
$loma=“Asokoro”;
$row['locales']来自数据库中的locality表
$row['locales']=“阿索科罗”;
$row['locales']=“Dutse”;
$row['locales']=“马里”;
$row['locales']=“烹饪”;
这意味着如果$loma是Asokoro匹配$row['locales']=“Asokoro”;则选择它作为选项菜单

<select name="checkout_area_name" id="checkout_area_name" required>
    $query = "SELECT * FROM `locality` WHERE state_name = '$hi_state'";
    $sql = mysqli_query($con, $query) or die(mysqli_error($con, $query));
    $r = ' <option value="">Please Choose Locality</option>';
    ?>
    <?php
    while ( $row = mysqli_fetch_assoc($sql))
    {
        ?>
        <?php $r = $r . '<option value="'.$loma.'" if ("'.$loma.' == '.$row["locales"].'") selected="selected" >'.$row['locales'].'</option>'; ?>
        <?php
    }
    echo $r;
    ?>
</select>

我正试图选择与$loma$row['locales']匹配的选项菜单,但我一直收到错误,或者当我console.log时,它不会产生我想要的结果。

最佳答案

将php脚本输出为html标记,请尝试将代码更改为:

<select name="checkout_area_name" id="checkout_area_name" required>
  $query = "SELECT * FROM `locality` WHERE state_name = '$hi_state'"; $sql = mysqli_query($con, $query) or die(mysqli_error($con, $query)); $r = '
  <option value="">Please Choose Locality</option>'; ?>
  <?php
    while ( $row = mysqli_fetch_assoc($sql))
    {
      $r = $r . '<option value="'.$loma.'" '.(($loma==$row["locales"])?'selected':'').'>'.$row['locales'].'</option>';
    }
    echo $r;
    ?>
</select>

关于php - 在动态下拉选择菜单中写入IF语句会出现错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57032480/

10-14 00:23