- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个看起来像这样的 .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(显示在 TIME 列中)的记录将位于同一行使用 TABLE 中的 Check In、BreakOut、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
现在..我已经做了上面我想在表格中发生的事情的例子但是......当我检查表格时,是的,它每行都有不同的日期但是时间(CheckIn,Breakout,Breakin,Checkout ) 错了。错误,因为例如 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
}
}
}
}
?>
最佳答案
我建议可能加载到临时表,并从中提取详细信息以用于填充您的真实表。
例如,如果您将 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
关于php - 上传 CSV 文件,PHP/MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16728244/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!