Php重复循环获取显示

时间:2015-10-28 11:48:03

标签: php mysql loops

我正在使用php。我用我的桌子取了两个数组。我不明白为什么它会重复表格,但它能够显示其他查询的值。

这里是整个代码:

    $select_department2 = mysql_query("SELECT DISTINCT work.`Department` FROM `work` INNER JOIN employee_leaves ON employee_leaves.ID_No = work.ID_No WHERE MONTH(employee_leaves.Date_Approved) LIKE '$month' AND Leave_Type LIKE 'Sick' AND Pay_Type LIKE 'Paid'");

while($row_department2 = mysql_fetch_array($select_department2)) 
{
   $dept = $row_department2['Department'];
}
   echo "<table border=1em>";
    echo "<tr>";
        echo "<td>Department</td>";
        echo "<td>Name</td>";
        echo "<td>Position</td>";
        echo "<td>SL Balance</td>";
        echo "<td>SL Retain</td>";
        echo "<td>SL Convertible</td>";
    echo "</tr>";

while ($row_id2 = mysql_fetch_array($select_id2)) {       

   $select_record2 = mysql_query("SELECT DISTINCT employee_leaves.ID_No as leaveID, First_Name,Last_Name, Person_Type, Leave_Type, Special_Type, Position, Subject_Type, SUM(DATEDIFF(Leave_End,Leave_Start) + 1) as Total_Days
                                      FROM employee_leaves, person_details, work, person
                                      WHERE person.ID_No LIKE employee_leaves.ID_No AND person_details.ID_No LIKE employee_leaves.ID_No AND work.ID_No LIKE employee_leaves.ID_No AND (Leave_Status LIKE 'Approved' OR Leave_Status LIKE 'Extended') AND Leave_Type LIKE 'Sick' AND Pay_Type LIKE 'Paid' AND MONTH(Date_Approved) LIKE '$month' AND Department='".$row_department2['Department']."' AND employee_leaves.ID_No= '".$row_id2['leavesID_No']."'    ");

   $queryAllEmployees = mysql_query("SELECT person_details.ID_No as personID, Last_Name, First_Name, Position, Subject_Type, Department, Person_Type
                                    FROM person_details, work, person
                                    WHERE person_details.ID_No LIKE work.ID_No AND person.ID_No LIKE work.ID_No AND Department LIKE 'Humanities' AND Position != 'Academic Coordinator'
                                    ORDER BY Last_Name");

这是我回应两个查询中的值的部分:

$arrayRowA = array();
        $arrayRowB = array();
        while($row = mysql_fetch_array($select_record2)){$arrayRowA[] = $row;}
        while($row = mysql_fetch_array($queryAllEmployees)){$arrayRowB[] = $row;}


        // Loop through two arrays in a square way (every combination of both arrays)
        foreach($arrayRowA as $keyA => $objectA){
            foreach($arrayRowB as $keyB => $objectB){
                $leaveID        = $objectA['leaveID'];
                $Sick_First_Name = $objectA['First_Name'];
                $Sick_Last_Name = $objectA['Last_Name'];
                $Sick_Position = $objectA['Position'];
                $Sick_Subject_Type = $objectA['Subject_Type'];
                $Sick_Leave_Type = $objectA['Leave_Type'];
                $Sick_Special_Type = $objectA['Special_Type'];
                $Sick_Total_Days = $objectA['Total_Days'];
                $Sick_Person_Type = $objectA['Person_Type'];

                $personID       = $objectB['personID'];
                $Person_Type    = $objectB['Person_Type'];
                $First_Name = $objectB['First_Name'];
                $Last_Name = $objectB['Last_Name'];

                $Department = $objectB['Department'];
                $Position = $objectB['Position'];
                $Subject_Type = $objectB['Subject_Type'];
                $Person_Type = $objectB['Person_Type'];


                if ($Person_Type == 'Regular') 
                  {
                    $Sick_Allowable_Days = 10;
                    $Sick_Remaining_Days = 10 - $Sick_Total_Days;
                  }else{
                    $Sick_Allowable_Days = 5;
                    $Sick_Remaining_Days = 5 - $Sick_Total_Days;
                  }    

                echo "<tr>";
                    echo "<td>$Department</td>";
                    echo "<td>$Last_Name, $First_Name</td>";
                    echo "<td>$Subject_Type $Position</td>";
                    echo "<td>$Sick_Allowable_Days</td>";
                    echo ($leaveID == $personID) ? "<td>$Sick_Total_Days</td>" : "<td>--</td>";
                    echo ($leaveID == $personID) ? "<td>$Sick_Remaining_Days</td>" : "<td>--</td>";
                echo "</tr>";
            }
        }

1 个答案:

答案 0 :(得分:1)

我想你应该试试这个:

$dept = array();
while($row_department2 = mysql_fetch_array($select_department2)) 
{
   $dept[] = $row_department2['Department'];
}

然后在下一个循环中使用

    while ($row_id2 = mysql_fetch_array($select_id2)) {       

   $select_record2 = mysql_query("SELECT DISTINCT employee_leaves.ID_No as leaveID, First_Name,Last_Name, Person_Type, Leave_Type, Special_Type, Position, Subject_Type, SUM(DATEDIFF(Leave_End,Leave_Start) + 1) as Total_Days
                                      FROM employee_leaves, person_details, work, person
                                      WHERE person.ID_No LIKE employee_leaves.ID_No AND person_details.ID_No LIKE employee_leaves.ID_No AND work.ID_No LIKE employee_leaves.ID_No AND (Leave_Status LIKE 'Approved' OR Leave_Status LIKE 'Extended') AND Leave_Type LIKE 'Sick' AND Pay_Type LIKE 'Paid' AND MONTH(Date_Approved) LIKE '$month' AND Department='".$dept[$i]."' AND employee_leaves.ID_No= '".$row_id2['leavesID_No']."'    ");

   $queryAllEmployees = mysql_query("SELECT person_details.ID_No as personID, Last_Name, First_Name, Position, Subject_Type, Department, Person_Type
                                    FROM person_details, work, person
                                    WHERE person_details.ID_No LIKE work.ID_No AND person.ID_No LIKE work.ID_No AND Department LIKE 'Humanities' AND Position != 'Academic Coordinator'
                                    ORDER BY Last_Name"); 
                                    $i++;