如何使用 explode() 将查询值拆分为 select 标记的多行

How to split a query value into several mutiple rows for a select tag using explode()

我在列 size 中有每个查询的值,例如38 40 42 44,然后我想 select size 字段并将其分开(使用 explode())以在 select 标签 html。 我用 explode() 在黑色 space 之后拆分数字,但代码没有给我多行的所有结果

$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
        while ($row = mysql_fetch_array($sql)) {
            $product_name = $row["product_name"];
            $price = $row["price"];
            $size = $row["size"];    
$myArray = explode(' ',  $size);
            foreach($myArray as $my_Array){
                            }

<select name="size"><option value="'.$my_Array.'">'.$my_Array.'</option>
<select name="size"><option value="'.$my_Array.'">'.$my_Array.'</option>

但代码只向我显示第一个值,例如 38...我想显示该值中有多少个数字。为每个显示带有值的 select 标签,以提供由用户 select 编辑的选项。我不知道我错过了什么。

编辑 多亏了ddp我解决了这个问题,它是关闭循环功能太早了。

问题出在你的 foreach($list as $value){ ... } 你将循环命名为与列表相同,它可能会被重写。

foreach 循环结束 } 在错误的地方。 你在循环中什么都不做。

//assuming that `size` is stored as 38 40 42 in the same row
//also assuming your LIMIT 1 is for testing, if not, you don't need the loop
/** PLEASE CHANGE TO MYSQLI OR PDO! mysql_ is depreciated and a huge security risk **/ 
$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
    while ($row = mysql_fetch_array($sql)) {
        $product_name = $row["product_name"];
        $price = $row["price"];
        $size = $row["size"];    
        myArray = explode(' ',  $size);
        //heres your issue, declare select to start with
        echo '<select name="size">';
        foreach($myArray as $my_Array){
             //the out put from your explode loop array needs to go here
             echo '<option value="'.$my_Array.'">'.$my_Array.'</option>';
        }
        echo '</select>';
}//close the while loop