我的PHP行数组在我的表中给了我重复项,而我没有';我不知道为什么
各位 我想在三张桌子上做一个连接。下面是完成此任务的代码:我的PHP行数组在我的表中给了我重复项,而我没有';我不知道为什么,php,for-loop,key-pair,Php,For Loop,Key Pair,各位 我想在三张桌子上做一个连接。下面是完成此任务的代码: $query = "SELECT Distinct ID, Date, SC_LName, SC_FName, Activity_Type, Hours, Pay_Rate, PT_First_Name, PT_Last_Name FROM Payroll LEFT OUTER JOIN Plans ON Plans.Client_ID = Payroll.Client_ID GROUP BY STR_TO_DATE( DATE, '
$query = "SELECT Distinct ID, Date, SC_LName, SC_FName, Activity_Type, Hours, Pay_Rate, PT_First_Name, PT_Last_Name
FROM Payroll
LEFT OUTER JOIN Plans ON Plans.Client_ID = Payroll.Client_ID
GROUP BY STR_TO_DATE( DATE, '%m/%d/%Y' ), Payroll.SC_LName";
$res = mysqli_query($con, $query);
查询之后,我在while循环中有一个for循环,如下所示:
while ($row = mysqli_fetch_assoc($res)) {
$daterange = $row['Approval_Date'];
$m30 = date('m/d/Y',strtotime('+ 30 days', strtotime($daterange)));
$m60 = date('m/d/Y',strtotime('+ 60 days', strtotime($daterange)));
$m90 = date('m/d/Y',strtotime('+ 90 days', strtotime($daterange)));
$m120 = date('m/d/Y',strtotime('+ 120 days', strtotime($daterange)));
$m150 = date('m/d/Y',strtotime('+ 150 days', strtotime($daterange)));
$m180 = date('m/d/Y',strtotime('+ 190 days', strtotime($daterange)));
$m210 = date('m/d/Y',strtotime('+ 210 days', strtotime($daterange)));
$m240 = date('m/d/Y',strtotime('+ 240 days', strtotime($daterange)));
$m270 = date('m/d/Y',strtotime('+ 270 days', strtotime($daterange)));
$m300 = date('m/d/Y',strtotime('+ 300 days', strtotime($daterange)));
$m330 = date('m/d/Y',strtotime('+ 330 days', strtotime($daterange)));
$m360 = date('m/d/Y',strtotime('+ 360 days', strtotime($daterange)));
$Hourly_Pay = round($row['Pay_Rate'] * $row['Hours'],2);
asort($row);
foreach($row as $key => $val) {
$time = strtotime($val);
$newformat = date('Y-m-d',$time);
$myformat = date('Y-m-d', strtotime($row['Date']));
if ( $time > strtotime("01/28/2015")
AND $time < strtotime("03/31/2015")
AND $key <> "Date"
AND $myformat = $newformat) {
echo "<tr><td>" . $row['Date'];
echo "</td><td>";
if (strtotime($row['Date']) < strtotime($m30)) {
echo "30 Day";
};
if (strtotime($row['Date']) > strtotime($m60)
AND strtotime($row['Date']) < strtotime($m90)) {
echo "60 Day";
};
if (strtotime($row['Date']) > strtotime($m90)
AND strtotime($row['Date']) < strtotime($m120)) {
echo "90-Day";
};
if (strtotime($row['Date']) > strtotime($m120)
AND strtotime($row['Date']) < strtotime($m150)) {
echo "210 Day";
};
if (strtotime($row['Date']) > strtotime($m150)
AND strtotime($row['Date']) < strtotime($m180)) {
echo "150-Day";
};
if (strtotime($row['Date']) > strtotime($m180)
AND strtotime($row['Date']) < strtotime($m210)) {
echo "180-Day";
};
if (strtotime($row['Date']) > strtotime($m210)
AND strtotime($row['Date']) < strtotime($m240)) {
echo "210 Day";
};
echo "</td><td>" . $row['SC_LName'] . "</td><td>"
. $row['SC_FName'] . "</td><td>" . $row['Title']
. "</td><td>" . $row['PT_Last_Name'] . "</td><td>"
. $row['PT_First_Name'] . "</td><td>"
. $row['Activity_Type'] . "</td><td>"
. $row['Hours'] . "</td><td>"
. $row['Pay_Rate'] . "</td><td>"
. $Hourly_Pay . "</td><td>"
. $row['Budget_Amt'] . "</td><td>"
. $row['Notes'] . "</td></tr>";
}
}
}
我将我的IF语句改为:
if ( $time > strtotime("01/28/2015") AND $time < strtotime("03/31/2015") AND $key == "Date" AND $myformat = $newformat) {echo "<tr><td>" . $row['Date']; echo "</td><td>"; if (strtotime($row['Date']) < strtotime($m30)) {echo "30 Day";}; if (strtotime($row['Date']) > strtotime($m60) AND strtotime($row['Date']) < strtotime($m90)) {echo "60 Day";}; if (strtotime($row['Date']) > strtotime($m90) AND strtotime($row['Date']) < strtotime($m120)) {echo "90-Day";}; if (strtotime($row['Date']) > strtotime($m120) AND strtotime($row['Date']) < strtotime($m150)) {echo "210 Day";}; if (strtotime($row['Date']) > strtotime($m150) AND strtotime($row['Date']) < strtotime($m180)) {echo "150-Day";}; if (strtotime($row['Date']) > strtotime($m180) AND strtotime($row['Date']) < strtotime($m210)) {echo "180-Day";}; if (strtotime($row['Date']) > strtotime($m210) AND strtotime($row['Date']) < strtotime($m240)) {echo "210 Day";}; echo "</td><td>" . $row['SC_LName'] . "</td><td>" . $row['SC_FName'] . "</td><td>" . $row['PT_Last_Name'] . "</td><td>" . $row['PT_First_Name'] . "</td><td>" . $row['Activity_Type'] . "</td><td>" . $row['Hours'] . "</td><td>" . $row['Pay_Rate'] . "</td><td>" . $Hourly_Pay . "</td><td>" . $row['Budget_Amt'] . "</td><td>" . $row['Notes'] . "</td></tr>";
if ( $time > strtotime("01/28/2015") AND $time < strtotime("03/31/2015") AND $key == "Date" AND $myformat = $newformat) {echo "<tr><td>" . $row['Date']; echo "</td><td>"; if (strtotime($row['Date']) < strtotime($m30)) {echo "30 Day";}; if (strtotime($row['Date']) > strtotime($m60) AND strtotime($row['Date']) < strtotime($m90)) {echo "60 Day";}; if (strtotime($row['Date']) > strtotime($m90) AND strtotime($row['Date']) < strtotime($m120)) {echo "90-Day";}; if (strtotime($row['Date']) > strtotime($m120) AND strtotime($row['Date']) < strtotime($m150)) {echo "210 Day";}; if (strtotime($row['Date']) > strtotime($m150) AND strtotime($row['Date']) < strtotime($m180)) {echo "150-Day";}; if (strtotime($row['Date']) > strtotime($m180) AND strtotime($row['Date']) < strtotime($m210)) {echo "180-Day";}; if (strtotime($row['Date']) > strtotime($m210) AND strtotime($row['Date']) < strtotime($m240)) {echo "210 Day";}; echo "</td><td>" . $row['SC_LName'] . "</td><td>" . $row['SC_FName'] . "</td><td>" . $row['PT_Last_Name'] . "</td><td>" . $row['PT_First_Name'] . "</td><td>" . $row['Activity_Type'] . "</td><td>" . $row['Hours'] . "</td><td>" . $row['Pay_Rate'] . "</td><td>" . $Hourly_Pay . "</td><td>" . $row['Budget_Amt'] . "</td><td>" . $row['Notes'] . "</td></tr>";
if($time>strottime(“01/28/2015”)和$timestrottime($m60)和strottime($row['Date m90)){echo“60天”;};if(strotime($row['Date'])>strotime($m90)和strotime($row['Date'])strotime($m120)和strotime($row['Date'])strotime($m180)和strotime($row['Date'])
总而言之,这两个更改为我提供了正确的行数。我对键/日期配对做了一些错误,并使连接变得过于复杂。我怀疑您看到重复行的原因是因为您在SELECT子句中包含ID和PT_Last_Name,但从未在输出中使用它
您可能想顺道来看看,征求一些关于如何更好地构建代码的建议。基于我在这里得到的慷慨帮助,我找到了解决方案。:) 我将查询更改为:
$query = "SELECT * from Plans
LEFT OUTER JOIN Payroll ON Payroll.Client_ID = Plans.Client_ID";
$query = "SELECT * from Plans
LEFT OUTER JOIN Payroll ON Payroll.Client_ID = Plans.Client_ID";
我将我的IF语句改为:
if ( $time > strtotime("01/28/2015") AND $time < strtotime("03/31/2015") AND $key == "Date" AND $myformat = $newformat) {echo "<tr><td>" . $row['Date']; echo "</td><td>"; if (strtotime($row['Date']) < strtotime($m30)) {echo "30 Day";}; if (strtotime($row['Date']) > strtotime($m60) AND strtotime($row['Date']) < strtotime($m90)) {echo "60 Day";}; if (strtotime($row['Date']) > strtotime($m90) AND strtotime($row['Date']) < strtotime($m120)) {echo "90-Day";}; if (strtotime($row['Date']) > strtotime($m120) AND strtotime($row['Date']) < strtotime($m150)) {echo "210 Day";}; if (strtotime($row['Date']) > strtotime($m150) AND strtotime($row['Date']) < strtotime($m180)) {echo "150-Day";}; if (strtotime($row['Date']) > strtotime($m180) AND strtotime($row['Date']) < strtotime($m210)) {echo "180-Day";}; if (strtotime($row['Date']) > strtotime($m210) AND strtotime($row['Date']) < strtotime($m240)) {echo "210 Day";}; echo "</td><td>" . $row['SC_LName'] . "</td><td>" . $row['SC_FName'] . "</td><td>" . $row['PT_Last_Name'] . "</td><td>" . $row['PT_First_Name'] . "</td><td>" . $row['Activity_Type'] . "</td><td>" . $row['Hours'] . "</td><td>" . $row['Pay_Rate'] . "</td><td>" . $Hourly_Pay . "</td><td>" . $row['Budget_Amt'] . "</td><td>" . $row['Notes'] . "</td></tr>";
if ( $time > strtotime("01/28/2015") AND $time < strtotime("03/31/2015") AND $key == "Date" AND $myformat = $newformat) {echo "<tr><td>" . $row['Date']; echo "</td><td>"; if (strtotime($row['Date']) < strtotime($m30)) {echo "30 Day";}; if (strtotime($row['Date']) > strtotime($m60) AND strtotime($row['Date']) < strtotime($m90)) {echo "60 Day";}; if (strtotime($row['Date']) > strtotime($m90) AND strtotime($row['Date']) < strtotime($m120)) {echo "90-Day";}; if (strtotime($row['Date']) > strtotime($m120) AND strtotime($row['Date']) < strtotime($m150)) {echo "210 Day";}; if (strtotime($row['Date']) > strtotime($m150) AND strtotime($row['Date']) < strtotime($m180)) {echo "150-Day";}; if (strtotime($row['Date']) > strtotime($m180) AND strtotime($row['Date']) < strtotime($m210)) {echo "180-Day";}; if (strtotime($row['Date']) > strtotime($m210) AND strtotime($row['Date']) < strtotime($m240)) {echo "210 Day";}; echo "</td><td>" . $row['SC_LName'] . "</td><td>" . $row['SC_FName'] . "</td><td>" . $row['PT_Last_Name'] . "</td><td>" . $row['PT_First_Name'] . "</td><td>" . $row['Activity_Type'] . "</td><td>" . $row['Hours'] . "</td><td>" . $row['Pay_Rate'] . "</td><td>" . $Hourly_Pay . "</td><td>" . $row['Budget_Amt'] . "</td><td>" . $row['Notes'] . "</td></tr>";
if($time>strottime(“01/28/2015”)和$timestrottime($m60)和strottime($row['Date m90)){echo“60天”;};if(strotime($row['Date'])>strotime($m90)和strotime($row['Date'])strotime($m120)和strotime($row['Date'])strotime($m180)和strotime($row['Date'])
总而言之,这两个更改为我提供了正确的行数。我在键/日期配对方面做了一些错误,使连接变得过于复杂。你确定所有日期都是相同的吗?你可能会在小时/分钟/秒上失去一些时间精度。谢谢你的回复,Clement.)日期字段仅限于当天,因此我不认为小时、分钟或秒会导致重复。首先,确保这不是数据库查询的问题。CBroe:我在phpmyadmin中运行了查询,并且得到了合理的结果。没有重复。很难说,但我建议您创建一个新的测试页nd将您的PHP简化为一个或两个条件语句。原因显然在那里,而不是查询本身。一旦确定了原因,您就可以慢慢地开始添加其他条件语句。我会尝试将
foreach
置于while
循环之外,看看会发生什么。祝您好运。谢谢。:)我实际上重写了查询和一些代码。我认为它现在可以工作了。我需要更新或关闭它或其他什么。不过,我会发布解决方案,供其他人查看。