简体   繁体   中英

Merge rows if have the same id and same value or value is null in PHP

I'm using PHP programming and Microsoft Access database. Both connected using PDO.

I have problem merging column's values in the same table if the Employee ID's "1stHalf" or "2ndHalf" have value on the same month and year.

This is because in my situation, employee might pay once a month or twice a month. That is why I have 1st half and 2nd half column in my table.

But the trick is I only have 1 column called "EPFee" and it will be merge based on "1stHalf" or "2ndHalf" value whether that month is once a month or twice a month.

Table that I have now :

Employee ID | 1stHalf | 2ndHalf | Month | Year | EPFee    
1011        |  0      |   1     |  2    | 2017 | 15.00   
1011        |  1      |   0     |  2    | 2017 | 29.00

Output that I want :

Employee ID | 1stHalf | 2ndHalf | Month | Year | EPF1stFee | EPF2ndFee   
1011        |  1      |   1     |   2   | 2017 |   29.00   |   15.00

If 2ndHalf value is 0 on Month 2/2017 The output should be like this:

Employee ID | 1stHalf | 2ndHalf | Month | Year | EPF1stFee | EPF2ndFee   
1011        |  1      |   0     |   2   | 2017 |   29.00   |   0.00

Here my code updated thanks to sr hs :

<?php
    $db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\Users\User\Desktop\Demo2018.mdb; Uid=; Pwd=COMPLETEPAYROLL;");

    $sql  = "SELECT `Employee ID`, `1stHalf`, `2ndHalf`, `Month`, `Year` FROM `tblPAyTrans` WHERE `Employee ID` = '1011' AND Month = '2' AND Year='2017'";

    $result = $db->query($sql);

    echo "<br>RESULT: <br><br>";
    echo "<table border='2'>
            <tr>
                <th>Employee ID</th>
                <th>1st Half</th>
                <th>2nd Half</th>
                <th>Month</th>
                <th>Year</th>
                <th>EPF1stFee</th>
                <th>EPF2ndFee</th>
            </tr>";

        $prevEmpId  = "0";
        $FirstHalf  = "0";
        $SecondHalf = "0";
        $Month      = "";
        $Year       = "";
        while ($row = $result->fetch()) {
            // You can take the following three lines out of the loop, as they remain constant
            $EmployeeID = $row['Employee ID'];
            $Month = $row['Month'];
            $Year  = $row['Year'];

            // Only change the value if non-zero
            if ($FirstHalf === "0"){
                $FirstHalf = $row['1stHalf'];
                $query1 = "SELECT `EPFee` FROM `tblPAyTrans` WHERE `Employee ID` = '$EmployeeID' AND `1stHalf` = '$FirstHalf'";
                $result2 = $db->query($query1);
                $getVal1 = $result2->fetch();
                $EPF1stFee = $getVal['EPFee'];
            }

            // Only change the value if non-zero
            if ($SecondHalf === "0"){
                $SecondHalf = $row['2ndHalf'];
                $query2 = "SELECT `EPFee` FROM `tblPAyTrans` WHERE `Employee ID` = '$EmployeeID' AND `2ndHalf` = '$SecondHalf'";
                $result3 = $db->query($query2);
                $getVal2 = $result3->fetch();
                $EPF2ndFee = $getVal2['EPFee'];
            }

        }

        // Print the record
            echo "<tr>";
                echo "<td>" . $EmployeeID. "</td>";
                echo "<td>" . $FirstHalf. "</td>";
                echo "<td>" . $SecondHalf. "</td>";
                echo "<td>" . $Month. "</td>";
                echo "<td>" . $Year. "</td>";
                echo "<td>" . $EP1stFee. "</td>";
                echo "<td>" . $EP2ndFee. "</td>";
            echo "</tr>";

        echo "</table>";
    ?>

Any ideas to solve this??

Does this SQL solve the problem?

SELECT `Employee ID`, sum(`1stHalf`) AS 1stHalf, sum(`2ndHalf`) AS 2ndHalf, `Month`, `Year`
FROM tblPAyTrans
GROUP BY `Employee ID`, `Month`, `Year`

Result:

Employee ID 1stHalf 2ndHalf Month   Year
1011           1       1      2     2017

Check your result here: http://sqlfiddle.com/#!9/98ddd/4

If you are looking for solution in PHP,

<?php
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\Users\User\Desktop\Demo2018.mdb; Uid=; Pwd=COMPLETEPAYROLL;");

$sql  = "SELECT `Employee ID`, `1stHalf`, `2ndHalf`, `Month`, `Year` FROM `tblPAyTrans` WHERE `Employee ID` = '1011' AND Month = '2' AND Year='2017'";

$result = $db->query($sql);

echo "<br>RESULT: <br><br>";
echo "<table border='2'>
        <tr>
            <th>Employee ID</th>
            <th>1st Half</th>
            <th>2nd Half</th>
            <th>Month</th>
            <th>Year</th>
        </tr>";

    $prevEmpId  = "0";
    $FirstHalf  = "0";
    $SecondHalf = "0";
    $Month      = "";
    $Year       = "";
    while ($row = $result->fetch()) {
        // You can take the following three lines out of the loop, as they remain constant
        $EmployeeID = $row['Employee ID'];
        $Month = $row['Month'];
        $Year  = $row['Year'];

        // Only change the value if non-zero
        if ($FirstHalf === "0")
            $FirstHalf = $row['1stHalf'];

        // Only change the value if non-zero
        if ($SecondHalf === "0")
            $SecondHalf = $row['2ndHalf'];

    }

    // Print the record
        echo "<tr>";
            echo "<td>" . $EmployeeID. "</td>";
            echo "<td>" . $FirststHalf. "</td>";
            echo "<td>" . $SecondHalf. "</td>";
            echo "<td>" . $Month. "</td>";
            echo "<td>" . $Year. "</td>";
        echo "</tr>";

    echo "</table>";
?>

this can also be done for all employees by the following code,

<?php
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\Users\User\Desktop\Demo2018.mdb; Uid=; Pwd=COMPLETEPAYROLL;");

$sql  = "SELECT `Employee ID`, `1stHalf`, `2ndHalf`, `Month`, `Year` FROM `tblPAyTrans` order by `EmployeeID` asc";

$result = $db->query($sql);

echo "<br>RESULT: <br><br>";
echo "<table border='2'>
        <tr>
            <th>Employee ID</th>
            <th>1st Half</th>
            <th>2nd Half</th>
            <th>Month</th>
            <th>Year</th>
        </tr>";

    $prevEmpId  = "0";
    $FirstHalf  = "0";
    $SecondHalf = "0";
    $Month      = "";
    $Year       = "";
    while ($row = $result->fetch()) {
        $EmployeeID = $row['Employee ID'];

        // Check if employee id is repeating, if yes, store the required values
        if (($prevEmpId === "0") || ($prevEmpId === $EmployeeID)) {
            if ($prevEmpId === "0")
                $prevEmpId = $EmployeeID;
            if (($FirstHalf === "0") || ($row['1stHalf'] !== "0"))
                $FirstHalf = $row['1stHalf'];
            if (($SecondHalf === "0") || ($row['2ndHalf'] !== "0"))
                $SecondHalf = $row['2ndHalf'];
            $Month = $row['Month'];
            $Year  = $row['Year'];
        }

        // When the employee Id changes print the values onto the table
        else {
            // Print old values
            echo "<tr>";
                echo "<td>" . $EmployeeID. "</td>";
                echo "<td>" . $FirststHalf. "</td>";
                echo "<td>" . $SecondHalf. "</td>";
                echo "<td>" . $Month. "</td>";
                echo "<td>" . $Year. "</td>";
            echo "</tr>";                

            // Set the new values as the employee id  changed
            $prevEmpId  = $EmployeeID;
            $FirstHalf  = $row['1stHalf'];
            $SecondHalf = $row['2ndHalf'];
            $Month      = $row['Month'];
            $Year       = $row['Year'];
        }
    }

    // Print last record
    // As the last record will be skipped before being displayed
        echo "<tr>";
            echo "<td>" . $EmployeeID. "</td>";
            echo "<td>" . $FirststHalf. "</td>";
            echo "<td>" . $SecondHalf. "</td>";
            echo "<td>" . $Month. "</td>";
            echo "<td>" . $Year. "</td>";
        echo "</tr>";

    echo "</table>";
?>

Make sure the query sorted by EmployeeId ASC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM