reformat the outcome of a query

Hi experts,

I've got this select query:

SELECT type, date, volume FROM mytable WHERE active='1'

what results in:

Type      Date      volume
1      2011-12-12      13
1      2011-12-13      1
1      2011-12-14      15
1      2011-12-15      13
1      2011-12-16      2
3      2011-12-12      2
3      2011-12-13      6
3      2011-12-14      8
3      2011-12-15      12
3      2011-12-16      6

But I'd like it to become like this:
 wanted format

Is there a simple way to do this in PHP can someone please help me by pointing me in the right direction? Or can this be done in the SQL (Mysql) query ? Mybe a post or artickle that is about this subject..
and how do you call this (English is not my native lanuage) .

Multiple comments are very welcome!

Thanks,

LVL 1
SteynskAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StingRaYCommented:
I think it is called Pivot.

You may take a look at http://gonzalo123.wordpress.com/2010/01/24/pivot-tables-in-php/

There is a code and example available there.
0
SteynskAuthor Commented:
Thank you for the quick response but I can't figure out how to implement this on my case. Don't you have more simple single table sample?

Thanks,
0
StingRaYCommented:
Try this:

<?php

$rows = array(
	array("type"=>"1","date"=>"2011-12-12","volume"=>"13"),
	array("type"=>"1","date"=>"2011-12-13","volume"=>"1"),
	array("type"=>"1","date"=>"2011-12-14","volume"=>"15"),
	array("type"=>"1","date"=>"2011-12-15","volume"=>"13"),
	array("type"=>"1","date"=>"2011-12-16","volume"=>"2"),
	array("type"=>"3","date"=>"2011-12-12","volume"=>"2"),
	array("type"=>"3","date"=>"2011-12-13","volume"=>"6"),
	array("type"=>"3","date"=>"2011-12-14","volume"=>"8"),
	array("type"=>"3","date"=>"2011-12-15","volume"=>"12"),
	array("type"=>"3","date"=>"2011-12-16","volume"=>"6")
);

$out = array();
for($i=0,$n=count($rows);$i<$n;$i++) {
	$type = $rows[$i]["type"];
	$date = $rows[$i]["date"];
	$volume = $rows[$i]["volume"];
	if (!isset($out[$type])) {
		$out[$type] = array();
	}
	$out[$type][$date] = $volume;
}

print_r($out);

?>

Open in new window



Output

Array
(
    [1] => Array
        (
            [2011-12-12] => 13
            [2011-12-13] => 1
            [2011-12-14] => 15
            [2011-12-15] => 13
            [2011-12-16] => 2
        )

    [3] => Array
        (
            [2011-12-12] => 2
            [2011-12-13] => 6
            [2011-12-14] => 8
            [2011-12-15] => 12
            [2011-12-16] => 6
        )

)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteynskAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.