0

I searched for this on older post of stackoverflow questions but i can't find the right solution for my problem here.

I have 2 tables in database:

article_category table:

cat_id   category
1        Elektronics
2        Furniture

article_subcategory table:

id   cat_id   subcategory            total
1    1        Kitchen-elektronics      1
2    1        Computers                5
3    2        Kitchen furniture        2
4    2        Living room furniture    7

What i want to do is to build a query for outputting the data in the HTML table like this:

                   <li>
                      <a href="#">
                         <div class="clearfix"><span class="pull-left">category items</span> <span class="pull-right">(total items)</span></div>
                      </a>

                      <ul class="test">

                         <li class="current"><a href="#">subcategory items (total items)</a></li>

                      </ul>
                   </li>

The total items in the first li must be the total items from all subcats underneath the head category. I have made a basic query but i see only the first subcategory item underneath the head category here. If it is possible, i want to do it without the use of functions here, but something in pure PHP.

SELECT 
   sc.subcategory,
   sc.total
   ac.category
FROM 
   article_category ac
JOIN
   article_subcategory sc
ON
   sc.cat_id = ac.cat_id
GROUP BY
   ac.cat_id

EDIT: FINAL SOLUTION

<?php
         //Make connection to db

$sql = "
            SELECT 
               ac.category,
               sc.aantal,
               sc.subcategory ,
               group_concat(sc.subcategory) as items,
               group_concat(sc.aantal) as totals,
               SUM(sc.aantal) as mainItemsTotal,
               group_concat(CONCAT(sc.subcategory, '-', sc.aantal)) as items2
            FROM 
               article_category ac
            JOIN
               article_subcategory sc
            ON
               sc.cat_id = ac.cat_id
            GROUP BY
               ac.cat_id
            ORDER BY 
               ac.category asc, sc.subcategory asc
            "; 
$res = mysql_query($sql) or die (mysql_error());


         while($row = mysql_fetch_assoc($res)) {

?>
        <ul>
                <li><?php echo $row['category']; ?> (<?php echo $row['mainItemsTotal']; ?>)</li>
                        <?php 
                        $items = $row['items2'];
                        $items_array = explode(',',$items);

                        if(is_array($items_array) && count($items_array) > 0 ){
                        ?>
                            <ul>
                                <?php 
                                foreach($items_array as $item_name){ 

                                    $arr = explode('-',$item_name);
                                    if($arr[1] > 0) { ?>
                                       <li>
                                           <?php echo $arr[0] . ' ('. $arr[1]. ')'; ?>
                                       </li>
                                <?php 
                                    } 
                               } 
                               ?>
                            </ul>
                        <?php } ?>
        </ul>
<?php } ?>

Now i have this output:

Elektronics (6)
   Kitchen-elektronics (1)
   Computers (5)

Furniture (9)
   Kitchen furniture (2)
   Living room furniture (7)

And that is all correct. Subitems with total = 0 are not shown.

Brecht27
  • 85
  • 1
  • 1
  • 8

2 Answers2

2

You are close. Just SUM the total.

Below is the updated query where

GROUP_CONCAT(sc.total) AS subItemsTotal will give you comma separated total for each sub item.

SUM(sc.total) AS mainItemsTotal will give you total items of all sub cats

   SELECT 
    ac.category,
    GROUP_CONCAT(sc.subcategory) AS items,
    GROUP_CONCAT(sc.total) AS subItemsTotal,
    SUM(sc.total) AS mainItemsTotal
FROM 
    article_category ac
JOIN
    article_subcategory sc
ON
    sc.cat_id = ac.cat_id
GROUP BY
    ac.cat_id

Working Fiddle

Community
  • 1
  • 1
Samir Selia
  • 7,007
  • 2
  • 11
  • 30
  • And what about the totals for each subitem? I think it must be done in the $item_array? – Brecht27 Dec 20 '16 at 12:10
  • Have updated my answer based on latest edit in your post. – Samir Selia Dec 20 '16 at 12:13
  • The part of SUM(sc.total) AS mainItemsTotal is working correct. But what to do with the GROUP_CONCAT(sc.total) AS subItemsTotal in my $items_array? How can i output that in the HTML (li elements)? – Brecht27 Dec 20 '16 at 12:23
  • 1
    explode it with comma just like you did for items array and show it based on array index – Samir Selia Dec 20 '16 at 13:22
  • Thanks Samir for your comment and helping me out here. I have updated my post with the final solution working. – Brecht27 Dec 20 '16 at 14:14
  • Why don't you use ROLLUP ? – Daniel E. Dec 20 '16 at 14:25
  • Never heard before of ROLLUP? What is different with the code i have edited? – Brecht27 Dec 20 '16 at 14:34
  • Is it possible to look at the ORDER BY in my final solution. Sometimes i refresh the page, the sorting is different?? – Brecht27 Dec 20 '16 at 14:35
  • @Samir: can you look also to my other post simular to this one but a little different: http://stackoverflow.com/questions/41250000/query-to-build-a-messages-system-from-different-joined-tables – Brecht27 Dec 20 '16 at 19:42
0

I have an other solution, you will have to change your php code so maybe you won't use it. But I think in a sql way it's a better solution :

I use the group by modifier "WITH ROLLUP" wich do what you ask for : http://dev.mysql.com/doc/refman/5.7/en/group-by-modifiers.html

Query :

SELECT 
    ac.category,
    sc.subcategory AS items,
    SUM(sc.total) AS Total
FROM 
    article_category ac
JOIN
    article_subcategory sc ON sc.cat_id = ac.cat_id
GROUP BY ac.category,items WITH ROLLUP

Result :

|----------------------------------------|
|category   |items                |Total |
|----------------------------------------|
|Elektronics|Computers            |5     
|Elektronics|Kitchen-elektronics  |1
|Elektronics|NULL                 |6
|Furniture  |Kitchen furniture    |2
|Furniture  |Living room furniture|7
|Furniture  |NULL                 |9
|NULL       |NULL                 |15
Daniel E.
  • 2,440
  • 1
  • 14
  • 24