dwperry1 Posted March 7, 2012 Share Posted March 7, 2012 I have a function in my php form that creates two digits after the ndecimal. Works great: function Money($float) { return sprintf("%01.2f", $float); } The problem is that when it prints the output and there is no value in that field on the form, it prints "0.00". What I need it to do is, if the value is "0" then print nothing " " or leave the database blank for that value. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/ Share on other sites More sharing options...
smerny Posted March 7, 2012 Share Posted March 7, 2012 basically just translated your words into code... function Money($float) { if($float == 0) return ""; // if the value is "0" then print nothing "" else return sprintf("%01.2f", $float); } Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1324955 Share on other sites More sharing options...
dwperry1 Posted March 7, 2012 Author Share Posted March 7, 2012 I tried this and I still get 0.00 in the database so it still prints 0.00 in the result. I tried changing the line "if($float == 0)" to "if($float == 0.00)" and I get the same result. I am entering the value into the db by: GetSQLValueString(Money($_POST['currency_total'], "text")), if this helps. Thanks, Doug Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1324958 Share on other sites More sharing options...
smerny Posted March 7, 2012 Share Posted March 7, 2012 so is the value coming in unset (not 0 or anything?) you could check if it's equal to 0 after you do the sprintf function Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1324962 Share on other sites More sharing options...
dwperry1 Posted March 7, 2012 Author Share Posted March 7, 2012 If I don't enter anything in the form fields, all of the Money fields are 0.00 since I began using the function. Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1324967 Share on other sites More sharing options...
smerny Posted March 7, 2012 Share Posted March 7, 2012 oh, well empty and 0 are different. you should make sure they are passing a number as well.. you can use the is_numeric() method Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1324970 Share on other sites More sharing options...
marcus Posted March 7, 2012 Share Posted March 7, 2012 function Money($float){ return (floatval($float) > 0) ? sprintf("%01.2f", floatval($float)) : 0; } var_dump(Money(0), Money(2.12), Money(3.112)); int(0) string(4) "2.12" string(4) "3.11" Insert into your database by just wrapping it in quotes instead of using the GetSQLValueString method INSERT INTO `table` (`floatField`) VALUES('".Money(1.21)."') Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1324971 Share on other sites More sharing options...
dwperry1 Posted March 7, 2012 Author Share Posted March 7, 2012 smerny, Still cannot make this function return no value. mgallforever, I don't understand why GetSQLValueString method won't work or why this has anything to do with returning 0.00? I am fairly new to php and don't understand the reasoning behind your code. Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1324982 Share on other sites More sharing options...
marcus Posted March 7, 2012 Share Posted March 7, 2012 What's the default value for the field in your table? It could possibly be set to 0.00 and a blank value will set itself to the default value. Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1324983 Share on other sites More sharing options...
dwperry1 Posted March 7, 2012 Author Share Posted March 7, 2012 default value is NULL. Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1324985 Share on other sites More sharing options...
smerny Posted March 7, 2012 Share Posted March 7, 2012 he seems to NOT want 0.00 to show. function Money($float) { if(!is_numeric($float) || $float == 0) return ""; // if the value is "0" then print nothing "" else return sprintf("%01.2f", $float); } ? as far as inserting into db, see http://www.tizag.com/mysqlTutorial/mysqlinsert.php Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1324986 Share on other sites More sharing options...
marcus Posted March 7, 2012 Share Posted March 7, 2012 function Money($float){ return (floatval($float) != 0) ? sprintf("%01.2f", floatval($float)) : ''; } var_dump(Money(0), Money(2.12), Money(3.112) , Money('cats')); You wouldn't have to use GetSQLValueString if you're confident enough that your methods will always return what you want them to return. My above method will take in the float variable, get the float value of it, check if it's equal to 0; if it isn't equal to 0 it will return the sprintf call, else it will return the empty string. The reason I used "cats" as an example is to show you the confidence of the method. floatval('cats') == 0 Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1324988 Share on other sites More sharing options...
dwperry1 Posted March 7, 2012 Author Share Posted March 7, 2012 Thanks for the explanation! I will look into it. Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1324990 Share on other sites More sharing options...
Pikachu2000 Posted March 7, 2012 Share Posted March 7, 2012 Attempting to insert an empty string into a FLOAT data type field will not result in a NULL value, it will result in 0.00. If you want a NULL value, either explicitly insert a NULL value, or use MySQL's DEFAULT keyword, since your default for the field is NULL. Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1324991 Share on other sites More sharing options...
dwperry1 Posted March 7, 2012 Author Share Posted March 7, 2012 This works: function Money($float){ return (floatval($float) > 0) ? sprintf("%01.2f", floatval($float)) : 0; } But, Now I get "0" returned in fields with bno value. How do I use the DEFAULT keyword? Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1325015 Share on other sites More sharing options...
dwperry1 Posted March 8, 2012 Author Share Posted March 8, 2012 function Money($float){ return (floatval($float) > 0) ? sprintf("%01.2f", floatval($float)) : 0; } This doesn't work with negative values. Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1325043 Share on other sites More sharing options...
marcus Posted March 8, 2012 Share Posted March 8, 2012 I had revised my code to work with negative values... function Money($float){ return (floatval($float) != 0) ? sprintf("%01.2f", floatval($float)) : ''; } Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1325057 Share on other sites More sharing options...
dwperry1 Posted March 8, 2012 Author Share Posted March 8, 2012 After testing: The code below returns (0.00) to all fields with no value. function Money($float) { return sprintf("%01.2f", $float); } The code below returns (0) which is better than 0.00, but doesn't work on negatibe numbers. When negative numbers are introduced, the result is: 0.00 and the negative value is ignored. function Money($float){ return (floatval($float) > 0) ? sprintf("%01.2f", floatval($float)) : 0; } var_dump(Money(0), Money(2.12), Money(3.112)); I am still looking for a solution that will allow for a return value of " " from the db when no value is entered. Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1325061 Share on other sites More sharing options...
marcus Posted March 8, 2012 Share Posted March 8, 2012 Do you even bother looking at the code changes? Running the updated code (third time posting for you) function Money($float){ return (floatval($float) != 0) ? sprintf("%01.2f", floatval($float)) : ''; } var_dump(Money(0.00), Money(0.01), Money(''), Money(-5.43)); string(0) "" string(4) "0.01" string(0) "" string(5) "-5.43" Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1325063 Share on other sites More sharing options...
dwperry1 Posted March 8, 2012 Author Share Posted March 8, 2012 mgallforever, Sorry, I didn't know there was a page two as I am unfamiliar with this forum. Your code produced the following errors upon submission: Warning: Missing argument 2 for GetSQLValueString(), and Notice: Undefined variable: theType ... Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1325069 Share on other sites More sharing options...
marcus Posted March 8, 2012 Share Posted March 8, 2012 That's your own issue, nothing to do with my code. Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1325071 Share on other sites More sharing options...
dwperry1 Posted March 8, 2012 Author Share Posted March 8, 2012 I don't know what issues you would be referring to. None of the other suggestions on this subject in this forum produced any errors. I am simply trying to enter a value in a php form that populates the MySQL db, and when no value is entered, I would like to have no value returned. I can accomplish this by not using the following code: function Money($float) { return sprintf("%01.2f", $float); } However, I use this code to format two decimal places to my values so I don't end up with 5.1 instead of 5.10 in the db. Can you help me develop code that will return a blank result when no value is given? Your help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1325075 Share on other sites More sharing options...
marcus Posted March 8, 2012 Share Posted March 8, 2012 The function I posted will return a blank result when a) value == 0 b) no value given Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1325078 Share on other sites More sharing options...
dwperry1 Posted March 8, 2012 Author Share Posted March 8, 2012 OK, can you make it work with GetSQLValueString() ? I would like to protect it from code injection. Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1325086 Share on other sites More sharing options...
marcus Posted March 8, 2012 Share Posted March 8, 2012 You can use the function stand alone without using GetSQLValueString. My function won't return anything but a float value or the empty string. Quote Link to comment https://forums.phpfreaks.com/topic/258481-function-help/#findComment-1325087 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.