2

I have two tables:

ROOMS
room_id>>>room_name

1       foot massage
2       face massage
3       back massage

ORDERS
room_id >>>start_date>>>>>>>end_date>>>>>>>>price

1       2012.09.10      2012.09.11      100
1       2012.09.11      2012.09.13      200
2       2012.11.11      2012.11.13      100
3       2012.12.11      2012.12.13      500

I want to out put results like this:

room number 1:

2012.09.10      2012.09.11      100
2012.09.11      2012.09.13      200

room number 2:

2012.11.11      2012.11.13      100

room number 3:

2012.12.11      2012.12.13      500

code:

$query_spa = "SELECT 
rooms.room_id,
rooms.room_name,
orders.start_date,
orders.end_date,
orders.price
FROM rooms
INNER JOIN orders
ON
 rooms.room_id =orders.room_id";

 $spa = mysql_query($query_spa, $localhost) or die(mysql_error());
 $results = array();
 while($line = mysql_fetch_array($spa, MYSQL_ASSOC)){
$results[] = $line;
  }


  $groups = array();
 foreach($results as $item)
 $groups[$item['room_id']][] = $item;
  foreach($groups as $value => $items)
      echo 'room number ' . $value . ' : ' ;
for ($i=0; $i< count($items);$i++)
{
echo $items[$i]['start_date'] ;
echo $items[$i]['end_date'] ;
echo $items[$i]['price'] ;
}

outputs :

room number 1: room number 2: room number 3: 2012.12.11 2012.12.13 500

1 Answer 1

4

Try this out:

$groups = array();
foreach($results as $item)
    $groups[$item['room_id']][] = $item;

foreach($groups as $value => $items)
{
    echo 'room number ' . $value . ' : ' ;
    foreach ($items as $item)
    {
        echo $item['start_date'] ;
        echo $item['end_date'] ;
        echo $item['price'] ;
    }
}

You can loop through sub-items with simple foreach. But better to make group sorting on fetching step:

while($line = mysql_fetch_array($spa, MYSQL_ASSOC)){
    $groups[$line['room_id']][] = $line;
}

So u got sorted result right after fetching.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.