drwxg62286 2016-12-06 20: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 20: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条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?