2016-12-06 27 views
0

我有在列大小對於每個查詢值,例如38 40 42 44,然後我想要選擇大小的字段字段並將其分開(使用explode())以提供選擇標記html後的選項。 我以前explode()拆分後的黑色空間的數量,但代碼不給我所有的結果在多行如何分割一個查詢值成若干行多發性使用爆炸()一個選擇標籤

$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第一個值......我想顯示值中有多少個數字。爲每個選擇標籤顯示值,以便讓用戶選擇該選項。我不知道我錯過了什麼。

編輯 感謝ddp我解決了這一問題,它是與太早關閉循環功能。

+0

爲什麼mysql的標籤,如果沒有代碼?編輯:它被刪除[編輯](http://stackoverflow.com/revisions/41004176/2)。如果與mysql有關聯,請再次添加您的代碼和(mysql)標籤。 –

+0

不要把代碼中的註釋,編輯你的問題,而不是http://stackoverflow.com/posts/41004176/edit –

+0

好吧對不起我因此未知道,我是新手我編輯,把MySQL的代碼,但問題不在於與MySQL – Stefan

回答

1
//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 
+0

謝謝你的幫助,是的,你說得對,我從

1

的問題是在你的foreach($list as $value){ ... }你被點名的循環一樣的列表,它可能rewrited。

0

foreach循環結束}出錯了。 你不會在循環中做任何事情。