Failing_Solutions Posted January 23, 2013 Share Posted January 23, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/273545-how-do-you-echo-a-mysql-piviot-table-query-results/ Share on other sites More sharing options...
Failing_Solutions Posted January 23, 2013 Author Share Posted January 23, 2013 (edited) 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>'; Edited January 23, 2013 by Failing_Solutions Quote Link to comment https://forums.phpfreaks.com/topic/273545-how-do-you-echo-a-mysql-piviot-table-query-results/#findComment-1407758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.