分层树视图类别并排写入
Hierarchical Treeview Categories Write Side by Side
我有分层类别。我想在 table 中打印它们。我希望它在 table.
的每一行中写入子类别和主类别
示例:
电子
电子产品 > 计算机
电子产品 > 电脑 > 配件
电子产品 > 电脑 > 配件 -> 鼠标
电子产品 > 电脑 > 配件 -> 键盘
化妆品
化妆品 > 护肤
化妆品 > 护肤 > 护肤霜
化妆品 > 护肤 > 面膜
我试过这个功能。它没有用。
public function getSubCats($Categories)
{
foreach($Categories as $category){
if(count($category->subcategory)){
$this->getSubCats($category->subcategory);
return $category->subcategory;
}else{
return $category->subcategory;
}
}
}
类别数据库table
id
parent_id
name
1
NULL
Electronics
2
1
Computers
3
2
Accessories
4
3
Keyboards
5
NULL
Cosmetics
6
5
Skin Care
7
6
Skin Cream
8
7
Skin Mask
索引blade类别table(子类别无限制)
Category Name
Action
Electronics
Edit
Electronics - Computers
Edit
Electronics - Computers - Accessories
Edit
Electronics - Computers - Accessories - Keyboards
Edit
Cosmetics
Edit
Cosmetics - Skin Care
Edit
Cosmetics - Skin Care - Skin Cream
Edit
Cosmetics - Skin Care - Skin Mask
Edit
你能帮帮我吗?
谢谢。
我使用 0(零)而不是 NULL 来确定顶级类别,因为从 MySQL 获取结果的方式不同。
where column = 1/2/3/4
对
where column is NULL
当使用 0(零)时,我可以使用相同的准备语句。
这是我想出的:
function get_cat_by_parent( $pdo, $id ) {
$sql = "SELECT * FROM Whosebug.categories WHERE parent_id = ?";
$stmt = $pdo->prepare( $sql );
try {
$stmt->execute([$id]);
$data = $stmt->fetchALL( PDO::FETCH_ASSOC );
} catch (PDOException $e ) {
// exception
}
return $data;
}
function recursive( $pdo, $array, $depth, $label ) {
$depth++;
$data = get_cat_by_parent( $pdo, $array['id'] );
if(count($data)) {
foreach( $data as $row) {
echo $label;
echo " → ";
echo $row['name'];
echo "<br />";
recursive( $pdo, $row, $depth, $label . " → " . $row['name'] );
}
}
}
$parents = get_cat_by_parent( $pdo, 0 );
foreach( $parents as $row ) {
echo $row['name'];
echo "<br />";
recursive( $pdo, $row, 0, $row['name'] );
}
输出:
Electronics
Electronics → Computers
Electronics → Accessories
Electronics → Accessories → USB Hub
Electronics → Accessories → Memory Card Readers
Electronics → Accessories → Mic Stand
Electronics → Keyboards
Cosmetics
Cosmetics → Skin Care
Cosmetics → Skin Cream
Cosmetics → Skin Cream → Oil of Olaz
Cosmetics → Skin Cream → Something
Cosmetics → Skin Cream → Other
Cosmetics → Skin Mask
我在你的测试数据中添加了几个类别。
我有分层类别。我想在 table 中打印它们。我希望它在 table.
的每一行中写入子类别和主类别
示例:
电子
电子产品 > 计算机
电子产品 > 电脑 > 配件
电子产品 > 电脑 > 配件 -> 鼠标
电子产品 > 电脑 > 配件 -> 键盘
化妆品
化妆品 > 护肤
化妆品 > 护肤 > 护肤霜
化妆品 > 护肤 > 面膜
我试过这个功能。它没有用。
public function getSubCats($Categories)
{
foreach($Categories as $category){
if(count($category->subcategory)){
$this->getSubCats($category->subcategory);
return $category->subcategory;
}else{
return $category->subcategory;
}
}
}
类别数据库table
id | parent_id | name |
---|---|---|
1 | NULL | Electronics |
2 | 1 | Computers |
3 | 2 | Accessories |
4 | 3 | Keyboards |
5 | NULL | Cosmetics |
6 | 5 | Skin Care |
7 | 6 | Skin Cream |
8 | 7 | Skin Mask |
索引blade类别table(子类别无限制)
Category Name | Action |
---|---|
Electronics | Edit |
Electronics - Computers | Edit |
Electronics - Computers - Accessories | Edit |
Electronics - Computers - Accessories - Keyboards | Edit |
Cosmetics | Edit |
Cosmetics - Skin Care | Edit |
Cosmetics - Skin Care - Skin Cream | Edit |
Cosmetics - Skin Care - Skin Mask | Edit |
你能帮帮我吗? 谢谢。
我使用 0(零)而不是 NULL 来确定顶级类别,因为从 MySQL 获取结果的方式不同。
where column = 1/2/3/4
对
where column is NULL
当使用 0(零)时,我可以使用相同的准备语句。
这是我想出的:
function get_cat_by_parent( $pdo, $id ) {
$sql = "SELECT * FROM Whosebug.categories WHERE parent_id = ?";
$stmt = $pdo->prepare( $sql );
try {
$stmt->execute([$id]);
$data = $stmt->fetchALL( PDO::FETCH_ASSOC );
} catch (PDOException $e ) {
// exception
}
return $data;
}
function recursive( $pdo, $array, $depth, $label ) {
$depth++;
$data = get_cat_by_parent( $pdo, $array['id'] );
if(count($data)) {
foreach( $data as $row) {
echo $label;
echo " → ";
echo $row['name'];
echo "<br />";
recursive( $pdo, $row, $depth, $label . " → " . $row['name'] );
}
}
}
$parents = get_cat_by_parent( $pdo, 0 );
foreach( $parents as $row ) {
echo $row['name'];
echo "<br />";
recursive( $pdo, $row, 0, $row['name'] );
}
输出:
Electronics
Electronics → Computers
Electronics → Accessories
Electronics → Accessories → USB Hub
Electronics → Accessories → Memory Card Readers
Electronics → Accessories → Mic Stand
Electronics → Keyboards
Cosmetics
Cosmetics → Skin Care
Cosmetics → Skin Cream
Cosmetics → Skin Cream → Oil of Olaz
Cosmetics → Skin Cream → Something
Cosmetics → Skin Cream → Other
Cosmetics → Skin Mask
我在你的测试数据中添加了几个类别。