drwxg62286 2016-12-06 12:23
浏览 106
已采纳

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

I have in the column size values for each query, e.g. 38 40 42 44, and I want then to select the size field and separate it (using explode()) to give the option to select after a select tag html. I used explode() to split the number after black space but the code is not giving me all the results in multiple rows

$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>

but the code is showing me only the first value like 38...And I want to show how many numbers are in the value. Show for each a select tag with the value, to give the option to be selected by the user. I don't know what i missed.

EDIT Thanks to ddp I fixed the problem, it was with the closing the loop function too early.

  • 写回答

3条回答 默认 最新

  • dqzuo0327 2016-12-06 12:33
    关注
    //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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部