ms.buggy Posted May 7, 2010 Share Posted May 7, 2010 Hi, I have a form with multiple fields that are stored to database. Problem is that for the further purposes, 0 values are invalid. So if someone writes zero values programn should clear those. Ok, this is very simple made with multiple if-functions, like 'if($variable==0){$variable="";}'. But I'm interested to find other way to handle multiple values, instead of writing separate if-function for all of them. I thought that this might not be the lightest way to handle variables. I think the optimal solution would be written so that zero values would not post from form to function that stores values to database. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/200983-clearing-zero-variables/ Share on other sites More sharing options...
Psycho Posted May 7, 2010 Share Posted May 7, 2010 Just create a function function clearZero($value) { return ($value==0) ? '' : $value; } Then use that function when assigning the values to your variables: $variable = clearZero($_POST['fieldName']); Quote Link to comment https://forums.phpfreaks.com/topic/200983-clearing-zero-variables/#findComment-1054495 Share on other sites More sharing options...
phpchamps Posted May 7, 2010 Share Posted May 7, 2010 As you want to test it on all the variable of post. Please find my code below :- function clearZero($arr){ if(is_array($arr) && !empty($arr)){ foreach($arr as $key=>$val){ $val != 0 ? $new_arr[$key] = $val : false; } } return $new_arr; } $clearArr = clearZero($_POST); print_r($clearArr); Quote Link to comment https://forums.phpfreaks.com/topic/200983-clearing-zero-variables/#findComment-1054513 Share on other sites More sharing options...
ms.buggy Posted May 7, 2010 Author Share Posted May 7, 2010 Thank you very much mjdamato! And thank you too phpchamps, but was this only for testing? In this case, I prefer to test and check the status of variables from the database, just in case. It always nice to learn new! Quote Link to comment https://forums.phpfreaks.com/topic/200983-clearing-zero-variables/#findComment-1054542 Share on other sites More sharing options...
ms.buggy Posted May 12, 2010 Author Share Posted May 12, 2010 Okay, code works mainly correctly. But I have managed to resieve an error message in some cases. Message is: "Fatal error: Cannot redeclare clearzero() (previously declared in..." and it's pointing to the line2 'function clearZero($value)'. This is caused when saved data already exist in the database, strange becouse there is no limitation/controlling of duplicate data. What is really strange is that this is caused when the function is not even used?? Becouse I have few values that are not even check with the 'clear zero' -funtion, when reading them to variables, and this may happen even if only these variables are used. The function is: function clearZero($value) { return ($value==0) ? '' : trim($value); } And variables are read: $variable1=trim($_POST['variable1']); $variable2=clearZero($_POST["variable2"]); I only guessing, but could this be some kind of a cache issue? Quote Link to comment https://forums.phpfreaks.com/topic/200983-clearing-zero-variables/#findComment-1056988 Share on other sites More sharing options...
Adam Posted May 12, 2010 Share Posted May 12, 2010 Sounds like you may be declaring the function within a loop. Quote Link to comment https://forums.phpfreaks.com/topic/200983-clearing-zero-variables/#findComment-1056995 Share on other sites More sharing options...
phpchamps Posted May 12, 2010 Share Posted May 12, 2010 you are declaring that function twice unknowingly..either in include file or in loop... post the full code... plz... Quote Link to comment https://forums.phpfreaks.com/topic/200983-clearing-zero-variables/#findComment-1056997 Share on other sites More sharing options...
ignace Posted May 12, 2010 Share Posted May 12, 2010 As you want to test it on all the variable of post. Please find my code below :- function clearZero($arr){ if(is_array($arr) && !empty($arr)){ foreach($arr as $key=>$val){ $val != 0 ? $new_arr[$key] = $val : false; } } return $new_arr; } $clearArr = clearZero($_POST); print_r($clearArr); Actually you can do this better and faster using mjdamato's function. $_POST = array_map('clearZero', $_POST); Quote Link to comment https://forums.phpfreaks.com/topic/200983-clearing-zero-variables/#findComment-1056999 Share on other sites More sharing options...
salathe Posted May 12, 2010 Share Posted May 12, 2010 Okay, code works mainly correctly. But I have managed to resieve an error message in some cases. Message is: "Fatal error: Cannot redeclare clearzero() (previously declared in..." and it's pointing to the line2 'function clearZero($value)'. See: http://www.phpfreaks.com/tutorial/defining-a-php-function-only-once Quote Link to comment https://forums.phpfreaks.com/topic/200983-clearing-zero-variables/#findComment-1057021 Share on other sites More sharing options...
ms.buggy Posted May 12, 2010 Author Share Posted May 12, 2010 I see and got it. I just cant figure out what causes the looping.. Of course clearZero function is called many times, becouse several variables are read throught that, but that isn't loop, or is it? But anyways Excellent! Quote Link to comment https://forums.phpfreaks.com/topic/200983-clearing-zero-variables/#findComment-1057055 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.