Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Problem with Where In?
#1

[eluser]daparky[/eluser]
I've ran into a problem while developing an application. Basically what i'm trying to do is get a list of results, explode the id's into comma seperated and perform a where_in delete sql.

My code:

This piece of code is to delete a box as well as all the products within that box.

Code:
$this->db->select('stock_id');    
        $this->db->where('stock_box', $id);
        $products = $this->db->get('stock')->result();
        $num = count($products);
        
        if($num > 0)
        {
            for($i=0; $i<$num; $i++)
            {
                $product[$i] = $products[$i]->stock_id;
            }
            
            $ids = implode(",", $product);
            
            $this->db->where_in('stock_id', $ids);
            $this->db->delete('stock');

        }

        $this->db->where('box_id', $id);
        $this->db->delete('box');

When i print_r the $ids it shows them as 1,2,3 etc which is fine but when i print the last query out it shows the result as:-

Code:
WHERE stock_id IN ('1,2,3')

Problem: It needs to have a quotation mark around all of the id numbers instead of around them as a whole. It should be doing this:-

Code:
WHERE stock_id IN ('1','2','3')

This works if i do it in a normal query but i want to use active record. Any help is appreciated.

p.s. this is what i did to solve the issue:-

Code:
$this->db->select('stock_id');    
        $this->db->where('stock_box', $id);
        $products = $this->db->get('stock')->result();
        $num = count($products);
        
        if($num > 0)
        {
            for($i=0; $i<$num; $i++)
            {
                $this->db->where('stock_id', $products[$i]->stock_id);
                $this->db->delete('stock');
            }
        }

        $this->db->where('box_id', $id);
        $this->db->delete('box');
#2

[eluser]danmontgomery[/eluser]
Code:
$ids = implode(",", $product);
$this->db->where_in('stock_id', $ids);

Because you're imploding $ids before passing it to where_in, so you're passing it the string "1,2,3". Just remove the call to implode().
#3

[eluser]daparky[/eluser]
[quote author="noctrum" date="1265681478"]
Code:
$ids = implode(",", $product);
$this->db->where_in('stock_id', $ids);

Because you're imploding $ids before passing it to where_in, so you're passing it the string "1,2,3". Just remove the call to implode().[/quote]

Thank you, i've sorted it. I just used $product for the where_in.




Theme © iAndrew 2016 - Forum software by © MyBB