我希望就如何修改我的 PHP 代码以在 HTML 中显示数据透视表获得一些帮助。现在它会在每一行上创建一个包含 Location、Component 和 Count 的表,但我宁愿将 Location 作为列标题,将组件作为行标题,其中 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>";
}
截至目前的结果显示每行的位置 - 组件 - 计数。