Jump to content

function help?


dwperry1

Recommended Posts

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?

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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)."')

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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 ...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.