【问题标题】:Required Recursive function for parent child relation tree?父子关系树需要递归函数?
【发布时间】:2015-10-16 07:17:39
【问题描述】:

场景:

我的课程有班次(早上、晚上等)[课程是班次的父级]。
每个班次都有部分(绿色,蓝色,B ect)[班次是部分的父级]。

图表:

我通过连接获得的当前记录:

    $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);

查询结果:

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
    )

)

想要这样的结果:

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
                            )
    )
)

请大家帮忙。

【问题讨论】:

  • 你的问题到底是什么?
  • 根据父子关系重新排列结果。我知道 sql 不支持分层数据。
  • 那么,您尝试过什么?什么不起作用?
  • 使用了哪些表/列?你如何获得第一个数组?在结果之前显示代码。

标签: php mysql join foreach codeigniter-3


【解决方案1】:

我无法从您发布的信息中推断出正确设置您的 shift 键所需的信息,但是您所要做的就是修改 switch 语句以正确设置此键:

$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'];
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 2014-10-24
    • 2020-10-11
    相关资源
    最近更新 更多