1

I have the following CI query builder query

$query_teams = $this->db->get_where('teams_in_cups', array('cup_id' => 2));
$team_ids = $query_teams->result_array();

I want $team_ids (which currently outputs two ids -> 22 and 25) to be compatible with the following query:

$this->db->where_in('id', $team_ids);    
$query_team_details = $this->db->get('teams');

In the second query, $team_ids should look like the following array(22,25);

I tried foreach(), implode(), explode(), but couldn't manage to get it to work.

0

1 Answer 1

1

You can use MySQL join to get the desired result.

I am fixing your current situation only.

$query_teams = $this->db->get_where('teams_in_cups', array('cup_id' => 2));
$team_data=$query_teams->result();
team_ids='';
foreach($team_data as $td)
{
   $team_ids=$team_id.','.$td->team_id;
}
$team_ids=ltrim($team_ids,',');
$this->db->where_in('id', $team_ids);    
$query_team_details = $this->db->get('teams');
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for fast answer,but '$td->team_id' returns undefined variable error. In MySQL the column name is team_id so I don't know why could it be?
use your own database table column name in place of team_id. I have just given one example.
inside the foreach, what does the variable $team_id do? Because it is not defined anywhere.
This is an imaginary item, I added, as you have not given the table structure. update the code as per the table structure or update the question with table structure, I will update the solution.
There is no reason to perform 2 trips to the database. where_in() expects an array of values as the second parameter. This answer should not be used.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.