如何计算类别中的所有产品,包括其子类别

时间:2018-04-09 09:18:25

标签: php mysql count

我有一个包含产品的页面。这些产品属于类别,但这些类别可以包含子类别,其中也可以包含产品。

在我的页面上,我有以下元素:

enter image description here

类别旁边的数字代表其中的产品。这适用于顶级类别。但举例来说Wastafels它没有显示任何产品,但Wastafels内部是子类别Waskom,而在该子类别中是另一个名为Waskom natuursteen的类别,其中有两个产品。< / p>

我希望计算所有子类别(和topcategory)中的所有产品,并将该数字显示在topcategory旁边。

我发现了这个问题:Counting all the posts belonging to a category AND its subcategories但这只有3级深度,我的类别可以深入无限级别(递归)。

目前,这就是我计算产品的方式:

//Get all toplevel categories
$pcat             = "SELECT * FROM `snm_categories` WHERE published = 1 and level = 1 and id NOT IN (1) order by rgt ";
$pcatcon          = $conn->query($pcat);
$catids           = '';
while ($pcat      = $pcatcon->fetch_assoc()){
    $catids[] = $pcat['id'];
    // All products inside above categories
    $aantal         = "SELECT DISTINCT id FROM `snm_content` WHERE catid = ".$pcat['id']." and state = 1";
    $aantalcon      = $conn->query($aantal);
    $aantal         = $aantalcon->fetch_assoc();

    if(is_null($aantal)){
        $totaal = '';
    }else{
        $totaal = $aantalcon->num_rows;
    }
    if($totaal != 0){
        $amount = '('.$totaal.')';
    }else{
        $amount = $totaal;
    }

    $productcatoverzicht .= '
    <li class="cat-item"><a href="'.$pcat['alias'].'">'.$pcat['title'].'</a><span class="count">'.$amount.'</span></li>';
}
echo productcatoverzicht;

但这只适用于topcategories。

我的结构是这样的:

category:
id - category id
parent_id - id of parent category (topcategory is always 1), this is the same as the id of it's parent.


article (product)
id - id of the product
catid - id of the category it belongs too (same as id of category)

我猜测我必须像我的菜单一样递归计算?

我的菜单代码:

<?PHP
    $alias = $_GET['alias'];
    //Haal alle categorieen en check gelijk of de desbetreffende categorie artikelen onder zich heeft hangen.
    $menu = "
    SELECT cat.id as cat_id, cat.level, cat.parent_id, cat.title as cat_title, cat.alias as cat_alias, cat.published, cat.rgt, cnt.state, cnt.id as content_id, cnt.catid, cnt.title as content_title, cnt.alias as content_alias
    FROM snm_categories cat
    LEFT JOIN snm_content cnt
    ON cnt.catid = cat.id
    WHERE cat.id NOT IN (1, 2, 3, 4, 5, 7)
    AND cat.published = 1
    GROUP BY cat.id
    ORDER BY cat.rgt ASC";
    $menuconn = $conn->query($menu);
    // Maak een nieuwe array om te vullen met onderstaand resultaat
    $menuData = array(
        'items' => array(),
        'parents' => array()
    );
    // Maak een nieuwe array met simpelweg items en parents, welke gekoppeld zitten aan cat_id/parent_id
    while($menu = $menuconn->fetch_assoc())
    {
        $menuData['items'][$menu['cat_id']] = $menu;
        $menuData['parents'][$menu['parent_id']][] = $menu['cat_id'];
    }

    // Functie om menu te maken, $parentId is 1 (de categorieen die geen parent hebben)
    function buildMenu($parentId, $menuData)
    {
        $html = '';
        if (isset($menuData['parents'][$parentId]))
        {
            //Als parent_id gelijk is aan 1 gooi een li eromheen (want het is geen subcat) anders een ul
            if($parentId == '1'){
              $html = '<li>';
            }else{
              $html = '<ul class="sub-menu">';
            }
            foreach ($menuData['parents'][$parentId] as $itemId)
            {
                $html .= '<li class="menu-item"><a href="'.$menuData['items'][$itemId]['cat_alias'].'">'.$menuData['items'][$itemId]['cat_title'].'</a>';

                // Voer deze functie uit binnen de functie loop (recursief)
                $html .= buildMenu($itemId, $menuData);

                $html .= '</li>';
            }
            //Als parent_id gelijk is aan 1 gooi een li eromheen (want het is geen subcat)
            if($parentId == '1'){
              $html .= '</li>';
            }else{
              $html .= '</ul>';
            }
        }
        return $html;
    }
    // Echo het resultaat van de functie en geef 1 mee als parent_id
    echo buildMenu(1, $menuData);
    ?>

如何解决这个问题?

所以当我得到这样的数据时:

category - wastafels - 2 products
subcategory - wastafelssub - 5 products
subsubcategory - wastafelssubsub - 2 products

我需要所有产品,这将是9。

0 个答案:

没有答案