Codeigniter - selecting children and parents from db

Posted by Tomek Buszewski on Stack Overflow See other posts from Stack Overflow or by Tomek Buszewski
Published on 2012-09-20T09:27:45Z Indexed on 2012/09/20 9:37 UTC
Read the original article Hit count: 152

Filed under:
|

I want to pull from my database records corresponding to parent_id, like this:

function getChildren($id, $parent_id) {
    $q = $this->db->select('id, name, slug, plat');
    $q = $this->db->from('games');
    $q = $this->db->where('parent_id',$id);
    $q = $this->db->or_where('id',$parent_id);
    $q = $this->db->get();

    return $q->result_array();
}

It - if it's a children game - get parent_id and search for a game with such id and for other games that has parent_id same as this one. If it's the parent game, it only looks for games with parent_id same as it's id.

The problem is... it's not always working. I have four games in db:

id  |  parent_id  |  title
15  |  0          |  Abe
19  |  15         |  Abe
20  |  0          |  RE2
21  |  20         |  RE2 DS

First two works, last two - only children (id = 21) shows parent.

© Stack Overflow or respective owner

Related posts about php

Related posts about codeigniter