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

5
  • Shouldn't it be "return $data['tools']" and not "return $data['toolss']"? Commented Dec 13, 2016 at 9:59
  • Dont use extra letter to indicate some other variable (tools vs. toolss) -> always use self explaining names (loadedTools vs. toolNames) - CleanCode Commented Dec 13, 2016 at 10:01
  • is it becomes a problem Commented Dec 13, 2016 at 10:08
  • stackoverflow.com/questions/3653462/… Commented Dec 13, 2016 at 10:09
  • anybody have any idea.. Commented Dec 13, 2016 at 10:22

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

k.then what about other two values REFLECTORSB and REFLECTORSA it contains single value so it should be displayed right?
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.
now its returning..see my answer but only for single values inserted rows not for stored as an array
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);
}

1 Comment

anybody have any idea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.