简体   繁体   中英

Display grouped sql results

I have a sql query that groups results. The print_r shows the results I would like are there. Now I would like to display these results in table groups ie Table One with a list of all the seats with that table, Table Two etc.

I have tried all kinds of things to get this done to no avail... here is the code. I can easily display the records - but would like to display them by groups arghhh

$seatings = $wpdb->get_results("SELECT 
                               bb_cl_seating.table,  
                               bb_cl_seating.seat, 
                               bb_cl_seating.seat_id,  
                               bb_events_attendee.fname,  
                               bb_events_attendee.lname, 
                               bb_events_attendee.email
                          FROM bb_cl_seating
                          LEFT JOIN bb_events_attendee 
                          ON bb_cl_seating.id = bb_events_attendee.id
                          WHERE bb_cl_seating.event_id = '1' ");

foreach ($seatings as $seating) {


    } // Ends foreach
$seatings = $wpdb->get_results("SELECT 
                               bb_cl_seating.table,  
                               bb_cl_seating.seat, 
                               bb_cl_seating.seat_id,  
                               bb_events_attendee.fname,  
                               bb_events_attendee.lname, 
                               bb_events_attendee.email
                          FROM bb_cl_seating
                          LEFT JOIN bb_events_attendee 
                          ON bb_cl_seating.id = bb_events_attendee.id
                          WHERE bb_cl_seating.event_id = '1' ");

foreach ($seatings as $seating => $group) {
    //$data[table] = seat,seat2,seat3...
    $data[$group[1]] = $data[$group[1]].','.$group[2];
    } // Ends foreach

In this example create one array $data of tables with yours seats sort by ','.

Its help?

This did the trick - hope it helps someone else:-)

$seatings = $wpdb->get_results("SELECT bb_cl_seating.table, bb_cl_seating.seat, bb_cl_seating.seat_id, bb_events_attendee.fname, bb_events_attendee.lname, bb_events_attendee.email
FROM bb_cl_seating
LEFT JOIN bb_events_attendee ON bb_cl_seating.id = bb_events_attendee.id
WHERE bb_cl_seating.event_id = '1' 
ORDER BY bb_cl_seating.table, bb_cl_seating.seat
");



$table_title = '';
foreach($seatings as $result => $col) {


     if($table_title !== $col->table) {
        $table_title = $col->table;
        echo "<strong>$table_title</strong>";
        echo "<br />";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM