2013-02-21 26 views
1

我一直在努力購買一個購物車的PHP代碼。PHP中的購物車不能正常工作

尤其是,添加該項目的方式,它將很容易有多個訂單項目的功能。

這裏是我有什麼,但它似乎並沒有用於添加第二項工作:

function addtocart($pid,$q) 
    { 
     if($pid<1 or $q<1) return; 
     if (is_array($_SESSION['cart'])) 
      { 
       $max=count($_SESSION['cart']); 
       $_SESSION['cart'][$max]['itemId']=$pid; 
       $_SESSION['cart']['itemId']['qty']= $_SESSION['cart']['itemId']['qty'] + $q; 
       $max=count($_SESSION['cart']); 
       echo "SECOND"; 
      } 
      else 
      { 
       $_SESSION['cart']=array(); 
       $_SESSION['cart'][0]['itemId']=$pid; 
       $_SESSION['cart'][0]['qty'] = $q; 
       $max=count($_SESSION['cart']); 
      } 
    } 

有什麼建議?

感謝

+0

什麼意思是不工作的第二個項目?它覆蓋了第一個項目嗎?或者只是不添加第二個項目? – SpringSteven 2013-02-21 18:47:11

回答

1

你超額付款授權它。

function addtocart($pid,$q) { 
    if($pid<1 or $q<1) return; 

    // has the main 'cart' array been set up in session yet? If not, do it 
    if (!array_key_exists('cart', $_SESSION) || !is_array($_SESSION['cart'])) 
     $_SESSION['cart']=array(); 

    // has this item been added to the cart yet? If not, do it (0 qty) 
    if (!array_key_exists($pid, $_SESSION['cart'])) { 
     $_SESSION['cart'][$pid] = array(
      'itemId'=>$pid, 
      'qty'=>0 
     ); 
    } 

    // add the requested quantity of items to the cart 
    $_SESSION['cart'][$pid]['qty'] = $_SESSION['cart'][$pid]['qty'] + $q; 
} 

第一次使用此函數添加項目時,它將在會話數組中設置正確的條目。此功能的任何後續使用添加相同的項目將增加qty密鑰。當一切都成定局,你就會有這樣的事情:

array(
    '54'=>array(
     'itemId'=>54, 
     'qty'=>4 
    ), 
    '99'=>array(
     'itemId'=>99, 
     'qty'=>2 
    ), 
) 

文檔

+0

什麼是產品id的多餘值的點是凱伊以及數組的子值? – Dutchie432 2013-02-21 18:56:55

+0

沒有技術原因;爲我未來的擴張做好準備。不知道用例,我不想偏離他在OP中建立的結構。它並沒有受到傷害,並且如果在某種情況下你只能看到這些值,那麼沒有上下文就沒有一個簡單的數字排列。 – 2013-02-21 18:58:30

+0

我會如何迴應'pid'和'qty'?我正在嘗試這個'$ pid = $ _ SESSION ['cart'] [$ i] ['itemId']​​;''和'$ q = $ _ SESSION ['cart'] [$ i] ['qty'];'但是他們顯示空... – samyb8 2013-02-21 20:03:57

0

你的功能可以簡化爲這樣的事情..

function addtocart($pid,$q) 
    { 
     if($pid<1 or $q<1) return; 

     //if the cart is not defined, define it 
     if (!is_array($_SESSION['cart'])) $_SESSION['cart']=array(); 

     //See if an arry item for the part number exists, or add it 
     if (!$_SESSION['cart'][$pid]) 
      $_SESSION['cart'][$pid]=$q; 
     else 
      $_SESSION['cart'][$pid]+=$q; 
    } 

再到後來讀它...

foreach($_SESSION['cart'] as $item=>$qty){ 
    echo $item.': '.$qty; 
} 

這是一個好方法,因爲如果添加同一件物品兩次,數量將加在一起

0

也許這可以幫助您。第二個索引應該是$max,而不是'itemId'

function addtocart($pid,$q) 
    { 
     if($pid<1 or $q<1) return; 
     if (is_array($_SESSION['cart'])) 
      { 
       $max=count($_SESSION['cart']); 
       $_SESSION['cart'][$max]['itemId']=$pid; 
       $_SESSION['cart'][$max]['qty']= $_SESSION['cart'][$max]['qty'] + $q; 
       $max=count($_SESSION['cart']); 
       echo "SECOND"; 
      } 
      else 
      { 
       $_SESSION['cart']=array(); 
       $_SESSION['cart'][0]['itemId']=$pid; 
       $_SESSION['cart'][0]['qty'] = $q; 
       $max=count($_SESSION['cart']); 
      } 
    }