简体   繁体   中英

Retrieving data from adjacency list model database.(MySQL)

function get_hierarchy_page()
{
    $detail = $this->input->post('name');
    $tree = $this->dashboard_model->get_detail($detail);
    $this->load->view('layout/layout', $data);

    if(isset($tree))
    {
        $data['hierarchy'] = $this->data_array_for_teacher($tree);
    }
}

function data_array_for_teacher($tree)
{
    if(!(isset($tree)))
        return 0;

    $data_query = mysql_query("select * from tbl_level");

    while($result = mysql_fetch_assoc($data_query))
    {
        $id = $result['level_id'];
        $data_array[$id] = $result;
    }

    //array to get level id
    while($level_result = mysql_fetch_assoc($tree))
    {
        $level_array[] = $level_result['level_id'];
    }

    if(!(isset($level_array)))
        return 0;

    foreach($level_array as $level)
    {
        $p   = $this->recursive_for_teacher($data_array,$level);    
        print_r($p);
    }
}

function recursive_for_teacher($data_array,$level_once)
{
    if(!isset($data_array,$level_once))
        return 0;

    $i = 0;
    static $p = array();
    $data = $data_array;
    $y = $level_once;
    $x = $data[$y]['level_parent'];
    $z = $data[$y]['level_name'];
    $y=$x;
    array_push($p,$z);
    $p[] = $z;

    if($y!=0)
        $this->recursive_for_teacher($data,$y);

    return $p;
}

I am working on a system which is based on hierarchical data stored in mysql using adjacency list model. I have 2 tables tbl_user and tbl_level .
tbl_level has the following fields - level_id , level_parent , level_name . These three fields are the same as id , parent_name , parent_id respectively in a standard database structure.

In the table tbl_level there are many organizations and each one has different hierarchical structures.
I wrote the above code which solves my problem of getting the the whole hierarchy of any authority in a given organization.
The only problem which i am now facing is when i am getting the required answer in one array.
I need one array for each authority.
Array(
[0] => Engineering and Technology
[1] => Univ1
[2] => Class
[3] => Master of Business Administration
[4] => Engineering and Technology
[5] => univ1
[6] => Class_cse
[7] => Computer Science
[8] => Bachelor of Technology
[9] => Engineering and Technology
[10] => univ1
)

Can anyone suggest a better solution for the problem ?

function parent_sort($categories)
{    
    foreach($categories as $item)
        $childs[$item->parent_id][] = $item;
    foreach($categories as $item) 
        if (isset($childs[$item->category_id]))
            $item->childs = $childs[$item->category_id];

    return $childs[0];
}

You can use this function to sort leveled array, and it will output sorted array by levels

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