-2

我在试图查明我遇到的问题时遇到了问题,下面的代码循环了两次并给了我一个输出,Match11Match111Match11Match111我真的不确定问题出在哪里,但它应该只循环一次。

<?php

function generateTimeCard($conn, $employeeID){

    // set current date
$date7 = date("m/d/y");

// parse about any English textual datetime description into a Unix timestamp
$ts = strtotime($date7);

// calculate the number of days since Monday
$dow = date('w', $ts);
$offset = $dow;
if ($offset < 0) {
    $offset = 6;
}

// calculate timestamp for the Monday
$ts = $ts - $offset*86400;

// loop from Monday till Sunday
for ($i = 0; $i < 7; $i++, $ts += 86400){

    $date1 = date("m/d/y", $ts);
    $date3 = date("l", $ts);
    $$date3 = $date1;
    $date2 = $date1;
    $day[$i] = $date1;

    $stmt = $conn->prepare('SELECT `employeeID`,`date`,`unitNumber`,`jobDescription`,`timeIn`,`timeOut`, `lunch`, TIMEDIFF(`timeOut`, `timeIn`)  
                            AS `totalTime` FROM `timeRecords` WHERE `date`= :day AND `employeeID`= :employeeID ORDER BY date,id;');

    $stmt->execute(array(':day'=>$day[$i], ':employeeID'=>$employeeID));    

    $dayOW = 1;

    while($row = $stmt->fetch()) {

           if ($row['lunch'] == "Yes"){
            echo "Match";
        } else{
            echo "1";
        }

        $dayCurrent = $date3 . "Hours." . $dayOW;
        $data[$dayCurrent] = $row['totalTime'];
        $timeDay = $date3 . "." . $dayOW;
        $unitNumber = $date3 . "UnitNumber." . $dayOW;
        $description = $date3 . "Description." . $dayOW;
        $data[$timeDay] = date("h:i A", strtotime($row['timeIn'])) . "/" . date("h:i A", strtotime($row['timeOut']));
        $data[$unitNumber] = $row['unitNumber'];
        $data[$description] = $row['jobDescription'];
        $dayOW++;
    }

    $stmt = $conn->prepare('SELECT `employeeID`, SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(`timeOut`, `timeIn`)))) 
                            AS `totalDay` FROM `timeRecords` WHERE   `date` = :day
                            AND `employeeID` = :employeeID GROUP BY `employeeID`;');
    $stmt->execute(array(':day'=>$date1, ':employeeID'=>$employeeID));
    $row = $stmt->fetch();
    $totalDay = "Total" . $date3;

    $data[$totalDay] = $row['totalDay'];

}
$data['Week']= $Sunday . " - " . $Saturday;
return $data;

}

?>

我的数据库看起来像这样

+----+------------+----------+--------+----------------------------+-------------+------------+-----------+---------+-------+
| id | employeeID | date     | timeIn | jobDescription             | equipType   | unitNumber | unitHours | timeOut | lunch |
+----+------------+----------+--------+----------------------------+-------------+------------+-----------+---------+-------+
|  1 |          1 | 01/20/13 | 6:00   | Worked in RockPort         | Excavator   | 01E        |      7238 | 17:13   | Yes   |
|  2 |          1 | 01/21/13 | 6:00   | Worked in Jefferson        | Excavator   | 01E        |      7238 | 17:17   |       |
|  3 |          1 | 01/22/13 | 6:00   | Worked in Jefferson        | Excavator   | 02E        |      7238 | 17:30   |       |
|  4 |          1 | 01/23/13 | 6:00   | Worked in Whispering Creek | Skid Loader | 32SL       |      2338 | 18:30   | Yes   |
|  5 |          1 | 01/24/13 | 8:00   | Worked in Hubbard          | Scraper     | 54C        |      9638 | 11:30   |       |
|  6 |          1 | 01/25/13 | 8:00   | Worked in Jefferson        | Dozer       | 4D         |       941 | 19:30   |       |
|  7 |          1 | 01/26/13 | 8:00   | Pushed Snow                | Loader      | 950H       |       342 | 20:30   |       |
+----+------------+----------+--------+----------------------------+-------------+------------+-----------+---------+-------+
4

1 回答 1

3

默认情况下,PDO 使用索引和列名键获取。所以有“重复”的价值。您希望将获取模式更改为 FETCH_NUM(仅索引键)($stmt->fetch(PDO::FETCH_NUM)),以便值不会出现在两个映射下。http://php.net/manual/en/pdostatement.fetch.php

于 2013-01-24T04:30:27.117 回答