PHP:逗号分隔的字符串到while循环中的html元素?

标签 php mysql

我正在尝试从存储在 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/

相关文章:

mysql - IF ELSE里面加载数据infile mysql

php - 使用 php 随机刷新 1000 次

php - 在数据表中执行操作后返回上一页

php - SQL 查询按组排序

mysql - SQL 简单连接条件

mysql - 如何编译MySQL "Words"的完整列表

php - 如何使用 View 按钮在 Controller 中调整 foreach 的速度?

php - array_unique 导致 undefined offset 错误

mysql - 有没有办法查询一个类是否包含具有某个已知名称的实例变量?

mysql - SQL分区在mysql中使用 'not exists'