Reputation: 1789
I have some rows in a database which represent line items in a sale. Each line item has five columns:
Sales
table).Products
table).group
value.I would like to iterate over all the rows for a particular sale_id
to produce a list where:
group
values are associated togetheris_subitem == false
for that group appears firstHow can I create a data structure which facilitates this and iterate the way I'd like?
Upvotes: 0
Views: 232
Reputation: 290
Something like this might work.. I haven't tested it.
$data = array();
$result = mysql_query( $your_query_here );
while( $row = mysql_fetch_assoc( $result ) )
{
if( !isset( $data[$row['group']] ) )
{
$data[$row['group']] = array();
}
if( false == $row['is_subitem'] )
{
// when subitem is false we put the item at at the front of the array by unshifting it.
array_unshift( $data[$row['group']], $row );
}
else
{
// else we just add the row to the end.
$data[$row['group']][] = $row;
}
}
Upvotes: 1