0

我有 tbl_building、tbl_rooms、tbl_lighting 等表。

每个都有自己的元组,因此可以有许多建筑物、建筑物内的许多房间以及房间内的许多照明项目。这是通过为每个建筑物/房间/照明提供自己的 ID 来构建的。

我要完成的任务之一是在 Excel 电子表格中为每个建筑物内的每个房间显示灯光列表。

这是我的代码。这显然是错误的,但我无法理解输出数据。

function getLights($buildingID, $d, $objPHPExcel, $status){

        $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], 'Please Enter Hours Of Usage: ');
        include("setText.php");$status[2]++;

        $groupStart = $status[1].$status[2];
            $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], 'Lights Used:');
            $status[1]++;
            $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], 'Count');
            $status[1]++;
            $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], 'kWh/yr');
            $status[1]++;
            $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], 'Energy Saving kWh/yr');
            $status[1]++;
            $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], 'Saving(£)');
            $status[1]++;
            $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], 'Cost');
            $status[1]++;
            $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], 'Change To...');    
        $groupEnd = $status[1].$status[2];

        include("setHeadersText.php");groupFill($objPHPExcel, $groupStart, $groupEnd, "calc");
        $status[2]++;$status[1] = chr(65);

        $query = "SELECT r_id FROM tbl_roomDesc WHERE b_id = '$buildingID'";
        $result = $d->query($query);
        $rooms = array();

        array_push($rooms, 2);//echo "<br/>".$row['r_id'];



            //echo "<br/>Building:";
        /*while($row = $result->fetchArray(SQLITE3_ASSOC)){ 
            array_push($rooms, $row['r_id']);//echo "<br/>".$row['r_id'];
        }*/

        //echo "<br/>";
        //var_dump($rooms);

        $lightArray = array(
            "CFL",
            "CFL Equivalent Halogen 100w spot",
            "CFL Equivalent Halogen 50w spot",
            "Double d",
            "Halogen 100w spot",
            "Halogen 50w spot",
            "PL",
            "T12 Single 4ft",
            "T12 Single 5ft",
            "T12 Single 6ft",
            "T12 Single 8ft",
            "T12 Twin 4ft",
            "T12 Twin 5ft",
            "T12 Twin 6ft",
            "T12 Twin 8ft",
            "T5 Quad 2ft",
            "T5 Quad 4ft",
            "T5 Single 4ft",
            "T5 Single 5ft",
            "T5 Triple 2ft",
            "T5 Triple 4ft",
            "T5 Triple 5ft",
            "T5 Twin 2ft",
            "T5 Twin 4ft",
            "T5 Twin 5ft",
            "T8 Quad 2ft",
            "T8 Quad 4ft",
            "T8 Single 4ft",
            "T8 Single 5ft",
            "T8 Single 6ft",
            "T8 Twin 2ft",
            "T8 Twin 4ft",
            "T8 Twin 5ft",
            "T8 Twin 6ft", 
            "Tungsten 60w",
            "Other"
            );

        //echo sizeof($lightArray);

        //for each room, get quant wattage and hours of each light referenced by the room id

        echo sizeOf($rooms);
        echo "<br/>";
        for($i=0; $i<sizeof($rooms);$i++){

            //set empty arrays
            $size = 36;
            $lightQuantityCount = array_fill(0, $size, 0);
            $lightWattageCount = array_fill(0, $size, 0);
            $lightHoursCount = array_fill(0, $size, 0);

            //get all lighting data of particular room
            $query2 = "SELECT * FROM tbl_lightingData WHERE r_id = '$rooms[$i]'";
            $result2 = $d->query($query2);
            $row2= $result2->fetchArray(SQLITE3_ASSOC);

            //for each light type in the lightarray chechk if light type from query == light type in array, if so add quantity, wattage and hours to arrays


            while($row2=$result2->fetchArray(SQLITE3_ASSOC)){

                echo "<br/>Room ID: ".$rooms[$i]."<br/>";
                $lightID = $row2['l_id'];
                $lightType = $row2['lightType'];
                $lightQuantity = $row2['lightQuantity'];
                $lightWattage = $row2['lightWattage'];
                $lightHours = $row2['lightHours'];
                echo "Light Profile:<br/>Light ID:  ".$lightID."<br/>Light Type:  ".$lightType."<br/>Light Wattage:  ".$lightWattage."<br/>Light Hours:  ".$lightHours."<br/>";

                for($j=0; $j<sizeof($lightArray); $j++){
                    if($lightType == $lightArray[$j]){
                        echo "<br/>MATCH<br/>";
                        $lightQuantityCount[$j] = $lightQuantityCount[$j] + $lightQuantity;
                        $lightWattageCount[$j] = $lightWattage;
                        $lightHoursCount[$j] = $lightHoursCount[$j] + $lightHours;
                    }
                    else{
                        echo "<br/>NO MATCH<br/>";
                    }
                }
            }

            //echo"<br/>ARRAY DUMP START:<br/>";
            //var_dump($lightQuantityCount);
            //echo"<br/>ARRAY DUMP END:<br/><br/>";

            echo "<br/>ADFSDFADFA : ".sizeof($lightQuantityCount);
            echo "<br/>ADFSDFADFA : ";

            for($k=0; $k<sizeof($lightQuantityCount); $k++){

                if($lightQuantityCount[$k] != 0){

                    echo "<br/>Light Type: ".$lightArray[$k]."<br/>";
                    echo "Light Quantity Count: ".$lightQuantityCount[$k]."<br/>";
                    echo "Wattage Count: ".$lightWattageCount[$k]."<br/>";
                    echo "Hours Count: ".$lightQuantityCount[$k]."<br/>";

                    include("setText.php");$status[2]++;

                    $groupStart = $status[1].$status[2];
                        $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], $lightArray[$k]);
                        $status[1]++;
                        $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], $lightQuantityCount[$k]);
                        $status[1]++;

                        $calc = $lightWattageCount[$k] * $lightQuantityCount[$k];
                        $calc2 = $calc / 1000;

                        $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], $calc2);
                    $groupEnd = $status[1].$status[2];

                    include("setHeadersText.php");groupFill($objPHPExcel, $groupStart, $groupEnd, "calc");
                    $status[2]++;$status[1] = chr(65);
                }
            }
        }

return $status;

}

评论获取更多信息,谢谢

克里斯

4

1 回答 1

0

再花几个小时后问题就解决了(代码中的头部),我已经设法克服了操作我存储在数据库中的数据的困惑。我的问题是我跳入了太多的 for 循环,我应该做加入之前的具体策略。

因此,下面的代码获取建筑物及其房间,并计算建筑物房间内的所有灯光。我希望这对那些遇到数据库问题以及将数据提取/操作为其他格式(如 excel)的人有所帮助。这就是堆栈溢出的正确性。这是代码:

//prints list of lights used in building with kwh/year and savings

函数 getLights($buildingID, $d, $objPHPExcel, $status){

$objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], 'Lighting Information: ');
include("setText.php");$status[2]++;

$groupStart = $status[1].$status[2];
    $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], 'Lights Used');
    $status[1]++;
    $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], 'Count');
    $status[1]++;
    $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], 'kWh/yr');
    $status[1]++;
    $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], 'Energy Saving kWh/yr');
    $status[1]++;
    $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], 'Saving(£)');
    $status[1]++;
    $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], 'Cost');
    $status[1]++;
    $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], 'Change To...');    
$groupEnd = $status[1].$status[2];

include("setHeadersText.php");groupFill($objPHPExcel, $groupStart, $groupEnd, "calc");
$status[2]++;$status[1] = chr(65);

$query = "SELECT r_id FROM tbl_roomDesc WHERE b_id = '$buildingID'";
$result = $d->query($query);
$rooms = array();

while($row = $result->fetchArray(SQLITE3_ASSOC)){ 
    array_push($rooms, $row['r_id']);
}

$lightArray = array(
"CFL",
"CFL Equivalent Halogen 100w spot",
"CFL Equivalent Halogen 50w spot",
"Double d",
"Halogen 100w spot",
"Halogen 50w spot",
"PL",
"T12 Single 4ft",
"T12 Single 5ft",
"T12 Single 6ft",
"T12 Single 8ft",
"T12 Twin 4ft",
"T12 Twin 5ft",
"T12 Twin 6ft",
"T12 Twin 8ft",
"T5 Quad 2ft",
"T5 Quad 4ft",
"T5 Single 4ft",
"T5 Single 5ft",
"T5 Triple 2ft",
"T5 Triple 4ft",
"T5 Triple 5ft",
"T5 Twin 2ft",
"T5 Twin 4ft",
"T5 Twin 5ft",
"T8 Quad 2ft",
"T8 Quad 4ft",
"T8 Single 4ft",
"T8 Single 5ft",
"T8 Single 6ft",
"T8 Twin 2ft",
"T8 Twin 4ft",
"T8 Twin 5ft",
"T8 Twin 6ft", 
"Tungsten 60w",
"Other"
);

$size = 36;
$lightQuantityCount = array_fill(0, $size, 0);
$lightWattageCount = array_fill(0, $size, 0);
$lightHoursCount = array_fill(0, $size, 0);

    $id_nums = implode(", ", $rooms);

    $query2 = "SELECT * FROM tbl_lightingData WHERE r_id IN ($id_nums)";
    $result2 = $d->query($query2);

    while($row2=$result2->fetchArray(SQLITE3_ASSOC)){

        $lightID = $row2['l_id'];
        $roomID = $row2['r_id'];
        $lightType = $row2['lightType'];
        $lightQuantity = $row2['lightQuantity'];
        $lightWattage = $row2['lightWattage'];
        $lightHours = $row2['lightHours'];

    for($a=0; $a<sizeOf($lightArray); $a++){

        if($lightType == $lightArray[$a]){
            $lightQuantityCount[$a] = $lightQuantityCount[$a] + $lightQuantity;
            $lightWattageCount[$a] = $lightWattage;
            $lightHoursCount[$a] = $lightHours;
        }
    }
}

for($b=0; $b<sizeOf($lightArray); $b++){

    $groupStart = $status[1].$status[2];
        $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], $lightArray[$b]);
        $status[1]++;
        $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], $lightQuantityCount[$b]);
        $status[1]++;

        $calc = $lightWattageCount[$b] * $lightQuantityCount[$b];
        $calc2 = $calc / 1000;

        $objPHPExcel->getActiveSheet()->setCellValue($status[1].$status[2], $calc2);
    $groupEnd = $status[1].$status[2];

    $status[2]++;$status[1] = chr(65);
}

return $status;

}

有用的链接:

将数组内爆以在 sql 中形成查询:内爆数组以用作 sql 查询

PHP Excel,非常适合 PHP->EXCEL 转换:

链接到 PHP Excel

以及来自 php 手册的旧信任数组:

ARRAY 手册 PHP

这是我的代码输出的图像:

上面代码的输出图像

关于这个问题的任何问题,评论!

于 2012-11-07T22:10:23.473 回答