gpt4 book ai didi

在多个选定的下拉列表中将 PHP 数组转换为字符串

转载 作者:搜寻专家 更新时间:2023-10-31 21:28:51 26 4
gpt4 key购买 nike

我有这个功能可以在多个下拉列表中显示选定的值:

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' );

现在在结果下拉列表中不显示选项的名称:

<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 $options:

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

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

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

编辑 2:我将 $options 更改为手动数组:$options = array( 'test', 'test1', 'test2' ); 这很好用。现在我需要将 my db result 转换为此。

如何修复这个错误?!

最佳答案

这里你的键是 0,1,2...,值是

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

所以使用 $option["name"] 代替 $option。

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

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

}

关于在多个选定的下拉列表中将 PHP 数组转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32470694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com