array_search函数不返回值

时间:2016-12-13 09:57:43

标签: php mysql arrays codeigniter-3

这里有两个表,即tools和tool_use。

工具表看起来像这样

  id   name               tools_names                                   quantity    type

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

tool_use表看起来像这样

 id     user_id   type        tools                 
 8      siraj    engineer   cutting playerA,cutting playerB     
 7      siraj    team       REFLECTORSB         
 6      siraj    team       REFLECTORSA     

我想在插入时显示除插入到tool_use表之外的tools_names但是整个tools_names正在显示eventHough结果看起来像在table.here是我的控件

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>

请帮我解决我的问题

2 个答案:

答案 0 :(得分:1)

当您array_search时,您正在尝试搜索据称包含$row->tools的{​​{1}}。并且您在一个不包含相同类型的逗号分隔值列表的数组中搜索,但是包含它们的爆炸版本(就像您在第3行上执行了cutting playerA,cutting playerB一样)。

答案 1 :(得分:0)

如果表tools中的tool_use包含单个值,我会找到解决方案,但如果它包含更多值,我的意思是存储为数组,让我远离目的地请看看

public function ajax_tools()
{
    $data['tools']=$this->Tools_model->view_available_tools($_POST['type']);
    foreach ($data['tools'] as $key=>$val)
     {
    $data['toolss'][] = $data['t']=explode(',',$val['tools_names']);
     }
    $data['tools_names'] = $this->Tools_model->get_tool_names($_POST['type'])->result();
    var_dump($data['tools_names']);
    foreach ($data['tools_names'] as $val) 
    {
        if (($key =array_search($val->tools,$data['t'])) !== false) 
        {
            unset($data['t'][$key]);
            $data['t'] = array_values($data['t']);

        }
    }

    $this->load->view('ajax_tools',$data);
}