使用PHP和MYSQL创建数据透视表 - 向左添加列

时间:2016-09-21 09:02:56

标签: php mysql pivot

我想在左表上添加列,其Component2:

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

1 个答案:

答案 0 :(得分:0)

我认为你在sql脚本中有错误。请让我知道它进一步调查的错误信息。我更新你的脚本,你可以试试。你想在'Component2'中显示哪些数据?

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