3

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

user_777
  • 845
  • 1
  • 9
  • 25

2 Answers2

1

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).

Stock Overflaw
  • 3,203
  • 1
  • 13
  • 14
  • k.then what about other two values `REFLECTORSB` and `REFLECTORSA` it contains single value so it should be displayed right? – user_777 Dec 14 '16 at 04:34
  • Actually no: considering only type `team`, `$data['toolss']` would be `[0 => ['REFA', 'REFB']]` whereas `$row->tools` would be `'REFB'` on first loop iteration (then `'REFA'` on second iteration). So your code does `array_search('REFB', [0 => ['REFA', 'REFB']])`, which cannot return any index. – Stock Overflaw Dec 14 '16 at 08:39
  • now its returning..see my answer but only for single values inserted rows not for stored as an array – user_777 Dec 14 '16 at 08:54
0

if tools in table tool_use contain single values i find it out the solution but if it contains more values i mean stored as an array keeping me away from the destination please have a look

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);
}
user_777
  • 845
  • 1
  • 9
  • 25