我的表结构是这样的:
See image of table for categories
我的模态代码:
public function getData($table)
{
return $this->db->select('*')->get($table)->result_array();
}
我得到一个这样的数组:
Array
(
[0] => Array
(
[id] => 1
[name] => Mobile Phones
[parent] => 0
[date] => 2017-05-16 08:55:22
[slug] => mobile-phones
)
[1] => Array
(
[id] => 2
[name] => Samsung
[parent] => 1
[date] => 2017-05-16 08:55:29
[slug] => samsung
)
[2] => Array
(
[id] => 3
[name] => Apple
[parent] => 1
[date] => 2017-05-16 18:07:56
[slug] => apple
)
[3] => Array
(
[id] => 4
[name] => Huawei
[parent] => 1
[date] => 2017-05-16 18:08:10
[slug] => huawei
)
[4] => Array
(
[id] => 5
[name] => HTC
[parent] => 1
[date] => 2017-05-16 18:08:22
[slug] => htc
)
[5] => Array
(
[id] => 6
[name] => OnePlus
[parent] => 1
[date] => 2017-05-16 18:08:33
[slug] => oneplus
)
[6] => Array
(
[id] => 7
[name] => LG
[parent] => 1
[date] => 2017-05-16 18:08:44
[slug] => lg
)
[7] => Array
(
[id] => 8
[name] => BlackBerry
[parent] => 1
[date] => 2017-05-16 18:08:55
[slug] => blackberry
)
[8] => Array
(
[id] => 9
[name] => Motorola
[parent] => 1
[date] => 2017-05-16 18:09:14
[slug] => motorola
)
[9] => Array
(
[id] => 10
[name] => Google
[parent] => 1
[date] => 2017-05-16 18:09:52
[slug] => google
)
)
但我希望得到如下数组:
Array
(
[0] => Array
(
[id] => 1
[name] => Mobile Phones
[parent] => 0
[date] => 2017-05-16 08:55:22
[slug] => mobile-phones
[root_category] => null
)
[1] => Array
(
[id] => 2
[name] => Samsung
[parent] => 1
[date] => 2017-05-16 08:55:29
[slug] => samsung
[root_category] => Mobile Phones
)
[2] => Array
(
[id] => 3
[name] => android
[parent] => 2
[date] => 2017-05-16 18:07:56
[slug] => android
[root_category] => Mobile Phones > Samsung
)
[3] => Array
(
[id] => 4
[name] => Huawei
[parent] => 3
[date] => 2017-05-16 18:08:10
[slug] => huawei
[root_category] => Mobile Phones > Samsung > android
)
[4] => Array
(
[id] => 5
[name] => HTC
[parent] => 1
[date] => 2017-05-16 18:08:22
[slug] => htc
[root_category] => Mobile Phones
)
[5] => Array
(
[id] => 6
[name] => OnePlus
[parent] => 1
[date] => 2017-05-16 18:08:33
[slug] => oneplus
[root_category] => Mobile Phones
)
[6] => Array
(
[id] => 7
[name] => LG
[parent] => 1
[date] => 2017-05-16 18:08:44
[slug] => lg
[root_category] => Mobile Phones
)
[7] => Array
(
[id] => 8
[name] => Men Fashion
[parent] => 0
[date] => 2017-05-16 18:08:55
[slug] => men-fashion
[root_category] => null
)
[8] => Array
(
[id] => 9
[name] => shirt
[parent] => 8
[date] => 2017-05-16 18:09:14
[slug] => shirt
[root_category] => Men fashion
)
[9] => Array
(
[id] => 10
[name] => T shirt
[parent] => 9
[date] => 2017-05-16 18:09:52
[slug] => google
[root_category] => Men Fashion > shirt
)
}
顺便说一下,我正在使用codeigniter。
答案 0 :(得分:0)
从数据库逻辑制作层次结构不是一个好习惯。
数据库表结构已足以存储父目录。
你需要有一个函数来递归调用它。
如果您的目标是创建面包屑,我假设您需要从最低层级爬升,直到parent
值为=。
function display_sub($id=0)
{
$breadcrumb = [];
// I assume your these are your model and function name.
$category = $this->item_model->get_category($id);
if($category['parent'] != 0)
{
// if parent not 0, add the name to $breadcrumb array,
// then do another run of this function
// but take this menu's parent id as parameter
array_push($breadcrumb,$row['name']);
$this->display_sub($row['parent']);
} else {
// if parent already 0, it's finish the job.
// and you can reverse it and display as intended.
return $breadcrumb;
}
}