在Codeigniter/PHP中显示类别
我正在尝试显示存储在数据库中的类别:在Codeigniter/PHP中显示类别,php,mysql,codeigniter,Php,Mysql,Codeigniter,我正在尝试显示存储在数据库中的类别: table niveau_1 id_1 - primary key title - string table niveau_2 id_2 - primary key id_1 - connect to niveau_1 (which niveau_1 is its 'parent') title - string table niveau_3 id_3 - primary key id_2 - conn
table niveau_1
id_1 - primary key
title - string
table niveau_2
id_2 - primary key
id_1 - connect to niveau_1 (which niveau_1 is its 'parent')
title - string
table niveau_3
id_3 - primary key
id_2 - connect to niveau_2 (which niveau_2 is its parent)
title - string
我的想法是从模型中的数据库中获取所有数据,并在函数中重建阵列:
function get_all_categories() {
$query = $this->db->query('SELECT * FROM categories');
//code to reconstruct my array like:
//foreach($query->result_array() as $row) {
//$categories[$row['niveau_1']] = array[]
//}
return $categories;
}
并输出一些数组,如:[0] => Array
(
[niveau_1] => main category name
[niveau_2] => Array(
...children here and children of these childs
)
)
有人知道怎么做吗?我被困在这里,不知道如何创建阵列。。。我知道如何在我的视图中显示它,但让它们进入阵列对我来说似乎很难
提前谢谢 这是一个想法:
// your model using activerecord
public function getCategories(){
$q = $this->db->get('categories');
if($q->num_rows > 0){
return $q->result();
}else{
return false;
}
}
//your controller
$this->load->model('category_model');
$data['results'] = $this->category_model->getCategories();
foreach($data['results'] as $c){
//iterate through categories, match them to points.
//match category_id in results with category_id in the other table results
//setup a multi dimensional array to build up what you need.
}
$c
)$c
)这没有经过测试,但您可以将其作为一个想法使用我认为您的数据库是错误的。据我所知,这是一个“目录”或类似的(类别、子类别、子类别等)。“1.Visie,Beleid…”多次出现 如果要更改数据库,请执行以下操作:
table niveau_1
id_1 - primary key
title - string
table niveau_2
id_2 - primary key
id_1 - connect to niveau_1 (which niveau_1 is its 'parent')
title - string
table niveau_3
id_3 - primary key
id_2 - connect to niveau_2 (which niveau_2 is its parent)
title - string
在这种情况下,您的数据库中不会出现重复的元素,我认为您所需的查询会更简单:
SELECT
niveau_1.title AS title_1,
GROUP_CONCAT(niveau_2.title,"@") AS title_2,
GROUP_CONCAT(niveau_3.title,"@") AS title_3
FROM niveau_1
LEFT JOIN niveau_2 ON niveau_2.id_1=niveau_1.id_1
LEFT JOIN niveau_3 ON niveau_3.id_2=niveau_2.id_2
GROUP BY niveau_2.id_2
这个查询只是半个解决方案,对您来说并不完美,但您可以从这里开始。我认为您的数据库是错误的。据我所知,这是一个“目录”或类似的(类别、子类别、子类别等)。“1.Visie,Beleid…”多次出现 如果要更改数据库,请执行以下操作:
table niveau_1
id_1 - primary key
title - string
table niveau_2
id_2 - primary key
id_1 - connect to niveau_1 (which niveau_1 is its 'parent')
title - string
table niveau_3
id_3 - primary key
id_2 - connect to niveau_2 (which niveau_2 is its parent)
title - string
在这种情况下,您的数据库中不会出现重复的元素,我认为您所需的查询会更简单:
SELECT
niveau_1.title AS title_1,
GROUP_CONCAT(niveau_2.title,"@") AS title_2,
GROUP_CONCAT(niveau_3.title,"@") AS title_3
FROM niveau_1
LEFT JOIN niveau_2 ON niveau_2.id_1=niveau_1.id_1
LEFT JOIN niveau_3 ON niveau_3.id_2=niveau_2.id_2
GROUP BY niveau_2.id_2
此查询只是半个解决方案,它并不适合您,但您可以从这里开始。第一,不要使用未准备好的SQL语句。。使用活动记录查询#2您需要运行两个查询,并使用foreach循环进行遍历和匹配。第一,不要使用未准备好的SQL语句。。使用活动记录查询#2您将需要运行两个查询,并使用foreach循环来遍历并匹配它们。