2014-05-04 73 views
0

我想创建一个与用户搜索相关的类别及其子类别(包括计数)的ul列表。相关类别和具有产品计数的子类

例子。 如果你是去eBay和搜索MAC书亲,左侧会显示

Computers/Tablets & Networking (20 items) 
    -Laptop & Desktop Accessories (5 items) 
    -Laptops & Netbooks (15 items) 

我有2个表:

Categories Table 

id | category_name | parent_id 
-------------------------------------- 
1 | computers  |  0 
2 | apple   |  2 
3 | microsoft  |  2 
4 | accessories  |  0 
5 | mouse   |  4 
6 | keyboards  |  4 
7 | printers  |  4 

############################################################# 

And Products Table 

id | product_name | category_id 
-------------------------------------- 
1 | macbook pro  |  2 
2 | macbook air  |  2 
3 | surface pro  |  3 
4 | ipad    |  2 
5 | backlit keyboard |  6 
6 | mini keyboard |  6 
7 | 3 in 1 printer |  7 


some sql and php to disply: 

computers (4) 
    -apple (3) 
    -microsoft (1) 

accessories (3) 
    -keyboards (2) 
    -printers (1) 
    -(dont show mouse because no mouses in products table) 

我已经花了几个小时寻找,但还没有发现什么即时寻找。

问候
巴蒂尔

+0

你在使用什么数据库?您的类别层次结构是否运行深度超过1级? –

+0

即时通讯使用MySQL和没有只是主要类别及其相关的子类别。先谢谢了。 – Shane

回答

1

我认为你可以得到你想要的只是用with rollup什么。问题是,总和将基本行后出现:使用第二statment

select parent_name, category_name, numcategories 
from (select cp.category_name as parent_name, c.category_name, count(*) as numcategories 
     from products p join 
      categories c 
      on p.categoryid = c.id join 
      categories cp 
      on c.parentid = cp.id 
     group by cp.category_name, c.category_name with rollup 
    ) t 
where category_name is not null 
order by category_name, 
     category_name is null desc; 
+0

我似乎无法得到论文的工作,但我不得不循环/ sqls一个主要类别和计数器,然后secound循环的子类别和计数器。 – Shane

+0

@Shane。 。 。你不应该那样做。这些如何不工作?语法错误?错误的结果? –

+0

请看下面的帖子 – Shane

0

@Gordon Linoff:

$result = mysqli_query($db,"select parent_name, category_name, counter from (select cp.category_name as parent_name, c.category_name, count(*) as counter from adverts p join categories c on p.category_id = c.id join categories cp on c.parent_id = cp.id group by cp.category_name, c.category_name with rollup) t where category_name is not null order by parent_name, counter DESC"); 
$count = mysqli_num_rows($result); 

if($count >= 1){ 
    while($row = mysqli_fetch_array($result)) { 

    if (empty($rowo['category_name'])) { 

     $category_view = ("<strong>". $rowo['parent_name']. "</strong> (". $rowo['counter']. ")"); 

    }else{ 

     $category_view = ("". $rowo['category_name']. " (". $rowo['counter']. ")"); 

    } 


    echo("<li>". $category_view ."</li>"); 


    } // while loop 

} // counter >= 1 




Antiques (3) 
    -Fabric/ Textiles (2) 
    -Ethnographic Antiques (1) 
Art (5) 
    -Photographs (4) 
    -Drawings (1) 

select cp.category_name as parent_name, c.category_name, count(*) as numcategories 
    from products p join 
     categories c 
     on p.categoryid = c.id join 
     categories cp 
     on c.parentid = cp.id 
    group by cp.category_name, c.category_name with rollup 

之前得到它,试试这个再次感谢戈登。

+0

不是为了推动我的运气,但也许是艺术第一,因为柜台大于古董。 – Shane

相关问题