簡體   English   中英

使用PHP和MYSQL創建數據透視表 - 向左添加列

[英]Create a Pivot table with PHP and MYSQL - add column left

我想在左表上添加列,其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>';

我認為你在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>';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM