Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用php中的选定值填充multiselect?_Php_Codeigniter_Jquery Chosen - Fatal编程技术网

如何用php中的选定值填充multiselect?

如何用php中的选定值填充multiselect?,php,codeigniter,jquery-chosen,Php,Codeigniter,Jquery Chosen,我正在使用codeigniter进行开发。我需要用选定的值填充multiselect 这是我的json数组(需要选择): 这是我的php代码: function tag($selected=0){ $query = $this->db->get('tags'); $html=''; foreach($query->result() as $row){ $html .='<option value="'.$row->id.'"

我正在使用codeigniter进行开发。我需要用选定的值填充multiselect

这是我的json数组(需要选择):

这是我的php代码:

function tag($selected=0){
    $query = $this->db->get('tags');
    $html='';
    foreach($query->result() as $row){

        $html .='<option value="'.$row->id.'">'.$row->tag.'</option>';


    }
    return $html;
}
功能标签($selected=0){
$query=$this->db->get('tags');
$html='';
foreach($query->result()作为$row){
$html.=''.$row->tag';
}
返回$html;
}
$selected包含一个json数组。现在我需要在json数组中填充Competite标记列表和选定项

我怎么做? 有人能告诉我这里使用什么方法吗?

函数标记($selected=null){
function tag($selected = null){
    $selected = ($selected === null) ? array() : json_decode($selected, true);
    $query = $this->db->get('tags');
    $html='';
    foreach($query->result() as $row){
        $isSelected = (in_array($row->id, $selected)) ? ' selected="selected"' : '';
        $html .='<option value="'.$row->id.'"'.$isSelected.'>'.$row->tag.'</option>';


    }
    return $html;
}
$selected=($selected==null)?array():json_decode($selected,true); $query=$this->db->get('tags'); $html=''; foreach($query->result()作为$row){ $isSelected=(在数组($row->id,$selected))中?'selected=“selected”:''; $html.=''.$row->tag'; } 返回$html; }
功能标签($selected=null){
$selected=($selected==null)?array():json_decode($selected,true);
$query=$this->db->get('tags');
$html='';
foreach($query->result()作为$row){
$isSelected=(在数组($row->id,$selected))中?'selected=“selected”:'';
$html.=''.$row->tag';
}
返回$html;
}

您的json数组是什么样子的?我正在保存json数组,如[“1”、“2”、“3”、“4”],这是标记的id,您到底想如何使用该数组?我不明白。我的表名是video,用户可以为每个视频添加标签。标记是由管理员添加的,现在我将标记id保存为json数组。当用户编辑他的视频标签时,我需要在multiselect中显示当前选定的标签和可用的标签。我正在使用selected,它是ui multiselect的javascript插件。我正在保存json数组,如[“1”、“2”、“3”、“4”],它是标签的id,您到底想如何使用该数组?我不明白。我的表名是video,用户可以为每个视频添加标签。标记是由管理员添加的,现在我将标记id保存为json数组。当用户编辑他的视频标签时,我需要在multiselect中显示当前选定的标签和可用的标签。我使用的是selected,它是ui multiselect的javascript插件。我检查了您的答案,但发现了错误,就像遇到了PHP错误一样。严重性:警告消息:in_array()希望参数2为array,字符串给定文件名:models/common.php行号:92我还可以看到数组像数组一样打印([0]=>1[1]=>2[2]=>3)哦,对不起,我使用了相同的变量两次。编辑了答案是的,我找到了并修复了:)我检查了你的答案,但遇到了PHP错误之类的错误严重性:警告消息:in_array()期望参数2是数组,字符串给定文件名:models/common.PHP行号:92我还可以看到数组像数组一样打印([0]=>1[1]=>2[2]=>3)哦,对不起,我用了两次相同的变量。编辑了答案是的,我找到并修复了:)
function tag($selected = null){
    $selected = ($selected === null) ? array() : json_decode($selected, true);
    $query = $this->db->get('tags');
    $html='';
    foreach($query->result() as $row){
        $isSelected = (in_array($row->id, $selected)) ? ' selected="selected"' : '';
        $html .='<option value="'.$row->id.'"'.$isSelected.'>'.$row->tag.'</option>';


    }
    return $html;
}