php - How to calculate the value of checked box?

Solution:

Try this

<body>
    <table>
        <tbody>
            <tr>
                <td><div><input type="checkbox"></div></td>
                <td>...</td>
                <td>...</td>
                <td>deducing</td>
                <td>1000</td>
            </tr>
            <tr>
                <td><div><input type="checkbox"></div></td>
                <td>...</td>
                <td>...</td>
                <td>earning</td>
                <td>6000</td>
            </tr>
            <tr>
                <td><div><input type="checkbox"></div></td>
                <td>...</td>
                <td>...</td>
                <td>earning</td>
                <td>3000</td>
            </tr>
            <tr>
                <td><div><input type="checkbox"></div></td>
                <td>...</td>
                <td>...</td>
                <td>earning</td>
                <td>1200</td>
            </tr>
            <tr>
                <td><div><input type="checkbox"></div></td>
                <td>...</td>
                <td>...</td>
                <td>deducing</td>
                <td>1000</td>
            </tr>
        </tbody>
    </table>
    <button type="button" id="button">Yo</button>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script>
        $(function() {
            $("#button").click(function() {
                let total = 0;
                $("tbody:first tr").each(function() {
                    if ($(this).children("td").first().children("div").children("input").is(":checked")) {
                        if ("deducing" == $(this).children("td").eq(3).text()) total = total - parseInt($(this).children("td").eq(4).text());
                        else if ("earning" == $(this).children("td").eq(3).text()) total = total + parseInt($(this).children("td").eq(4).text());
                    }
                });
                alert(total);
            });
        });
    </script>
</body>

Answer

Solution:

If my comment is correct, and the issue here is that you have the correct amounts, but have lost the "meta information" of whether to add or subtract to the total, then I think the simplest solution would be to do the following:

Firstly change the value of the checkbox so that it reflects the action to take - ie if it is {-code-1} then make the value negative. So your input would be:

<input type='checkbox' name='payroll_group' value=" . ($row['category_type'] === '{-code-1}' ? $row["amount"] * -1 : $row["amount"]) . "class='form-check-input'>

Where ($row['category_type'] === '{-code-1}' ? $row["amount"] * -1 : $row["amount"]) will check if the category_type is {-code-1} then multiply amount by -1 otherwise keep the current amount value. Note the surrounding parenthesis are necessary.

Then in your current js, you should end up with [-1000,35000,6500] in your current example. You can then do a simple loop to go through the array and add to a total, or you could use something like

var totalAmount = favorite.reduce(
    function (total, amount) {
        return parseInt(total) + parseInt(amount)
    },
    0 // The initial value to start with
)

Source