gpt4 book ai didi

php - 使用 PHP 和 MYSQL 创建数据透视表 - 当前显示的表重复标签

转载 作者:行者123 更新时间:2023-11-29 13:56:26 29 4
gpt4 key购买 nike

我希望获得一些有关如何修改 PHP 代码以在 HTML 中显示数据透视表的帮助。现在,它创建了一个表,每行包含位置、组件和计数,但我宁愿将位置作为列标题,将组件作为行标题,而计数作为数据。

这是我的代码:

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>";
}

截至目前的结果显示位置 - 组件 - 每行计数。

最佳答案

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>';

应该是这样

关于php - 使用 PHP 和 MYSQL 创建数据透视表 - 当前显示的表重复标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15794439/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com