Exoon Posted May 30, 2011 Share Posted May 30, 2011 Hello, Im making a little script to keep track on how much people have currently earnt on my site. I have everything work but i can't figure out how to work out the current balance At the moment ive got: $currentbalance = $row['totalpaid'] - $row['points']; Which works untill the points becomes more then the total paid then it starts going in minus. Im sure there's a simple fix but i just can't think of a way. Any help would be great. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/237876-working-out-remaining-balance/ Share on other sites More sharing options...
mikesta707 Posted May 30, 2011 Share Posted May 30, 2011 is the absolute value of that difference the current balance? if so use the abs() function. This takes the absolute value of the input argument. $currentbalance = abs($row['totalpaid'] - $row['points']); Quote Link to comment https://forums.phpfreaks.com/topic/237876-working-out-remaining-balance/#findComment-1222364 Share on other sites More sharing options...
seanlim Posted May 30, 2011 Share Posted May 30, 2011 If it's just a matter of displaying 0 when the $currentbalance drops below 0, use $currentbalance = max($row['totalpaid'] - $row['points'], 0); But more importantly, why would the points be negative in the first place? Is your calculations correct? Quote Link to comment https://forums.phpfreaks.com/topic/237876-working-out-remaining-balance/#findComment-1222365 Share on other sites More sharing options...
Exoon Posted May 30, 2011 Author Share Posted May 30, 2011 Im not really sure now ive confused myself. Im trying to show How much has been paid and how is remaining So if they've earnt 5.00 and they request 4.50 i want the current balance to then show 0.50 Ive got Points field which contains the total money theyve earnt(including what theyve been paid), and ive got totalpaid which contains the total amount theyve been paid. Quote Link to comment https://forums.phpfreaks.com/topic/237876-working-out-remaining-balance/#findComment-1222374 Share on other sites More sharing options...
mikesta707 Posted May 30, 2011 Share Posted May 30, 2011 why don't you reverse the two operands? Seems like you should be subtracting total paid from points, not the other way around. Also, a negative value can make sense assuming you let people "over withdraw" or go into debt Quote Link to comment https://forums.phpfreaks.com/topic/237876-working-out-remaining-balance/#findComment-1222386 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.