使用PHP操作数据,将SQLite数据库转换为Excel电子表格

时间:2012-11-07 19:53:26

标签: php database excel sqlite data-manipulation

我有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;

}

评论更多信息,谢谢

克里斯

1 个答案:

答案 0 :(得分:0)

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

因此,下面的代码占用了建筑物,它的房间,并在建筑物的房间内运行所有灯光。我希望这对那些遇到数据库问题并将数据提取/操作成其他格式(例如excel)的人有用。这就是堆栈溢出的权利。这是代码:

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

function 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中执行数组以形成查询:Imploding an array to use as a sql query

PHP Excel,非常适合PHP-&gt; EXCEL转换:

Link to PHP Excel

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

ARRAY manual PHP

以下是我的代码输出的图像:

image of output of the code above

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