tabia
tabia

Reputation: 635

Count() function updates after page refresh php

I have made a simple cart in which i have a div which will be showed on hover which possess purchased items. The problem is each time i click on add to cart button. It does not count the cart items at instance. All i need to do is to refresh my page by myself. Any suggestions or help will be appreciable.

Code

  <div id="cartContainer">
  <div id="cart">
  <li style="color: #515151">
  <img id="cart_img" src="images/cart.png">
  Cart <span class='badge' id='comparison-count'>
  <?php
  if(isset($_SESSION['cart'])&& !empty($_SESSION['cart'])&& count($_SESSION['cart'])>0)
   {
   echo count($_SESSION['cart']);
    }
  else {
  $cart_count=0;
  echo $cart_count;
  }
 ?>
  </span>
 </li>
  <div id="sidebar">
   <?php
  if(isset($_SESSION['cart'])&& !empty($_SESSION['cart'])){
  ?>
   <table id="s_table">
   <?php
   foreach($_SESSION['cart'] as $id => $value){
     //here it shows the item details
     }
    ?>
   </table>
   </div>
    </div>
   </div>

Upvotes: 1

Views: 821

Answers (1)

Scarecrow
Scarecrow

Reputation: 4137

I am assuming that you are doing add-to-cart by ajax call.

Now on your ajax success do this

var no_of_item = Number($("#comparison-count").html());
var new_no_of_item = no_of_item + no_of_item_purchased_in_last_add_to_cart;
$("#comparison-count").html(new_no_of_item);

If you are not doing it by ajax,then add the product to cart at the beginning of the php file before echoing any html tag.

Upvotes: 1

Related Questions