PHP CodeIgniter problem with JOIN statement
I have two tables: events_archive and reports. The events table shows all information about an event including a ref_id
(which is a unique ID for this event), a demo_id
(the user ID of the demo who participated in the event) and a client_id
(the user ID of the client who participated in the event). In the reports table, there is an event_ref
column which corresponds the event's ref_id
.
I'm trying to get CodeIgniter to show all reports for a particular user on a page. I only want the reports to show that correspond to events the user has participated in (so, events that have a d开发者_如何学JAVAemo_id
or client_id
value equal to the value in the user's session ID).
Here's the code I'm using in the model:
function get_reports($user_id,$type,$limit,$offset){
$this->db->select('*');
$this->db->from('reports');
$this->db->join('events_archive','events_archive.ref_id = reports.event_ref AND events_archive.'.$type.'_id = '.$user_id);
$this->db->limit($limit,$offset);
$reports = $this->db->get();
if($reports->num_rows()>0){
foreach($reports->result() as $report){
$this->table->add_row( $report->id );
}
}
$this->table->set_heading('ID');
return $this->table->generate();
}
But it just lists 2 over and over again (I guess 2 is the report ID it found). It should list the report ID's incrementally. Oddly enough it's listing 16 times (and there are 16 reports that match the criteria for being shown). There's either something wrong with the JOIN or the loop. I think it's the JOIN. Does anybody know where I'm going wrong?
I solved this by changing $this->db->select('*')
to $this->db->select('reports.*')
精彩评论