如何显示数据库中的选定值并在php中的多个选择框中显示

问题描述 投票:0回答:1

我有一个类似数组

$a = array("A", "B", "C");

我已使用多次选择将值存储在数据库中。

保存的值是2个不同行中的"B", "C"

我需要在相同的多重选择框中显示所选值"b""D"值。

这里困难的概念是我需要如下显示数组中select选项的值

   foreach($dbRows as $dbRow) {
       // here if i display the selected values using if condition the value are selected by the array values repeats like
        a - no selected
        b - selected
        c - no selected

again loops repeats like

       a - no selected
       b - no selected
       c - selected

   }
}

如何不重复显示值?

php html-select
1个回答
0
投票

您可以循环选择框数组,并使用in_array()检查其值是否在DB数组中]

$a = array("A", "B", "C");

foreach($a as $v)
{
    $selected = in_array($v, $db_array) ? 'selected' : '';
}
© www.soinside.com 2019 - 2024. All rights reserved.