Tuesday, 28 August 2018

PHP - The array_search function does not return the values

Here am having two tables namely tools and tool_use.

tools table looks like this
  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 table looks like this
 id     user_id   type        tools
 8      siraj    engineer   cutting playerA,cutting playerB
 7      siraj    team       REFLECTORSB
 6      siraj    team       REFLECTORSA

i want to display the tools_names except inserted to tool_use table while inserting but the entire tools_names are displaying eventhough the result looks like in the table.here is my control
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);
}

Here is my models
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));
}

this is my view
<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>

please help me to solve my issue

When you array_search, you're trying to search for $row->tools which supposedly contains cutting playerA,cutting playerB. And you search for that inside an array that does not contain the same kind of comma-separated lists of values but instead contains their exploded versions (as you did an explode on line 3).

0 comments:

Post a Comment