首先,我对此发布了另一个问题,但是没有得到答案,所以我需要发布一个新的问题。
我有一个按层次列出类别的函数,我在foreach
循环中创建了另一个函数(在同一函数上),并且也将其用于下拉列表,因此不需要使用现有的多个函数来创建另一个查询。次。
我想使用相同的查询来列出所选类别下的子类别。
示例:如果单击category.php?id=100
,那么我要在登录页面中列出类别100下的子类别。
这是我的职能:
$allCategories = array();
$categoryMulti = array(
'categories' => array(),
'parent_cats' => array()
);
while ($row = $stmt->fetch()) {
//I created another array to get subcats didnt work
//$categories['categories'][1]
$categoryMulti['categories'][$row['cat_id']] = $row;
$categoryMulti['parent_cats'][$row['parent_id']][] = $row['cat_id'];
$allCategories[] = $row;
}
function listCategoryTree($parent, $category)
{
$html = "";
if (isset($category['parent_cats'][$parent])) {
$html .= "<ul>\n";
foreach
($category['parent_cats'][$parent] as $cat_id) {
if (!isset($category['parent_cats'][$cat_id])) {
$html .= "<li>" . $category['categories'][$cat_id]['cat_name'] . "</li>";
} else {
$html .= "<li>" . $category['categories'][$cat_id]['cat_name'];
$html .= listCategoryTree($cat_id, $category);
$html .= "</li>";
}
}
$html .= "</ul> \n";
}
return $html;
}
用法:echo listCategoryTree(0, $categoryMulti);
这是我在下拉菜单中使用的方式,仅作为示例,它可以正常工作:
function selectCategories($categories)
{
foreach ($categories as $category) {
echo '<option value="' . $category['cat_id'] . '">' . $category['cat_name'] . '</option>';
}
}
selectCategories($allCategories);
这是我尝试使其与选定的类别方法一起工作的示例之一,但不幸的是,它没有工作。
我在函数中创建了一个新数组:
$subCategories = array();
$subCategories[] = $row;
并且将其作为如下函数调用,但没有用。
function cats($categories)
{
foreach($categories as $category) {
echo '<ul>';
echo '<li>' . $category['cat_name'] . '</li>';
echo '</ul>';
}
}
echo cats(100, $subCategories);
有两种解决方案。首先,我将使用以下数据(categories
表)作为示例。
+----+--------------------------+-----------+
| id | name | parent_id |
+----+--------------------------+-----------+
| 1 | Electronics | NULL |
| 2 | Apparel & Clothing | NULL |
| 3 | Phones & Accessories | 1 |
| 4 | Computer & Office | 1 |
| 5 | Men's Clothing | 2 |
| 6 | Women's Clothing | 2 |
| 7 | Cell Phones | 3 |
| 8 | Cell Phone Accessories | 3 |
| 9 | Phone Parts | 3 |
| 10 | Computers & Accessories | 4 |
| 11 | Tablets & Accessories | 4 |
| 12 | Computer Peripherals | 4 |
| 13 | Computer Components | 4 |
| 14 | Office Electronics | 4 |
+----+--------------------------+-----------+
您可以使用WITH (Common Table Expressions)子句在一个查询中轻松获取类别的所有类别或子类别(需要MySQL 8.0):
// Database connection
$options = [
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
];
$pdo = new PDO('mysql:host=localhost;dbname=<DATABASE_NAME>', '<USERNAME>', '<PASSWORD>', $options);
function getCategories(PDO $db, $parentId = null)
{
$sql = $parentId ? 'WITH RECURSIVE cte (id, name, parent_id) AS (SELECT id, name, parent_id FROM categories WHERE parent_id = ? UNION ALL SELECT c.id, c.name, c.parent_id FROM categories c INNER JOIN cte ON c.parent_id = cte.id) SELECT * FROM cte' : 'SELECT * FROM categories';
$stmt = $db->prepare($sql);
$stmt->execute($parentId ? [$parentId] : null);
return $stmt->fetchAll();
}
如果您使用的是MySQL 5.7,请按如下所示更改该功能:
function getCategories(PDO $db, $parentId = null)
{
$sql = $parentId ? 'SELECT id, name, parent_id FROM (SELECT * FROM categories ORDER BY parent_id, id) c, (select @pv := ?) initialisation WHERE find_in_set(parent_id, @pv) AND LENGTH(@pv := concat(@pv, ",", id))' : 'SELECT * FROM categories';
$stmt = $db->prepare($sql);
$stmt->execute($parentId ? [$parentId] : null);
return $stmt->fetchAll();
}
要获取数据库中的所有类别:
$allCategories = getCategories($pdo);
输出:
+----+--------------------------+-----------+
| id | name | parent_id |
+----+--------------------------+-----------+
| 1 | Electronics | NULL |
| 2 | Apparel & Clothing | NULL |
| 3 | Phones & Accessories | 1 |
| 4 | Computer & Office | 1 |
| 5 | Men's Clothing | 2 |
| 6 | Women's Clothing | 2 |
| 7 | Cell Phones | 3 |
| 8 | Cell Phone Accessories | 3 |
| 9 | Phone Parts | 3 |
| 10 | Computers & Accessories | 4 |
| 11 | Tablets & Accessories | 4 |
| 12 | Computer Peripherals | 4 |
| 13 | Computer Components | 4 |
| 14 | Office Electronics | 4 |
+----+--------------------------+-----------+
要获取类别的子类别:
$subCategories = getCategories($pdo, 1); // 1 is parent_id
输出:
+----+--------------------------+-----------+
| id | name | parent_id |
+----+--------------------------+-----------+
| 3 | Phones & Accessories | 1 |
| 4 | Computer & Office | 1 |
| 7 | Cell Phones | 3 |
| 8 | Cell Phone Accessories | 3 |
| 9 | Phone Parts | 3 |
| 10 | Computers & Accessories | 4 |
| 11 | Tablets & Accessories | 4 |
| 12 | Computer Peripherals | 4 |
| 13 | Computer Components | 4 |
| 14 | Office Electronics | 4 |
+----+--------------------------+-----------+
如果需要HTML输出,可以循环浏览$allCategories
/ $subCategories
(根据您的示例):
function prepareCategories(array $categories)
{
$result = [
'all_categories' => [],
'parent_categories' => []
];
foreach ($categories as $category) {
$result['all_categories'][$category['id']] = $category;
$result['parent_categories'][$category['parent_id']][] = $category['id'];
}
return $result;
}
function buildCategories($categories, $parentId = null)
{
if (!isset($categories['parent_categories'][$parentId])) {
return '';
}
$html = '<ul>';
foreach ($categories['parent_categories'][$parentId] as $cat_id) {
if (isset($categories['parent_categories'][$cat_id])) {
$html .= "<li><a href='#'>{$categories['all_categories'][$cat_id]['name']}</a>";
$html .= buildCategories($categories, $cat_id);
$html .= '</li>';
} else {
$html .= "<li><a href='#'>{$categories['all_categories'][$cat_id]['name']}</a></li>";
}
}
$html .= '</ul>';
return $html;
}
echo buildCategories(prepareCategories($allCategories));
输出:
echo buildCategories(prepareCategories($subCategories), 1);
输出:
我们将在表中添加left
和right
列,并在其中添加数字以标识属于父级的组。 (请注意,我们不会使用parent_id
列。)
+----+--------------------------+--------------------------+
| id | name | parent_id | left | right |
+----+--------------------------+--------------------------+
| 1 | Electronics | NULL | 1 | 22 |
| 2 | Apparel & Clothing | NULL | 23 | 28 |
| 3 | Phones & Accessories | 1 | 2 | 9 |
| 4 | Computer & Office | 1 | 10 | 21 |
| 5 | Men's Clothing | 2 | 24 | 25 |
| 6 | Women's Clothing | 2 | 26 | 27 |
| 7 | Cell Phones | 3 | 3 | 4 |
| 8 | Cell Phone Accessories | 3 | 5 | 6 |
| 9 | Phone Parts | 3 | 7 | 8 |
| 10 | Computers & Accessories | 4 | 11 | 12 |
| 11 | Tablets & Accessories | 4 | 13 | 14 |
| 12 | Computer Peripherals | 4 | 15 | 16 |
| 13 | Computer Components | 4 | 17 | 18 |
| 14 | Office Electronics | 4 | 19 | 20 |
+----+--------------------------+--------------------------+
现在,我们需要更改功能:
function getCategories(PDO $db, $parentId = null)
{
$sql = $parentId ? 'SELECT children.* FROM categories parent INNER JOIN categories children ON parent.left < children.left AND parent.right > children.left WHERE parent.id = ?' : 'SELECT * FROM categories';
$stmt = $db->prepare($sql);
$stmt->execute($parentId ? [$parentId] : null);
return $stmt->fetchAll();
}
要获取数据库中的所有类别:
$allCategories = getCategories($pdo);
输出:
+----+--------------------------+--------------------------+
| id | name | parent_id | left | right |
+----+--------------------------+--------------------------+
| 1 | Electronics | NULL | 1 | 22 |
| 2 | Apparel & Clothing | NULL | 23 | 28 |
| 3 | Phones & Accessories | 1 | 2 | 9 |
| 4 | Computer & Office | 1 | 10 | 21 |
| 5 | Men's Clothing | 2 | 24 | 25 |
| 6 | Women's Clothing | 2 | 26 | 27 |
| 7 | Cell Phones | 3 | 3 | 4 |
| 8 | Cell Phone Accessories | 3 | 5 | 6 |
| 9 | Phone Parts | 3 | 7 | 8 |
| 10 | Computers & Accessories | 4 | 11 | 12 |
| 11 | Tablets & Accessories | 4 | 13 | 14 |
| 12 | Computer Peripherals | 4 | 15 | 16 |
| 13 | Computer Components | 4 | 17 | 18 |
| 14 | Office Electronics | 4 | 19 | 20 |
+----+--------------------------+--------------------------+
要获取类别的子类别:
$subCategories = getCategories($pdo, 1); // 1 is parent_id
输出:
+----+--------------------------+--------------------------+
| id | name | parent_id | left | right |
+----+--------------------------+--------------------------+
| 3 | Phones & Accessories | 1 | 2 | 9 |
| 4 | Computer & Office | 1 | 10 | 21 |
| 7 | Cell Phones | 3 | 3 | 4 |
| 8 | Cell Phone Accessories | 3 | 5 | 6 |
| 9 | Phone Parts | 3 | 7 | 8 |
| 10 | Computers & Accessories | 4 | 11 | 12 |
| 11 | Tablets & Accessories | 4 | 13 | 14 |
| 12 | Computer Peripherals | 4 | 15 | 16 |
| 13 | Computer Components | 4 | 17 | 18 |
| 14 | Office Electronics | 4 | 19 | 20 |
+----+--------------------------+--------------------------+
您可以如解决方案1中所示呈现HTML。 Read more关于在嵌套集合模型中更新和插入新数据。
来源与阅读: