我希望能够帮助我修改PHP代码,以便在HTML中显示数据透视表。现在它在每一行上创建一个包含Location,Component和Count的表,但我宁愿将Location作为列标题,将Component作为行标题,其中count是数据。
这是我的代码:
function print_table_commodity_location_count_inbd_pivot(){
$query_items = "SELECT `tbl_origin_dest`.`name` AS 'Location',
`tbl_component`.`component_name` AS 'Component',
COUNT(`tbl_entry_item`.`entry_item_id`) AS 'Comp_Count'
FROM tbl_entry
LEFT JOIN tbl_entry_item ON `tbl_entry`.`entry_id`=`tbl_entry_item`.`entry_id`
LEFT JOIN tbl_customer ON `tbl_entry`.`customer_id`=`tbl_customer`.`customer_id`
LEFT JOIN tbl_serv_prov ON `tbl_entry`.`serv_prov_id`=`tbl_serv_prov`.`serv_prov_id`
LEFT JOIN tbl_origin_dest ON `tbl_entry`.`location`=`tbl_origin_dest`.`id`
LEFT JOIN tbl_project ON `tbl_entry`.`project_id`=`tbl_project`.`project_id`
LEFT JOIN tbl_component ON `tbl_entry_item`.`component_id`=`tbl_component`.`component_id`
WHERE `tbl_entry`.`in_out_type`='I' AND `outbound_entry_id` IS NULL AND `outbound_entry_item_id` IS NULL
GROUP BY `tbl_origin_dest`.`name`,
`tbl_component`.`component_name`";
$result_items=mysql_query($query_items);
$num_items=mysql_numrows($result_items);
echo "<table style=\"background-color: silver;\" border=\"1\" cellpadding=\"1\" cellspacing=\"1\">
<tbody>
<tr>
<td><font face=\"Arial, Helvetica, sans-serif\">Location</font></td>
<td><font face=\"Arial, Helvetica, sans-serif\">Component</font></td>
<td><font face=\"Arial, Helvetica, sans-serif\">Inventory Count</font></td>
</tr>";
$x=0;
while ($x < $num_items) {
$x1=mysql_result($result_items,$x,"Location");
$x2=mysql_result($result_items,$x,"Component");
$x3=mysql_result($result_items,$x,"Comp_Count");
echo "<tr>
<td><font face=\"Arial, Helvetica, sans-serif\">".$x1."</font></td>
<td><font face=\"Arial, Helvetica, sans-serif\">".$x2."</font></td>
<td><font face=\"Arial, Helvetica, sans-serif\">".$x3."</font></td>
</tr>";
$x++;
}
echo "</table>";
}
截至目前的结果显示 位置 - 组件 - 每行的计数。
答案 0 :(得分:1)
function print_table_commodity_location_count_inbd_pivot(){
$query_items = "SELECT `tbl_origin_dest`.`name` AS 'Location',
`tbl_component`.`component_name` AS 'Component',
COUNT(`tbl_entry_item`.`entry_item_id`) AS 'Comp_Count'
FROM tbl_entry
LEFT JOIN tbl_entry_item ON `tbl_entry`.`entry_id`=`tbl_entry_item`.`entry_id`
LEFT JOIN tbl_customer ON `tbl_entry`.`customer_id`=`tbl_customer`.`customer_id`
LEFT JOIN tbl_serv_prov ON `tbl_entry`.`serv_prov_id`=`tbl_serv_prov`.`serv_prov_id`
LEFT JOIN tbl_origin_dest ON `tbl_entry`.`location`=`tbl_origin_dest`.`id`
LEFT JOIN tbl_project ON `tbl_entry`.`project_id`=`tbl_project`.`project_id`
LEFT JOIN tbl_component ON `tbl_entry_item`.`component_id`=`tbl_component`.`component_id`
WHERE `tbl_entry`.`in_out_type`='I' AND `outbound_entry_id` IS NULL AND `outbound_entry_item_id` IS NULL
GROUP BY `tbl_origin_dest`.`name`,
`tbl_component`.`component_name`";
$result_items=mysql_query($query_items);
$locationArray = array();
$components = array()
echo '<table style=\"background-color: silver;\" border=\"1\" cellpadding=\"1\" cellspacing=\"1\">
<tbody><tr><td></td>';
$locations = array();
while($row = mysql_fetch_assoc($result_items)){
//create an array with all the locations
if(!in_array($row['Location'], $locations){
$locations[] = $row['Location'];
}
//create an array for every component and the count at a location
$components[$row['Component']][$row['Location']] = $row['Comp_count'];
}
//create the first row columnns (header)
foreach($locations as $location){
echo '<td>'.$location.'</td>'
}
echo '</tr>';
foreach($components as $component => $componentLocations){
echo '<tr><td>'.$component.'</td>';
foreach($locations as $loc){
//if there is no component at this location
if(!array_key_exists($loc, $componentLocations)){
echo '<td>0</td>';
}else{
echo '<td>'.$componentLocations[$loc].'</td>';
}
}
echo .'</tr>';
}
echo '</tbody></table>';
应该是它