简体   繁体   English

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

[英]Create a Pivot table with PHP and MYSQL - Current displayed table repeats labels

I am hoping to get some help on how I can modify my PHP code to show a pivot table in HTML. 我希望获得一些有关如何修改PHP代码以在HTML中显示数据透视表的帮助。 Right now it creates a table with Location, Component, and Count on each row, but I would rather have Location as the column header and component as the row header with count being the data. 现在,它在每行上创建一个具有Location,Component和Count的表,但是我宁愿将Location作为列标题,将component作为行标题,以count为数据。

Here is my code: 这是我的代码:

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

Results as of right now show Location - Component - Count for each line. 截至目前,结果显示每行的位置-组件-计数。

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

That should be it 应该是这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM