2015-09-09 24 views
3

我有這個功能的顯示選擇在多個下拉值:PHP數組到字符串轉換在多個選擇下拉

function _is_dropdown_($name,array $selected=null,$table ,$class,$required,$type_name,$type, $size) 
{ 
     $options = DB::fetch("SELECT name, id FROM " . $table . " WHERE ". $type_name ." = ? ORDER BY name",$type); 
     //$options = array(); 
     /*** begin the select ***/ 
     $dropdown = '<select name="'.$name.'" id="'.$name.'" class="'.$class.'" required = "'.$required.'" size="'.$size.'" multiple>'."\n"; 

     /*** loop over the options ***/ 
     foreach($options as $key=>$option) 
     { 
       /*** assign a selected value ***/ 
       $select = in_array($option, $selected) ? ' selected' : null; 

       /*** add each option to the dropdown ***/ 
       $dropdown .= '<option value="'.$key.'" '.$select.' >'.$option.'</option>'."\n"; 
     } 

     /*** close the select ***/ 
     $dropdown .= '</select>'."\n"; 

     /*** and return the completed dropdown ***/ 
     return $dropdown; 
} 

的結果:

$name = 'book_cover[]'; 
$selected = json_decode($DB_QUERY[0]['book_cover'] , true); 
echo _is_dropdown_($name,$selected ,NEWS_TYPE ,'contentgroup','required','book_type','4', '4'); 

現在,在結果下拉不顯示的option名:

<select name="book_cover[]" class="contentgroup" required = "required" size="4" multiple> 
<option value="0" >Array</option> 
<option value="1" >Array</option> 
<option value="2" >Array</option> 
<option value="3" >Array</option> 
<option value="4" >Array</option> 
<option value="5" >Array</option> 
<option value="6" >Array</option> 
<option value="7" >Array</option> 
<option value="8" >Array</option> 
<option value="9" >Array</option> 
<option value="10" >Array</option> 
<option value="11" >Array</option> 
<option value="12" >Array</option> 
</select> 

並顯示此錯誤:

[2015-09-09 08:28:06] [E_NOTICE] [8] Array to string conversion in C:\xampp\htdocs\cms\class\functions.php:1032 

編輯:我print_r $選項:

(
    [0] => Array 
     (
      [name] => test 
     ) 

    [1] => Array 
     (
      [name] => test2 
     ) 

    [2] => Array 
     (
      [name] => test3 
     ) 
) 

編輯2:我改變$options手動陣列:$options = array('test', 'test1', 'test2');這個工作得很好。現在我需要將my db result轉換爲此。

如何解決這個錯誤?

+0

顯然'$ option'是一個數組。需要查看查詢的結果集才能進一步調試。 – Chris

回答

1

這裏你的關鍵是0,1,2 ...和值

$option = Array 
(
    [name] => test 
) 

因此,而不是$選項使用$選項[「名稱」]。

foreach($options as $key=>$option) { 

     $select = in_array($option["name"], $selected) ? ' selected' : null; 
     $dropdown .= '<option value="'.$key.'" '.$select.' >'.$option["name"].'</option>'."\n"; 

} 
0

你有$options這是行。所以每個$option是一排,一個有2個元素的數組(name, id)

0

更新的功能,請使用:

function _is_dropdown_($name,array $selected=null,$table ,$class,$required,$type_name,$type, $size) 
    { 
      $options = DB::fetch("SELECT name, id FROM " . $table . " WHERE ". $type_name ." = ? ORDER BY name",$type); 
      //$options = array(); 
      /*** begin the select ***/ 
      $dropdown = '<select name="'.$name.'" id="'.$name.'" class="'.$class.'" required = "'.$required.'" size="'.$size.'" multiple>'."\n"; 

      /*** loop over the options ***/ 
      foreach($options as $key=>$option) 
      { 
        /*** assign a selected value ***/ 
        $select = ($key==$selected ? ' selected' : null); 

        /*** add each option to the dropdown ***/ 
        $dropdown .= '<option value="'.$key.'" '.$select.' >'.$option[$key]['name'].'</option>'."\n"; 
      } 

      /*** close the select ***/ 
      $dropdown .= '</select>'."\n"; 

      /*** and return the completed dropdown ***/ 
      return $dropdown; 
    } 
+0

'解析錯誤:語法錯誤,意外'if'(T_IF)in' – Perspolis

+0

語法不正確....更新請檢查 –

+0

相同的結果。看我的編輯。 – Perspolis

0

檢查這個

$dropdown .= '<option value="'.$key.'" '.$select.' >'.$option['name'].'</option>'."\n";