Jump to content

How do you echo a mysql piviot table query results?


Failing_Solutions

Recommended Posts

I have a query that generates a mysql pivot table which is great, but now I can't figure out how to echo that out in php.

The query is:

SELECT
 IFNULL(Worker, 'Totals') AS Operator,
 sums.`2012-11-26`, sums.`2012-11-27`, sums.`2012-11-28`
FROM (
 SELECT
  w.worker_name as 'Worker',
   ROUND(SUM(IF(production_date='2012-11-26',production_net/(production_time/60),0)),2) As '2012-11-26',
   ROUND(SUM(IF(production_date='2012-11-27',production_net/(production_time/60),0)),2) As '2012-11-27',
   ROUND(SUM(IF(production_date='2012-11-28',production_net/(production_time/60),0)),2) As '2012-11-28'
 FROM production
JOIN workers AS w USING (worker_id)
 WHERE product_id='1267'
 GROUP BY worker_id
) AS sums

 

To fetch data I've generally used something like

while($rollup_row=mysql_fetch_array($rollup_query)) {
echo $rollup_row['Something'];
}

 

However that doesn't seem to a valid way to get through these results. I do have an array that holds the aliases to create this query but I'm not sure how to itterate through it to get a nice pretty table.

 

Anybody have some advice,

Thank you

Whelp

 

It isn't easy, but by creating verticle array from the query results then looping through them you can get a table

 

Here's my code......

 

 $results.='<div><table border="0" class="sortable"><th>Operator</th>';
for($n=0;$n<$count_dates;$n++) {
//create the date column headers in us english format
$results.='<th>'.date('m-d-Y',strtotime($valid_date_array[$n])).'</th>';
}

//echo $rollup_sql;
//create the operator array
$operators = array();
///create all the verticle results arrays
foreach($valid_date_array as $value) {
$col[$value]=array();
}
while($rollup_row=mysql_fetch_array($rollup_query)) {
array_push($operators, $rollup_row['Operator']);
//push values to the vertical arrays
foreach($valid_date_array as $value) {
array_push($col[$value],$rollup_row[$value]);
}
/* //verify vertical arrays
foreach($valid_date_array as $value) {
print_r($col[$value]);
} okay! */
}
$count_rows=count($operators);
///echo $count_rows . ' rows<br>';
//echo $count_dates+1 . ' columns<br>';
$i=0;
//loop through all the veritical arrays and creat rows
for($k=0;$k<$count_dates;$k++) {
//set row color
if ($i%2 !=0) {
$rowColor = 'tr1';
} else {
$rowColor ='tr2';
}
//first we create a row with the operators name
$results.='<tr class="'.$rowColor.'"><td>'.$operators[$k].'</td>';
//now loop through each date array
foreach($valid_date_array as $value) {
$results.='<td>'.$col[$value][$k].'</td>';
}
//then end the row and let the loop continue to create the next
$results.='</tr>';
}

$results.='</table></div>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.