简体   繁体   中英

Required Recursive function for parent child relation tree?

Scenario:

i have course that has shifts(morning ,evening ect.) [ course is parent of shift ].
and for every shift there are sections (A Green,A blue ,B ect)[ shift is parent of sections ].

graph:

在此处输入图片说明

Current record i get by joins :

    $this->db->select("sections.section_id,c.course_id,s.id as shift_id");
    $this->db->from($this->_table);
    $this->db->join('shift s', 's.id = sections.shift_id', 'left');
    $this->db->join('courses c', 's.course_id = c.course_id', 'left');
    $this->db->join('employees e', 'e.employee_id = sections.head_id', 'left');
    $this->db->distinct();
    $this->db->order_by('section_id', 'desc');
    $this->db->where('c.course_id',$course_id);

query result:

Array
(
    [0] => Array
        (
            [course_id] => 3
            [section_id] => 5
            [shift_id] => 7
        )

[1] => Array
    (
        [course_id] => 2
        [section_id] => 4
        [shift_id] => 5
    )

[2] => Array
    (
        [course_id] => 2
        [section_id] => 3
        [shift_id] => 6
    )

[3] => Array
    (
        [course_id] => 1
        [section_id] => 2
        [shift_id] => 4
    )

[4] => Array
    (
        [course_id] => 1
        [section_id] => 1
        [shift_id] => 4
    )

)

Want result like this:

Array
(
    [course_id 3] => Array
        (
            [shift id] => array
                                (
                                 [section_id] => 5
                                )
        )
[course_id 2] => Array
    (
        [shift_morning] =>array
                            (
                             [section_id] => 4
                            )
        [shift_noon] =>array
                            (
                             [section_id] => 3
                            )
    )
 [course_id 1] => Array
    (
        [shift_morning] =>array
                            (
                             [section_id] => 2
                            )
        [shift_noon] =>array
                            (
                             [section_id] => 1
                            )
    )
)

any can help please.

I was unable to extrapolate the required information to properly set your shift key from your posted information however all you have to do is modify the switch statement to properly set this key:

$this->db->select("sections.section_id,c.course_id,s.id as shift_id");
$this->db->from($this->_table);
$this->db->join('shift s', 's.id = sections.shift_id', 'left');
$this->db->join('courses c', 's.course_id = c.course_id', 'left');
$this->db->join('employees e', 'e.employee_id = sections.head_id', 'left');
$this->db->distinct();
$this->db->order_by('section_id', 'desc');
$this->db->where('c.course_id',$course_id);
$query = $this->db->get();

$results = array();
foreach ($query->result_array() as $row) {
    switch ($row['shift_id']) {
        case 5:
            $shift = 'morning';
        break;

        case 6:
            $shift = 'noon';
        break;
    }

    $results['course_id '.$row['course_id']]['shift_'.$shift]['section_id'] = $row['section_id'];
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM