Grouping results together and counting outcomes

I have pulled some data from my database and have output the following

20f -Won:0 Placed:0 
20f -Won:0 Placed:0 
20f -Won:0 Placed:0 
20f -Won:0 Placed:0 
20f -Won:0 Placed:0 
20f -Won:0 Placed:0 
20f -Won:1 Placed:1 
20f -Won:1 Placed:1 
20f -Won:0 Placed:0 
21.5f -Won:1 Placed:1 
21.5f -Won:0 Placed:0 
21.5f -Won:0 Placed:1 
21.5f -Won:0 Placed:1 
21.5f -Won:0 Placed:1 
21f -Won:0 Placed:1 
21f -Won:0 Placed:0 
22f -Won:0 Placed:0 
22f -Won:0 Placed:0 
22f -Won:0 Placed:0 
23f -Won:0 Placed:0 
23f -Won:0 Placed:0 
23f -Won:0 Placed:0 
24f -Won:0 Placed:0 
24f -Won:0 Placed:1 

Open in new window

this has been done by using the following outcome

  $data .= .$Horsedist. " -Won:".$wintotals. " Placed:".$placetotals." <br />";

Open in new window

What i want to do is group each distance up and add the won and placed number so it would output the following (Raced been the total of the number)

20f -Won:2 Placed:2 Raced:9
21.5f -Won:1 Placed:4 Raced:5
21f -Won:0 Placed:1 Raced:2
22f -Won:0 Placed:0 Raced:3
23f -Won:0 Placed:0 Raced:3
24f -Won:0 Placed:1  Raced:2

Open in new window


I am struggling to figure how to do this.
runnerjp2005Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Depending on your db, you may be able to do a cross tab before you output it.  But it will probably faster to output your data and let the client do the cross tab.    Check out pivotjs http://rwjblue.github.io/pivot.js/ or http://webix.com/pivot/.  Is this something you can wrap your head around or do you want to stay with php or your database?
runnerjp2005Author Commented:
Hey  Scott- im not pulling this data from tables - Wintotals and Place totals are worked out via code –
Julian HansenCommented:
I assume you are using a loop to produce this - based on the following line

 $data .= .$Horsedist. " -Won:".$wintotals. " Placed:".$placetotals." <br />";

Open in new window


You could do this - this assumes that your code is in some sort of loop which you will do in the bit marked
$summary = array()
foreach( YOUR LOOP HERE)
{
  if (!isset($summary[$Horsedist])) {
    $summary[$Horsedist] = array (
      'win' => 0,
      'place' => 0,
      'race' => 0
    );
  }
  
  // Your current processing here
  
  $summary[$Horsedist]['win'] += $wintotals;
  $summary[$Horsedist]['place'] += $placetotals;
  $summary[$Horsedist]['race'] ++;  
}
// Dump the results
foreach($summary as $horsedist => $results) {
  echo "{$horsedist} -Won:{$results['win']} Placed: {$results['place']} Raced: {$results['race']}<br/>";
}

Open in new window

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
> im not pulling this data from tables

In the case of those js libraries, you are outputting data to json or csv which can easily be done via php.  Then the javascript generates the pivot table.    The main advantage is you can rework the pivots on the fly without going back and forth to your data source.  If you are working with a lot of data, it will be faster to process.  If you only have 20 or 30 rows of data, you probably wouldn't notice a difference.

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
Ray PaseurCommented:
For just a quick solution, I might do something like this.  You could format the output better.  This will give you the aggregation of the data.
http://iconoun.com/demo/temp_runnerjp2005.php
<?php // demo/temp_runnerjp2005.php

/**
 * http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28675537.html
 */
error_reporting(E_ALL);
echo '<pre>';

// A DATA FRAGMENT
$records = <<<EOD
20f -Won:0 Placed:0
20f -Won:0 Placed:0
20f -Won:0 Placed:0
20f -Won:0 Placed:0
20f -Won:0 Placed:0
20f -Won:0 Placed:0
20f -Won:1 Placed:1
20f -Won:1 Placed:1
20f -Won:0 Placed:0
21.5f -Won:1 Placed:1
21.5f -Won:0 Placed:0
21.5f -Won:0 Placed:1
21.5f -Won:0 Placed:1
21.5f -Won:0 Placed:1
21f -Won:0 Placed:1
21f -Won:0 Placed:0
22f -Won:0 Placed:0
22f -Won:0 Placed:0
22f -Won:0 Placed:0
23f -Won:0 Placed:0
23f -Won:0 Placed:0
23f -Won:0 Placed:0
24f -Won:0 Placed:0
24f -Won:0 Placed:1
EOD;


Class Aggregate
{
    public function __construct()
    {
        $this->dataset = [];
    }
    public function load($element)
    {
        $thing = explode(' ', $element);
	@$this->dataset[$thing[0]]['W'] += preg_replace('/[^0-9]/', NULL, $thing[1]);
	@$this->dataset[$thing[0]]['P'] += preg_replace('/[^0-9]/', NULL, $thing[2]);
	@$this->dataset[$thing[0]]['R'] ++;
    }
}

// MAKE AN ARRAY FROM THE DATA SET AND LOAD THE AGGREGATE OBJECT
$arr = explode(PHP_EOL, $records);
$data = new Aggregate;
foreach ($arr as $str)
{
    $data->load($str);
}

// SHOW THE AGGREGATION
print_r($data);

Open in new window

runnerjp2005Author Commented:
Thanks all for you time on this issue. I was able to solve it following the above example :)
Julian HansenCommented:
Which example exactly?

You assigned points to a post that did not include an example?
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
runnerjp2005, did you mean to select the example above the answer you accepted?
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.