Welcome Guest, Not a member yet? Register   Sign In
Passing array in a where clause?
#1

[eluser]datguru[/eluser]
Hello everyone first post here, been using code igniter for about 3 weeks now and im loving it, I wonder if any of you could help me here.

What I'm trying to accomplish is pulling data from one table collecting the ID fields and then passing all those id fields into a secon query to gather information from another database heres a little example.
Code:
foreach($this->db->result() as $row)
{
  $inst_id = $row->institutionid //Need this to be put into an array
}

That gathers the ID I need now im stuck on the next bit what i want to do is access the project table and get the project name based on the ID.

The only way I can think of doing this is doing a query for each element in the array and putting that into another array of names. I need any names with any of those values in the ID field.



Any help would be greatly appreciated.

Thanks guys.
#2

[eluser]xwero[/eluser]
You means something like this
Code:
$inst_ids = array();
foreach($this->db->result() as $row)
{
  $inst_ids[] = $row->institutionid //Need this to be put into an array
}
#3

[eluser]datguru[/eluser]
Yep that bit was how I kinda new how to do thanks for clearing that up though, but the most confusing part is how do I then take that array of ids and so a select based on those IDs.

So I want to select all data where id = instids. Can you just pass an array and it will find every field which matches up to one of those ids? Or is there a special way to perform the kind of functionality I need?
#4

[eluser]xwero[/eluser]
in AR there is the method where_in()
Code:
$inst_ids = array();
foreach($this->db->result() as $row)
{
  $inst_ids[] = $row->institutionid //Need this to be put into an array
}
$query2 = $this->db->where_in('id',$inst_ids)->get('table');
$query2 fetches all rows from the table where the id field is the same a one of the id from $inst_ids. The sql statement in mysql is
Quote:select * from table where id in (1,2,3)
But i recommend you use a check to see the array is not empty otherwise you will get an error.
#5

[eluser]datguru[/eluser]
Thats great just going to try it now Smile. Just want to say that you've been a great help so far! So thanks Big Grin.
#6

[eluser]xwero[/eluser]
That is what that forum is for Wink
#7

[eluser]datguru[/eluser]
Worked perfectly Smile Genious Smile.
#8

[eluser]datguru[/eluser]
Sorry for double post wasnt sure if it actually made the topic re appear in bold.

Im trying to now output the step that the institution is on now there are lots of steps but I only want to select the highest step and I only want one step per institution (the highest step). The problem i'm having with my current query is that it selects all top querys for each institution (so gives multiple steps for institutions which I dont want.)

Heres the query.
Code:
$data['instname'] = $this->db->where_in('institution_id',
$inst_id)->where('completed',1)->order_by('steps_id','desc')->limit(sizeof($inst_id))->join('institutions','institutions.id
= progress.institution_id')->get('progress');
Thanks again.




Theme © iAndrew 2016 - Forum software by © MyBB