sabecs
asked on
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.
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>
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.
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; ?>
ASKER
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?
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window