views:

98

answers:

3

I have a table that looks like:

Client_ID | Order_ID | 
10        | 1        | 
10        | 2        |
10        | 3        |
14        | 6        |
14        | 7        |
14        | 9        |

Is there an elegant way in PHP of printing out this result set putting in evidence the client_id groups instead of making a loop that remembers the previous Client_ID and verifies if it changed.

Client 10:

  • Order 1
  • Order 2
  • Order 3

Client 14:

  • Order 6
  • Order 7
  • Order 9
+2  A: 

All SQL can do is capture a series of rows that hold no relation from one row to the next. It's up to PHP to impose some structure to those results with a loop probably like what you envisioned:

$last_client_id = null;
while($row = mysql_fetch_assoc($results)) {
    if ($row['client_id'] != $last_client_id) {
        // Do whatever it is you do for each new client
    }
    // Do whatever it is you do for each order
    $last_client_id = $row['client_id'];
}
VoteyDisciple
Ok, I expected that. Thanks.
Wadih M.
+1  A: 

Take a look at the GROUP_CONCAT function: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

edit: I think I wrongly assumed you get it from database :/

Pomyk
+1  A: 

Short: No.

Long: How else can the previous iteration know that a new id is coming? There must be some kind of case handling here, it will not be possible without.

codymanix