如何使用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.
我在每个查询的 size em>列中都有,例如 38 40 42 44,然后我想选择 size em>字段并将其分开(使用explode())以提供选择标签html之后的选项。
I使用 但代码只显示第一个值,如 编辑 strong>
感谢 ddp 我解决了这个问题,过早关闭了循环函数。 p>
div> explode( ) code>在黑色空格后分割数字但是代码没有给我多行中的所有结果 p>
$ 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){
}
&lt; select name =“size”&gt;&lt; option value =“'。$ my_Array。'“&gt;'。$ my_Array。'&lt; / option&gt;
&lt; select name =”size“&gt;&lt; option value =”'。$ my_Array。'“&gt;'。$ my_Array。'&lt ; / option&gt;
code> pre>
38 code> ...而且我想显示多少 数字在价值中。 显示每个带有值的选择标记,以提供用户选择的选项。 我不知道我错过了什么。 p>
//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
The problem is in your foreach($list as $value){ ... }
you named the loop the same as the list and it might be rewrited.
The foreach loop ending } is in to wrong place. You do not do anything in the loop.