我正在尝试从存储在 MYSQL 数据库中的逗号分隔字符串创建单选按钮。
但是,我只得到逗号分隔字符串的最后一个值,因此它只创建了 1 个单选按钮,而实际上它应该创建多个单选按钮,因为有这样的字符串:
1,2,3,4
我的 PHP mysql 查询如下所示:
list = "";
$sql2 ="SELECT * FROM table";
$query2 = mysqli_query($db_conx, $sql2);
$productCount = mysqli_num_rows($query2); // count the output amount
if ($productCount > 0) {
while($row = mysqli_fetch_array($query2, MYSQLI_ASSOC)){
$column = $row["column"];
$drawes ="";
$mark=explode(",", $Draw);
foreach($mark as $out) {
$drawes = '<label for="one">
<input type="radio" id="one" name="duration" value="1" />
'.$out.'
</label>';
}
list .= '<div style="width:100%; height:auto; border-bottom:solid 1px #CCC; padding-bottom:10px;padding-top:10px;">
'.$drawes.'
</div>';
}
}
我 echo $list;
在我的页面上,但我的页面上只有 1 个单选按钮,字符串的最后一个值如下所示:
<label for="one">
<input type="radio" id="one" name="duration" value="1" />
4
</label>
有人可以就这个问题提出建议吗?
提前致谢。
最佳答案
您正在循环中对 $drawes
进行赋值。变量的当前内容被覆盖。
您似乎想连接 到现有值。
PHP 连接赋值运算符是“.=
”。
$a = 'this';
$a .= 'and something else';
echo $a;
关于PHP:逗号分隔的字符串到while循环中的html元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35019901/