[英]PHP Array to string conversion in multiple selected dropdown
I have this function for show selected value in multiple dropdown: 我有这个函数用于在多个下拉列表中显示所选值:
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;
}
for result: 结果:
$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' );
Now in result dropdown not show name of option
: 现在结果下拉列表中没有显示
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>
And show this error : 并显示此错误:
[2015-09-09 08:28:06] [E_NOTICE] [8] Array to string conversion in C:\xampp\htdocs\cms\class\functions.php:1032
EDIT : i print_r
$options: 编辑 :我
print_r
$选项:
(
[0] => Array
(
[name] => test
)
[1] => Array
(
[name] => test2
)
[2] => Array
(
[name] => test3
)
)
EDIT 2 : i change $options
to manual array : $options = array( 'test', 'test1', 'test2' );
编辑2 :我将
$options
更改$options
手动数组: $options = array( 'test', 'test1', 'test2' );
this worked fine. 这工作得很好。 now i need to convert
my db result
to this. 现在我需要将
my db result
转换为此。
how do fix this error ?! 如何修复此错误?!
Here your key are 0,1,2... and values are 这里你的关键是0,1,2 ...而值是
$option = Array
(
[name] => test
)
So instead of $option use $option["name"]. 因此,而不是$ option使用$ option [“name”]。
foreach($options as $key=>$option ) {
$select = in_array( $option["name"], $selected ) ? ' selected' : null;
$dropdown .= '<option value="'.$key.'" '.$select.' >'.$option["name"].'</option>'."\n";
}
You have $options
which are rows. 你有
$options
是行。 So each $option
is a row, an array with 2 elements (name, id)
. 所以每个
$option
都是一行,一个包含2个元素(name, id)
的数组。
Updated the function, Please use this: 更新了功能,请使用:
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;
}
检查一下
$dropdown .= '<option value="'.$key.'" '.$select.' >'.$option['name'].'</option>'."\n";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.