0

I am trying to show all Items (TicketNo) of a group (GroupNo) including ItemCount. The Problem is: it shows only the first Item. This is my code:

<?php 
foreach($dbh->query('SELECT GroupNo,COUNT(*),Tickets.* 
                    FROM Tickets 
                    WHERE entryScans != 0 
                    GROUP BY GroupNo') as $row) {
    echo "<tr><th>" . $row['GroupNo'] . "</th><th>" . $row['COUNT(*)'] . "</th></tr>";
    echo "<tr><th>" . $row['TicketNo'] . "</th><th>" . $row['hash'] . "</th></tr>";
}
               ?>

EDIT: this is the result i want:

Gourp 1 (Count:3)
- Ticket 1
- Ticket 2
- Ticket 3
Group 2 (Count:2)
- Ticket 4
- Ticket 5
Group 3 (Count:4)
- Ticket 6
- Ticket 7
- Ticket 8
- Ticket 9

But what i get right now is:

Gourp 1 (Count:3)
- Ticket 1
Group 2 (Count:2)
- Ticket 4
Group 3 (Count:4)
- Ticket 6
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Sima
  • 13
  • 6
  • `GROUP BY` will do that. If you don't want that don't use `GROUP BY` and use PHP solutions like the one in https://stackoverflow.com/questions/12706359/php-array-group to create item groups – apokryfos May 13 '18 at 13:33
  • Which part of the code is wrong? what do I need to change to get all the records and not only the first one? regards – Sima May 13 '18 at 16:51
  • GROUP BY does not work the way you think it does . If you group by GroupNo you will get 1 row per distinct GroupNo that's just how GROUP BY works. – apokryfos May 13 '18 at 17:00
  • If you're using PDO you can use its `PDO::FETCH_GROUP` mode instead of the SQL `GROUP BY`. (Mind the differences, though) – Dormilich May 14 '18 at 06:39

0 Answers0