2

I'm using php. I'm fetching two arrays with my table. I don't understand why it duplicates the table but it was able to display the values from the other query.

Here's the whole code:

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

Here is the part where I echo the values from both queries:

$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 Answer 1

1

I think you should try this:

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

Then in the next loop use

    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++;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.