[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

Hide DIV if record found in MySQL table.

Hi,
I am trying to work out how to hide "add to cart" button and display an "item added" image instead if an item has already been added to my shopping cart..

I hope this makes sense.

Thanks in advance for your help.

<?php
		//find item in cart and hide "add to cart" button if already in cart, display in cart image instead
		$query_my_cart = sprintf("SELECT items_id from cart WHERE cart.cookieId = '%s'", $cookie);
		$my_cart = mysql_query($query_my_cart, $conn_data) or die(mysql_error());
		$row_my_cart = mysql_fetch_assoc($my_cart);
		
		do{
		$items_id = $row_my_cart['items_id'];
		
		//if match found 
		//code here to show div with id itemadded.$items_id and hide div with id addtocart.$items_id
		
		} while($row_my_cart = mysql_fetch_assoc($my_cart)) ;	

?>

<div class="itemadded_div" id="itemadded<?php echo $id; ?>" >
<img src="/images/form_button_item_added.png" width="136" height="33" alt="Item has been added to cart" />
</div>

<div class="addtocart_div" id="addtocart<?php echo $id; ?>">
<a href="#" class="btnWrap"><input class="formbutton" name="additem" value="Add to Cart" type="button" onClick="javascript:AddItem('<?php echo $id; ?>',document.getElementById('qty<?php echo $cnt; ?>').value,document.getElementById('spec1<?php echo $cnt; ?>').value,document.getElementById('spec2<?php echo $cnt; ?>').value)"></a>
</div>

Open in new window

0
sabecs
Asked:
sabecs
1 Solution
 
ropennerCommented:
sounds like a simple question so this is the simple IF ELSE statement that would do what it seems you are asking
<?php
		//find item in cart and hide "add to cart" button if already in cart, display in cart image instead
		$query_my_cart = sprintf("SELECT items_id from cart WHERE cart.cookieId = '%s'", $cookie);
		$my_cart = mysql_query($query_my_cart, $conn_data) or die(mysql_error());
		$row_my_cart = mysql_fetch_assoc($my_cart);
		
		do{
		$items_id = $row_my_cart['items_id'];
		
		//if match found 
		//code here to show div with id itemadded.$items_id and hide div with id addtocart.$items_id
		if ($items_id == $id) {
?>
			<div class="itemadded_div" id="itemadded<?php echo $id; ?>" >
			<img src="/images/form_button_item_added.png" width="136" height="33" alt="Item has been added to cart" />
			</div>
<?php 
} else {
?>
		<div class="addtocart_div" id="addtocart<?php echo $id; ?>">
		<a href="#" class="btnWrap"><input class="formbutton" name="additem" value="Add to Cart" type="button" onClick="javascript:AddItem('<?php echo $id; ?>',document.getElementById('qty<?php echo $cnt; ?>').value,document.getElementById('spec1<?php echo $cnt; ?>').value,document.getElementById('spec2<?php echo $cnt; ?>').value)"></a>
		</div>		
<?php		
		
		
		} while($row_my_cart = mysql_fetch_assoc($my_cart)) ;	
?>

Open in new window

0
 
Robert SchuttSoftware EngineerCommented:
The query only acts on the 'cart' table so presumably the items you show here are all in the cart, this looks like code for a show cart/check-out page. On another page, maybe where you show all items in a certain category or matching a search term, you could add a left join on cart and then use the above if-else.
0
 
FugasCommented:
Hi, the if:else strategy is good, but most effective it will be like this.
<?php
		//find item in cart and hide "add to cart" button if already in cart, display in cart image instead
		$query_my_cart = sprintf("SELECT items_id from cart WHERE cart.cookieId = '%s'", $cookie);
		$my_cart = mysql_query($query_my_cart, $conn_data) or die(mysql_error());
		
		while($row_my_cart = mysql_fetch_assoc($my_cart)) $cart_id[$row_my_cart['items_id']] = 1;
		
?>
<?php if(isset($cart_id[$id])) : ?>
<div class="itemadded_div" id="itemadded<?php echo $id; ?>" >
<img src="/images/form_button_item_added.png" width="136" height="33" alt="Item has been added to cart" />
</div>
<?php else: ?>
<div class="addtocart_div" id="addtocart<?php echo $id; ?>">
<a href="#" class="btnWrap"><input class="formbutton" name="additem" value="Add to Cart" type="button" onClick="javascript:AddItem('<?php echo $id; ?>',document.getElementById('qty<?php echo $cnt; ?>').value,document.getElementById('spec1<?php echo $cnt; ?>').value,document.getElementById('spec2<?php echo $cnt; ?>').value)"></a>
</div>
<?php endif; ?>

Open in new window

0
 
sabecsAuthor Commented:
Thanks for your help and comments, I know it will work with a loop but I may have hundreds of items per page so I don't want to run the loop for each item.

Should I just check if just check if item is in array and then show or hide the div?

Is it better to do something like this?
//find item in cart and hide add to cart button if already in cart, display in cart image
	$query_my_cart = sprintf("SELECT items_id from cart WHERE cart.cookieId = '%s'", $cookie);
	$my_cart = mysql_query($query_my_cart, $conn_data) or die(mysql_error());
	$row_my_cart = mysql_fetch_array($my_cart);
	
	do{
	$items_in_cart[] = $row_my_cart['items_id'];
	} while($row_my_cart = mysql_fetch_assoc($my_cart)) ;	




<div class="itemadded_div" id="itemadded<?php echo $id; ?>" <?php if (is_array($items_in_cart) && (in_array(<?php echo $id; ?>,$items_in_cart))) echo 'display:show' else echo echo 'display:none'; } ?>>
<img src="/images/form_button_item_added.png" width="136" height="33" alt="Item has been added to cart" />
</div>

<div class="addtocart_div" id="addtocart<?php echo $id; ?>" <?php if (is_array($items_in_cart) && (in_array(<?php echo $id; ?>,$items_in_cart))) echo 'display:none' else echo echo 'display:show'; } ?>>
<a href="#" class="btnWrap"><input class="formbutton" name="additem" value="Add to Cart" type="button" onClick="javascript:AddItem('<?php echo $id; ?>',document.getElementById('qty<?php echo $cnt; ?>').value,document.getElementById('spec1<?php echo $cnt; ?>').value,document.getElementById('spec2<?php echo $cnt; ?>').value)"></a>
</div>

Open in new window

0
 
FugasCommented:
This code <?php
            //find item in cart and hide "add to cart" button if already in cart, display in cart image instead
            $query_my_cart = sprintf("SELECT items_id from cart WHERE cart.cookieId = '%s'", $cookie);
            $my_cart = mysql_query($query_my_cart, $conn_data) or die(mysql_error());
            
            while($row_my_cart = mysql_fetch_assoc($my_cart)) $cart_id[$row_my_cart['items_id']] = 1;
            
?> you should run only once. It will set array fields with ids in your selection from the cart table. In the loop you will have the rest of code.
<?php if(isset($cart_id[$id])) : ?>
<div class="itemadded_div" id="itemadded<?php echo $id; ?>" >
<img src="/images/form_button_item_added.png" width="136" height="33" alt="Item has been added to cart" />
</div>
<?php else: ?>
<div class="addtocart_div" id="addtocart<?php echo $id; ?>">
<a href="#" class="btnWrap"><input class="formbutton" name="additem" value="Add to Cart" type="button" onClick="javascript:AddItem('<?php echo $id; ?>',document.getElementById('qty<?php echo $cnt; ?>').value,document.getElementById('spec1<?php echo $cnt; ?>').value,document.getElementById('spec2<?php echo $cnt; ?>').value)"></a>
</div>
<?php endif; ?>
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now