我的 PHP 行数组在我的 table 中给了我重复项,我不知道为什么
My PHP row array is giving me duplicates in my table, and I don't know why
大家。
我正在尝试对三个表进行联接。这是完成此操作的代码:
$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>";
}
}
}
我的问题是我得到了重复的行。行得出完全相同的数据。这不是预期的行为。预期的行为是每一行都有不同之处。 PT_LName/FName 应该不同,日期应该不同,或者 Activity_Type 应该不同。对于任何两行,它们不应该都相同,但我得到了每行的副本。而且重复的数量似乎有所不同。
这是我第一次在这里发帖,所以如果您需要任何其他信息,请告诉我。我对此很陌生。 :)
编辑添加:我找到了解决方案,基于我得到的慷慨帮助。 :)
我将查询更改为:
$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>";
总而言之,这两项更改为我提供了正确数量的行。我在 key/Date 配对上做错了,让连接变得太复杂了。
我怀疑您看到您认为是重复行的原因是因为您在 SELECT 子句中包含 ID 和 PT_Last_Name 但从未在输出中使用它。
您可能想顺便拜访 https://codereview.stackexchange.com/ 并寻求一些关于更好地构建代码的建议。
基于我得到的慷慨帮助,我找到了解决方案。 :)
我将查询更改为:
$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>";
总而言之,这两项更改为我提供了正确数量的行。我在 key/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>";
}
}
}
我的问题是我得到了重复的行。行得出完全相同的数据。这不是预期的行为。预期的行为是每一行都有不同之处。 PT_LName/FName 应该不同,日期应该不同,或者 Activity_Type 应该不同。对于任何两行,它们不应该都相同,但我得到了每行的副本。而且重复的数量似乎有所不同。
这是我第一次在这里发帖,所以如果您需要任何其他信息,请告诉我。我对此很陌生。 :)
编辑添加:我找到了解决方案,基于我得到的慷慨帮助。 :)
我将查询更改为:
$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>";
总而言之,这两项更改为我提供了正确数量的行。我在 key/Date 配对上做错了,让连接变得太复杂了。
我怀疑您看到您认为是重复行的原因是因为您在 SELECT 子句中包含 ID 和 PT_Last_Name 但从未在输出中使用它。
您可能想顺便拜访 https://codereview.stackexchange.com/ 并寻求一些关于更好地构建代码的建议。
基于我得到的慷慨帮助,我找到了解决方案。 :)
我将查询更改为:
$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>";
总而言之,这两项更改为我提供了正确数量的行。我在 key/Date 配对上做错了,让连接变得太复杂了。