1

I've gathered data from multiple MySql tables and stored them as associative arrays using a foreach loop with the query.

I would like to use those associative arrays and the implode method in the mysql query to gather more data from a separate table.

I know that with the implode method, when dealing with Indexed arrays, you can just insert the array directly in the "implode section". But with associative arrays, I am unsure how to call all the available arrays and insert them in the query.

Please refer to the attached image for a detailed illustration explaining it further.

Below is also a portion of my code

public function user_implode()
{

        $s_id = array(
          "id" => 383
        );

        $count = 0; 
            foreach ($query->result() as $row)
            {   
                $count = $count + 1;
                $loop_number[$count] =  $row->id; 

            }

        $this->db->from('occupation');
        $this->db->where_in('id',implode("','",$loop_number[$count]));
        $query = $this->db->get();



        foreach ($query->result() as $row)
        {

                echo $row->id;

        }

        echo 'Total Results: ' . $query->num_rows();

}

THANKS ALOT enter image description here

1 Answer 1

1

The second parameter to where_in() should be an array.

You are generating a string with implode() and only of the last value of the array instead of the whole array.

So all you need is:

$this->db->where_in('id', $loop_number);

And I don't see where $query comes from, it seems to be undefined when you use it in the first loop in your method.

Apart from that you should initialize your variable, $loop_number = []; before the loop.

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

1 Comment

Thanks very much my friend. This is very well explained. And thanks for the additional tip for initiating variables :)

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.