页面刷新php后,Count()函数更新

时间:2015-12-16 21:04:13

标签: javascript php jquery

我已经制作了一个简单的购物车,其中我有一个div,将在悬停时显示拥有购买的物品。问题是每次我点击add to cart按钮。它不计算实例中的购物车项目。我需要做的就是自己刷新页面。任何建议或帮助都会很明显。

代码

  <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>

1 个答案:

答案 0 :(得分:1)

我假设您正在通过add-to-cartajax进行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);

现在你的ajax成功了吗

import random

#create the empty list to store values
dierolls = []  

#used to roll ythe dice
def diceroll():  
    return random.randint(1,6)

#gets user input to determine how many dice we are rollin    
def askfornumofdicetoroll():  
    return int(input("How many dice should we roll?"))

#fills the dierolls list with the appropriate     
def fillthelist(dicecount): 
    #empty the list and start fresh each iteration
    dierolls[:] = []

    #input a die roll for each die the user says to roll
    for i in range(0,dicecount):
        dierolls.append(diceroll())
    #print dierolls #used to check that this code was running
    return dierolls

#what to do when all the dice match
def wongame(attempts):
    print("You matched all the dice in", attempts , "tries")

#compares all the items in the list, and see's if they match
def comparelist(dicetoroll,attempts):

    fillthelist(dicetoroll)
    #print statement used to make sure this was running
    print dierolls
    #print statment used to see if this section of code was running
    print(all(dierolls[0] == elem for elem in dierolls))

    #gives a check to make sure the code is running and not stopped by
    #printing a result every 100 attempts
    if attempts%100 == True:
        print attempts
    else:
        pass

    #does the actual check to see if all items in the list are the same
    if all(dierolls[0] == elem for elem in dierolls):
        #if all items in list are the same, go to the results function
        wongame(attempts)
    else:
        #increment the attempts counter, and try again
        attempts += 1
        comparelist(dicetoroll, attempts)  

#runs the program
def main():
    attempts = 1
    dicetoroll = askfornumofdicetoroll()
    comparelist(dicetoroll,attempts)

如果您没有通过ajax执行此操作,请在回显任何html标记之前将产品添加到php文件开头的购物车中。