Jump to content

[SOLVED] php or statement


ldoozer

Recommended Posts

can anyone see why my statement is not working?

 

if ($TEMPLATE['id'] !="11" || $TEMPLATE['id'] !="15" || $TEMPLATE['id'] !="16" || $TEMPLATE['id'] !="17") { ?>
<table id="productOptionsTbl">
<?=$TEMPLATE['options'];?>
<tr>
	<td class="itemOptionLabel">Add to basket:</td>
	<td class="itemOptionSelect">
		<input name="quantity" type="text" value="1" size="2" />
		item(s)
		<input type="hidden" name="id" value="<?=$TEMPLATE['id'];?>" />
		<input name="action" type="hidden" value="add" />
		<input type="submit" class="submitStyled" value="Buy now" />
	</td>
</tr>
</table>
<? } ?>

Link to comment
https://forums.phpfreaks.com/topic/117146-solved-php-or-statement/
Share on other sites

You are using negative logic (the !) with an OR function. The conditional statement will always be true because the only way it would be false is if the variable being tested simultaneously had all the value at the same time. Pick one of the values and go though the statement and see what it evaluates as -

 

Try 17 -

if (17 !="11" || 17 !="15" || 17 !="16" || 17 !="17") { ?>

 

this gives -

 

if (TRUE || TRUE || TRUE || FALSE) { ?>

 

You need to change the OR's to AND's -

 

if ($TEMPLATE['id'] !="11" && $TEMPLATE['id'] !="15" && $TEMPLATE['id'] !="16" && $TEMPLATE['id'] !="17") { ?>

 

You can also place the list of values into an array and use the in_array() function to test if the variable matches something in the array or use the not (!) !in_array(...) to test if the variable does not match something in the array.

 

 

 

Thank you all very much for the help, heres what i ended up doing to get it working:

<? /*Added to hide add to cart button on specific products */ 
if ($TEMPLATE['id'] !="11" && $TEMPLATE['id'] !="15" && $TEMPLATE['id'] !="16" && $TEMPLATE['id'] !="17") { ?>
<table id="productOptionsTbl">
<?=$TEMPLATE['options'];?>
<tr>
	<td class="itemOptionLabel">Add to basket:</td>
	<td class="itemOptionSelect">
		<input name="quantity" type="text" value="1" size="2" />
		item(s)
		<input type="hidden" name="id" value="<?=$TEMPLATE['id'];?>" />
		<input name="action" type="hidden" value="add" />
		<input type="submit" class="submitStyled" value="Buy now" />
	</td>
</tr>
</table>
<? } ?>

Archived

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

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