Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6714919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:33:07+00:00 2026-05-26T08:33:07+00:00

I have been trying to figure out a weird issue with signed integers and

  • 0

I have been trying to figure out a weird issue with signed integers and PHP in a finance app I am working on. Each transaction’s amount is a signed integer, I balance the account using SUM() and have a function that should output each entry with a running balance, ordered by the row’s id and date in descending order.

Everything appeared to be working fine until the account balance went below 0 (-6.20 or so). When a deposit was entered, instead of simply returning to a positive number, the running total output 1.00 and the running total of nearly every transaction prior to it read as a negative – except for perhaps the ones that were either near or below $0. Quite puzzled.. here is the function (part of a class):

public function fetchTransactions($limit = null) {
        global $db;
        $this->balanceAccount(); //Calls upon another function to return the balance
        $runningTotal = $this->accountBalance;
        $prevAmount = 0;
        if (isset($limit)) {
            $sql = "SELECT amount, payee, cat, date FROM transactions ORDER BY date DESC, id DESC LIMIT $limit";
        } else {
            $sql = "SELECT amount, payee, cat, date FROM transactions ORDER BY date DESC, id DESC";
        }
        $fetchTransactionsSQL = $db->query($sql);
        while ($transactionDetails = $fetchTransactionsSQL->fetch()) {
            $date = date("m/d", strtotime($transactionDetails['date']));
            $payee = stripslashes($transactionDetails['payee']);
            $category = $transactionDetails['cat'];
            $amount = $transactionDetails['amount'];
            $runningTotal -= $prevAmount; //I have also tried addition here
            $prevAmount = $amount;
            $amountOutput = (strpos($amount, '-') === true ? '-' . money_format("%n", $amount) : money_format("%n", $amount));
            $runningTotalOutput = money_format("%n", $runningTotal);
            echo "
                <tr>
                    <td>$date</td>
                    <td><strong>$payee</strong></td>
                    <td>$category</td>
                    <td>$amountOutput</td>
                    <td>$runningTotalOutput</td>
                </tr>                    
            ";
        }
    }

Any help would be greatly appreciated. Thanks!

EDIT = Further explanation

To clear up some confusion. The entries are being pulled from the database in descending order, where the last transaction is at the end of the MySQL table but the first one displayed on the page. I start $prevAmount at 0 and then set it to the current transaction’s amount during the loop. $runningTotal starts as the overall account balance and the running balance is obtained by subtracting the previous transaction’s amount ($prevAmount) from $runningTotal during the loop. If anyone can suggest an easier way to accomplish this I’m all ears — it worked great until the account balance fell below 0.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:33:07+00:00Added an answer on May 26, 2026 at 8:33 am

    A simple case of needing to look elsewhere in my code for the problem. For whatever reason, I had used number_format() on both the Starting Balance and Account Balance function’s output. Apparently this gets in the way of mathematical operations, everything works as it should after removing this from those functions. Boo for bad code, hooray for figuring it out!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been going mad trying to figure out why my scripts weren't working,
I have been trying to figure out why my variable assigned sum isn't working,
I have been trying to figure out how to integrate a node.js app into
I have been trying to figure out a solution but nothing has really presented
I have been trying to figure out a way to tag several methods from
I have been trying to figure out why and how to use performSelector. I
I have been trying to figure this out for way to long tonight. I
I have been trying to figure this out for some time. It seems lots
I have been pulling my hair out trying to figure out what I can't
I have been scratching my head for hours trying to figure out why this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.