Jump to content

Conditional Fails


son

Recommended Posts

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

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

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

$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

Link to comment
Share on other sites

$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

I think I have to re-phrase: I would like to disallow the first value. Son
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]);

Link to comment
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...