1

我有一个看起来像这样的 .csv 文件...

     Name            AC-No.      Time                State        Exception   Operation

Johnny Starks Depp   1220    4/12/2013 12:45:18 AM  Check In        OK   
Johnny Starks Depp   1220    4/12/2013 5:46:58 AM   Out             Out 
Johnny Starks Depp   1220    4/12/2013 6:22:41 AM   Out Back        Out  
Johnny Starks Depp   1220    4/12/2013 10:42:17 AM  Check Out       Repeat   
Johnny Starks Depp   1220    4/12/2013 10:42:19 AM  Check Out       OK

我已经可以把它上传到我的数据库了。我的问题是,它显示在表格中的结果。在 mysql 数据库中,我有一个包含列(Name、ACNo.、Date、CheckIn、Breakout、Breakin、CheckOut)的 TABLE。我想要发生的事情是我做错了.. .csv 文件中具有相同DATE的记录(显示在时间列中)将与签入、中断、表中的 BreakIn 和 CheckOut 不会产生多条记录。像这样..

     Name         |ACNo |     Date  | CheckIn     |  Breakout  |   Breakin  | Checkout
                  |     |           |             |            |            |
Johnny Starks Depp|1220 | 4/12/2013 | 12:45:18 AM | 5:46:58 AM | 6:22:41 AM | 10:42:19 AM

现在..我已经做了上面我想在表格中发生的示例但是...当我检查表格时,是的,它每行有不同的日期,但是(签入,突破,突破,结账)中的时间是错误的. 错误,因为例如 DATE 4/12/2013 中的时间记录在 DATE 4/13/2013 中。我使用的代码有什么问题?或者我还有什么办法可以做到这一点?这些是我的代码:

<?php

$conn = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("dtrlogs",$conn);

$datehere ="";

if(isset($_POST['Submit']))
{

$file = $_FILES['file']['tmp_name'];

if($file == "")
{
    ?>
    <script type="text/javascript">
    alert('No File Selected!');
    </script>
    <?php
}

else
{
$handle = fopen($file,"r");
while(($fileop = fgetcsv($handle,1000,";")) !==false)
{
    $Name = $fileop[0];
    $ACNo = $fileop[1];
    $Time = $fileop[2];
    $State = $fileop[3];
    $NewState = $fileop[4];
    $Exception = $fileop[5];
    $Operation = $fileop[6];

    //i separated time/date in the Column TIME in the .csv file
    $date = date('m/d/Y', strtotime($Time));
    $hours = date('H:i:s A', strtotime($Time));


    //this is what i used to prevent multiple dates
    if($datehere != $date) {

        $datehere = $date;
    }
    else{

        $date = "";
    }







    //this is to insert the name and acno to the other table. 
    //(this is for the lists of employee)

    $sql=mysql_query("SELECT * FROM employee Where ACNo = '$ACNo'");
    $row=mysql_fetch_array($sql);
    if($row == 0) {

       $sql1 = mysql_query("INSERT INTO employee(ACNo, Name) VALUES('$ACNo','$Name')");
    }



    //Inserts record if Date is not yet recorded and if already has just updates 
    //the row's column (Checkin, breakout, breakin and Checkout) 
    //with its correct time according to the date

    $query = mysql_query("SELECT * FROM dtrs Where ACNo = '$ACNo' AND Date = '$date'");
    $rows = mysql_fetch_array($query);
    if($rows == 0) {

    $sql2 = mysql_query("INSERT INTO dtrs(Name, ACNo, Date, CheckIn, BreakOut, 
    BreakIn,CheckOut) 
    VALUES('$Name','$ACNo','$date','$CheckIn','$Breakout','$Breakin','$Checkout')");

    }
    else{

    $sql2 = mysql_query("Update dtrs Set CheckIn = '$CheckIn', BreakOut = 
    '$Breakout', BreakIn = '$Breakin', CheckOut = '$Checkout'");
    }







    //this is my conditions to identify whether the TIME/HOUR is 
    //Stated as CheckIn, Breakout, Breakin, or Checkout

    if($NewState == "Check In" || $State == 'Check In' && $Exception == "OK") {

        if($Exception == "Invalid" || $Exception == "Repeat")
        {}
        else {
            $CheckIn = $hours;
        }
    }
    if($State == 'Out' && $Exception == "Out") {

        if($Exception == "Invalid" || $Exception == "Repeat")
        {}
        else {
            $Breakout = $hours;
        }
    }
    if($State == 'Out Back' && $Exception == "Out") {

        if($Exception == "Invalid" || $Exception == "Repeat")
        {}
        else {
            $Breakin = $hours;
        }
    }
    if($NewState == "Check Out" || $State == "Check Out" && $Exception == "OK") {

        if($NewState == "Check In")
        {}
        else {
            $Checkout = $hours;
        }
     }


}


if($sql2)
{
if($sql2)
{


?>
    <script type="text/javascript">
    alert('File Upload Successful!');
    </script>
<?php
}
}
}

}

?>
4

1 回答 1

0

我建议可能加载到临时表,并从中提取详细信息以用于填充您的真实表。

例如,如果您将 CSV 文件加载到名为 LoadTable 的表(不是临时表,因为您只能在一段 SQL 中使用临时表一次),您可以提取不同的名称和帐号,然后将其加入子选择以获取每个相关事件的最大事件时间。

像这样的东西: -

SELECT DerivMain.Name, 
    DerivMain.ACNo, 
    DerivMain.EventDate, 
    DATE_FORMAT(DerivCheckIn.EventDateTime, '%l:%i:%s %p'), 
    DATE_FORMAT(DerivBreakout.EventDateTime, '%l:%i:%s %p'),  
    DATE_FORMAT(DerivBreakin.EventDateTime, '%l:%i:%s %p'), 
    DATE_FORMAT(DerivCheckout.EventDateTime, '%l:%i:%s %p')
FROM (SELECT DISTINCT Name, ACNo, DATE(STR_TO_DATE(Date,'%c/%e/%Y %l:%i:%s %p')) As EventDate FROM LoadTable) DerivMain
LEFT OUTER JOIN (SELECT Name, ACNo, MAX(STR_TO_DATE(Date,'%c/%e/%Y %l:%i:%s %p') AS EventDateTime) FROM LoadTable WHERE State = 'Check In' GROUP BY Name, ACNo) DerivCheckIn
ON DerivMain.Name = DerivCheckIn Name AND DerivMain.ACNo = DerivCheckIn.ACNo 
LEFT OUTER JOIN (SELECT Name, ACNo, MAX(STR_TO_DATE(Date,'%c/%e/%Y %l:%i:%s %p') AS EventDateTime) FROM LoadTable WHERE State = 'Out' GROUP BY Name, ACNo) DerivBreakout
ON DerivMain.Name = DerivBreakout Name AND DerivMain.ACNo = DerivBreakout.ACNo 
LEFT OUTER JOIN (SELECT Name, ACNo, MAX(STR_TO_DATE(Date,'%c/%e/%Y %l:%i:%s %p') AS EventDateTime) FROM LoadTable WHERE State = 'Out Back' GROUP BY Name, ACNo) DerivBreakin
ON DerivMain.Name = DerivBreakin Name AND DerivMain.ACNo = DerivBreakin.ACNo 
LEFT OUTER JOIN (SELECT Name, ACNo, MAX(STR_TO_DATE(Date,'%c/%e/%Y %l:%i:%s %p') AS EventDateTime) FROM LoadTable WHERE State = 'Check Out' GROUP BY Name, ACNo) DerivCheckout
ON DerivMain.Name = DerivCheckout Name AND DerivMain.ACNo = DerivCheckout.ACNo 
于 2013-05-24T11:41:03.477 回答