Muhammad Usman
Muhammad Usman

Reputation: 1423

Get All Indexes Of array in codeigniter

Here Is MY Output OF problem i want to get all reciver_id indexes from this array and after getting all indexes i want to get the result from another table users against these id's

$data['cat_row']        =       $this->messages_model->get_sentnotify($user_id);
        foreach($data['cat_row'] as $key){
            $profile    =   $key['reciver_id'];
        }
 $data['profile']        =       $this->messages_model->Sender_profile($profile);

through this code i get only 1 value of first index how to get all... getting all i want to get users from other table against each id

function Sender_profile($profile)
    {
        $this->db->where('id', $profile);
        $query = $this->db->get($this->db_table2);
        return $query->result_array();
    }

kindly help me about sql also how i get all values from database in one iteration enter image description here

Upvotes: 1

Views: 4678

Answers (2)

Jaydeep Baldaniya
Jaydeep Baldaniya

Reputation: 123

In your controller , you need to access all your receiver_id then modify your foreach loop:

$i=0;
foreach($data['cat_row'] as $key){
        $profile[$i]    =   $key['reciver_id'];
        $i++;
    }

Now you have all your receiver_id in $profile array. and pass this array in controller function and should query like this:

SELECT * FROM table WHERE id IN (5,4,3,1,6);// Just for example you need to modify your query according to your need.

Upvotes: 0

Ahsan
Ahsan

Reputation: 1084

Code Part 1

Get all reciver_id in an array like follows:

$data['cat_row'] = $this->messages_model->get_sentnotify($user_id);

$profile = [];

foreach($data['cat_row'] as $key){

  array_push($profile, $key['reciver_id']);

}

$data['profile'] = $this->messages_model->Sender_profile($profile);

Code Part 2

Use Codeigniter's where_in() function.

function Sender_profile($profile) {

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

  $query = $this->db->get($this->db_table2);

  return $query->result_array();

}

Upvotes: 3

Related Questions