Dharmendra vadher
Dharmendra vadher

Reputation: 59

CodeIgniter: get data after each 'foreach()' command

$query = $this->db->get("courses");
$data['courses'] = $query->result();
foreach($courses as $c){
    $this->db->select('*');
    $this->db->from('subjects');
    $this->db->where('id',$c->course);
    $query = $this->db->get("subjects");
    $data['subjects'] = $query->result();

    foreach($subjects as $s){
        $this->db->select('*');
        $this->db->from('tests');
        $this->db->where('id',$t->test);
        $query = $this->db->get("tests");
        $data['tests'] = $query->result();
    }
} 

I want to print tables of Courses having top first row with single column with data $c->course and bellow rows with two column having data $s->subject and $t->test respectively...

Upvotes: 0

Views: 53

Answers (1)

Rajat Gupta
Rajat Gupta

Reputation: 340

you can use like this for first row for courses and next two rows for subjects and tests

$query = $this->db->get("courses");
$data['courses'] = $query->result();
foreach($courses as $key=>$c){
    $this->db->select('*');
    $this->db->from('subjects');
    $this->db->where('id',$c->course);
    $query = $this->db->get("subjects");
    $data['subjects'][$key] = $query->result();

    foreach($subjects as $key=>$s){
        $this->db->select('*');
        $this->db->from('tests');
        $this->db->where('id',$t->test);
        $query = $this->db->get("tests");
        $data['tests'][$key] = $query->result();
    }
} 

Upvotes: 1

Related Questions