ngreenwood6 Posted October 10, 2008 Share Posted October 10, 2008 I have a script that puts a user inputted value into the database. It can be put in as either a debit or a credit. If it is a credit it will be positive and if it is a debit it will be negative. I have created a drop down box that allows them to select debit or credit. However I just want them to be able to select either debit or credit and if it is debit automatically put a "-" in front of the number. The reason is because later it is called and the values are added. If not the user will have to put a "-" when they enter the value. Does anyone know how I can do this? I have something like this but it doesnt work: if($type == "Debit") { $amount = "-$amount"; } I have tried some others but none seem to work. Any help is appreciated. Link to comment https://forums.phpfreaks.com/topic/127921-solved-setting-a-value/ Share on other sites More sharing options...
sh0wtym3 Posted October 10, 2008 Share Posted October 10, 2008 I need to see more of your code to help but, try using this: if($type == "Debit") { $newamount=.'-'.$amount; } ... And then insert $newamount into your database. And where is your $amount variable originally being called? If the number is being input by a user, there should be a line of code somewhere that looks something like: $amount= $_POST['amount']; (That's assuming you've named your text input box "amount") Link to comment https://forums.phpfreaks.com/topic/127921-solved-setting-a-value/#findComment-662322 Share on other sites More sharing options...
DarkWater Posted October 10, 2008 Share Posted October 10, 2008 Well, think about what a negative number is. You can change a number to its negative with a general equation like: 0 - x = -x So, by that logic: if ($type == "Debit") { $newamount = 0 - (int) $amount; } Link to comment https://forums.phpfreaks.com/topic/127921-solved-setting-a-value/#findComment-662338 Share on other sites More sharing options...
AndyB Posted October 10, 2008 Share Posted October 10, 2008 Improved variant: if ($type=="Debit") { $amount = 0 - abs($amount); } Generates a negative value for $amount even if the user mistakenly places a negative sign before their input debit value. Link to comment https://forums.phpfreaks.com/topic/127921-solved-setting-a-value/#findComment-662343 Share on other sites More sharing options...
ngreenwood6 Posted October 11, 2008 Author Share Posted October 11, 2008 I have tried all of your suggestions but none of them seem to work properly for me. Any other suggestions? Link to comment https://forums.phpfreaks.com/topic/127921-solved-setting-a-value/#findComment-662464 Share on other sites More sharing options...
sh0wtym3 Posted October 11, 2008 Share Posted October 11, 2008 Can you post your entire code? Link to comment https://forums.phpfreaks.com/topic/127921-solved-setting-a-value/#findComment-662467 Share on other sites More sharing options...
ngreenwood6 Posted October 11, 2008 Author Share Posted October 11, 2008 Sorry actually this topic is solved. I had the query above the part where I changed the value of $amount. When I moved it below it works properly. Thanks for all the help. However, someone can someone explain to me what the abs() function does? Link to comment https://forums.phpfreaks.com/topic/127921-solved-setting-a-value/#findComment-662468 Share on other sites More sharing options...
DarkWater Posted October 11, 2008 Share Posted October 11, 2008 It returns the absolute value of a number. Link to comment https://forums.phpfreaks.com/topic/127921-solved-setting-a-value/#findComment-662494 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.