我的PHP行数组在我的表中给了我重复,我不知道为什么

时间:2015-03-20 12:51:25

标签: 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,  '%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>";

总之,这两个变化给了我正确的行数。我在使用键/日期配对时遇到了问题,并使连接过于复杂。

2 个答案:

答案 0 :(得分:0)

我怀疑你看到你认为重复行的原因是因为你在SELECT子句中包含ID和PT_Last_Name但从不在输出中使用它。

您可能希望删除https://codereview.stackexchange.com/并就如何更好地构建代码提出一些建议。

答案 1 :(得分:0)

我找到了解决方案,基于我来到这里的慷慨帮助。 :)

我将查询更改为:

$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>";

总之,这两个变化给了我正确的行数。我在使用键/日期配对时遇到了问题,并使连接过于复杂。