son Posted September 13, 2009 Report Share Posted September 13, 2009 I only want to insert data into productAge when the an option other than value '0' is selected. The code is: if(count($ageing) > 0 && $ageing != 0) { // first delete all exiting entries for pid in productAge $del_pa_query = "DELETE FROM productAge WHERE product_id = $pid"; $del_pa_result = mysqli_query ($dbc, $del_pa_query); // now add new entries for pid in productAge $sql1 = "INSERT INTO productAge (age_id, product_id) VALUES "; foreach ($ageing as $v) { $sql1 .= "('$v', '$pid'),"; } $sql1 = substr($sql1,0,-1); $sql1Res = mysqli_query ($dbc, $sql1); } It keeps inserting data with a 0-value for age_id. Am not getting it...Son Link to comment Share on other sites More sharing options...
chibineku Posted September 13, 2009 Report Share Posted September 13, 2009 Hm...if $ageing is an array, then count($ageing) > 0 just means that it isn't an empty array, and $ageing != 0 means that $ageing is not FALSE, effectively, so those conditions don't really test for any value of $ageing being non-zero - just that it exists at all. If it's not an array, then count($ageing) > 0 is always true as long as $ageing is set, surely. Link to comment Share on other sites More sharing options...
son Posted September 13, 2009 Author Report Share Posted September 13, 2009 Hm...if $ageing is an array, then count($ageing) > 0 just means that it isn't an empty array, and $ageing != 0 means that $ageing is not FALSE, effectively, so those conditions don't really test for any value of $ageing being non-zero - just that it exists at all. If it's not an array, then count($ageing) > 0 is always true as long as $ageing is set, surely.How can you disallow the first value (0) of the array?Son Link to comment Share on other sites More sharing options...
boen_robot Posted September 13, 2009 Report Share Posted September 13, 2009 (edited) $ageing != 0 checks whether the value of the variable is other than zero. An array is not a zero, regardless of its contents.If you want to check whether an array contains the value zero, you'll have to use the array_search() function instead, like: if(!empty($areing) && array_search(0, $areing, true) === false) How can you disallow the first value (0) of the array?You mean how you can check out if the first value is other than zero? Ecplicitly address it of course, i.e. replace$ageing != 0 with $ageing[0] != 0 Edited September 13, 2009 by boen_robot Link to comment Share on other sites More sharing options...
son Posted September 13, 2009 Author Report Share Posted September 13, 2009 $ageing != 0 checks whether the value of the variable is other than zero. An array is not a zero, regardless of its contents.If you want to check whether an array contains the value zero, you'll have to use the array_search() function instead, like: if(!empty($areing) && array_search(0, $areing, true) === false) You mean how you can check out if the first value is other than zero? Ecplicitly address it of course, i.e. replace $ageing != 0 with $ageing[0] != 0 I think I have to re-phrase: I would like to disallow the first value. Son Link to comment Share on other sites More sharing options...
boen_robot Posted September 13, 2009 Report Share Posted September 13, 2009 I think I have to re-phrase: I would like to disallow the first value. SonYou can't... not really... especially if the array originates from a $_POST or $_GET input. The best you can do is remove it manually with unset(), like:unset($ageing[0]); Link to comment Share on other sites More sharing options...
son Posted September 13, 2009 Author Report Share Posted September 13, 2009 You can't... not really... especially if the array originates from a $_POST or $_GET input. The best you can do is remove it manually with unset(), like:unset($ageing[0]); Thanks for your help. I will check this out...Son Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now