这里有两张桌子,即工具和工具。
工具表如下所示

  id   name               tools_names                                   quantity    type

  13  cutting player  cutting playerA,cutting playerB,cutting playerC     3       engineer
  12  REFLECTORS      REFLECTORSA,REFLECTORSB                             2         team

工具使用表如下所示
 id     user_id   type        tools
 8      siraj    engineer   cutting playerA,cutting playerB
 7      siraj    team       REFLECTORSB
 6      siraj    team       REFLECTORSA

我想在插入时显示除插入工具使用表之外的工具名称,但如果结果与表中的结果相似,则显示整个工具名称。这是我的控件
public function ajax_tools()
{
    $data['tools']=$this->Tools_model->view_available_tools($_POST['type']);
    foreach ($data['tools'] as $key=>$val) {
    $data['toolss'][] =explode(',',$val['tools_names']);

       }
    $data['tools_names'] = $this->Tools_model->get_tool_names($_POST['type'])->result();
   foreach ($data['tools_names'] as $row)
    {
        if (($key =array_search($row->tools,$data['toolss'])) !== false)
        {
            unset($data['toolss'][$key]);
            $data['toolss'] = array_values($data['toolss']);

        }
    }
    return $data['toolss'];
    $this->load->view('ajax_tools',$data);
}

这是我的模型
public function view_available_tools($type)
{
    $this->db->order_by('id','desc');
    $this->db->where('status',1);
    $this->db->where('type',$type);
    $query=$this->db->get('tools');
    return $query->result_array();
}

public function get_tool_names($type)
{
    return $this->db->get_where('tool_use',array('type'=>$type));
}

这是我的观点
<div class="form-group">
        <label for="type" class="control-label">Type:</label>
        <select name="type" id="type" class="form-control" required>
        <option value="">please select</option>
        <option value="team" <?php echo set_select('type','team'); ?>>Team</option>
        <option value="engineer" <?php echo set_select('type','engineer'); ?>>Engineer</option>
        </select>
      </div>

      <div class="form-group ">
        <label for="tools" class="control-label">Tools:</label>
        <select name="tools[]" id="tools"  multiple="multiple" required>
        <option value="">please select</option>

        </select>
      </div>

<script>
$('#type').change(function(){
var type=$('#type').val();
var url='<?php echo base_url(); ?>tools/tools_control/ajax_tools';
      $.post(url, {type:type}, function(data)
      {

        $('#tools').html(data);
      });
 });
</script>

请帮我解决我的问题

最佳答案

当您array_search时,您正试图搜索$row->tools,它应该包含cutting playerA,cutting playerB。在不包含相同类型逗号分隔值列表但包含其分解版本的数组中搜索(就像在第3行执行explode一样)。

10-07 15:58