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 6568445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:28:42+00:00 2026-05-25T14:28:42+00:00

I am working on a simple finance tracker and appear to be stuck trying

  • 0

I am working on a simple finance tracker and appear to be stuck trying to figure out what should be a simple task using PHP and MySQL – displaying a running financial balance.

There are two MySQL tables, one with the starting balance for each account and a second with the individual transactions. For the purpose of development, I am only working with one account. What I am trying to do is iterate through the transactions and display a running total, subtracting from the starting balance if it is a withdraw or adding if it is a deposit. The amount column for each transaction is negatively signed for withdraws and positively for deposits.

Here is what I have tried without any luck:

<?php
  $getTransactions = $db->query("SELECT * FROM transactions ORDER BY date, id");
  //Get the starting balance
  $getStartingBalance = $db->query("SELECT amount FROM startingBalance WHERE id = 1");
  $startingBalance = $getStartingBalance->fetch();
  //Start off with Running Total same as Starting Balance
  $runningTotal= $startingBalance['amount'];
  //Iterate through transactions
  while ($transaction = $getTransactions->fetch()) {
    $amount = $transaction['amount'];
    if ($amount < 0) {
      $runningTotal = $runningTotal - $amount;
    } else {
      $runningTotal = $runningTotal + $amount;
    }
  }
?>

The above will perform addition even if the number is negative. How would I accomplish this with signed numbers? In the past, I was using an integer to differentiate between deposits and withdraws (1 for deposit, 2 for withdraw) but it was suggested that signed values would be the better route.

Thanks for any help

  • 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-25T14:28:43+00:00Added an answer on May 25, 2026 at 2:28 pm

    A negative negative is a positive! For example 3 - (-1) == 4

    As your transactions are signed, you don’t need to selectively add or subtract. You can skip:

    if ($amount < 0) {
          $runningTotal = $runningTotal - $amount;
        } else {
          $runningTotal = $runningTotal + $amount;
        }
    

    And replace it with just

    $runningTotal = $runningTotal + $amount;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on simple queries but can't figure out how to modify to produce
Im working on simple PHP CMS system and Im stuck with database design. This
I'm looking for good/working/simple to use PHP code for parsing raw email into parts.
Working on a simple poker script in PHP and need a way to determine
Working on a simple C program I'm stuck with an if test: int line_number
I have a simple working PHP script to write an HTML table from a
I am trying to convert a working simple Java application to an applet. The
I'm working on simple application on GAE using python2.7,the purpose for this application is
Currently i am working in simple game app using openGLES, draw a line using
I'm working on simple benchmark framework (reason: boredom and practice). Now I'm trying to

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.