2012-01-30 72 views
0

我正在尋找一些關於我的購物車的幫助。我使用一個簡單的腳本。一切都按我想要的方式工作,但我不知道如何確定某些產品是否已經存在,如果他們確實想增加價值。下面的腳本是:如何查看產品是否存在,如果是+數值

for ($x=0;$x<count($_SESSION["sess_name"]);$x++) { 
    $x_id= $_SESSION["sess_id"]; 
    $x_name_prd= $_SESSION["sess_name_prd"]; 
    $x_number_prd= $_SESSION["sess_number_prd"]; 

    //if there is a product that is already in the cart then go to function check_me(); to do something to make the sess_number_prd +1 everytimes people click on the button. 
    if ($x_name_prd[$x]==$name_prd){ 
     check_me($x_name_prd[$x]); 
    } 
} 

我已經試過這種方法,但不工作

function check_me ($name_prd) { 
    for ($i=0;$i<count($_SESSION['sess_name']);$i++) { 
     if (!in_array($_SESSION['sess_id'][$i],$)) { 
      $temp_id[]=$_SESSION['sess_id'][$i]; 
      $temp_name[]=$_SESSION['sess_name_prd'][$i]; 

      $temp_num[]=+1; 
     } 
    } 
    $_SESSION['sess_id']=$temp_id; 
    $_SESSION['number_prd']=+1; 
    $_SESSION['sess_name']=$temp_name; 
} 

有人可以幫我請..

回答

0

如果你已經有些控制權存儲在$ _SESSION中的數組的結構爲您的購物籃我建議使用這樣的東西。

/*Entry point 
    It is assumed at this point you have the value product id ($pid) and initiated 
    your $_SEESION[<basket array index>] set - "basket" in this example 
*/ 
(isset($_SESSION['basket'][$pid]))? $_SESSSION['basket'][$pid]++ : $_SESSION['basket'][$pid] = 1; 

這種方法不需要在籃下每個項目的反覆比較,以確定它的一個實例已經被添加到籃下。這將允許一個簡單的

foreach($_SESSION['basket'] as $product_id => $noOfProduct){ 
    /*Do your checkout thing here*/ 
} 

一旦你完成添加到它處理籃。

代碼由Alex在發表的評論,例如這個問題不解決的

for ($x=0;$x<count($_SESSION["sess_id"]);$x++){ 
    $x_id= $_SESSION["sess_id"][$x]; //[$x] appended, presumed missing 
    $x_id_prd= $_SESSION["sess_id_prd"]; 
    $x_name_prd= $_SESSION["sess_name"]; 
    $x_size_prd= $_SESSION["sess_size"]; 
    $x_color_prd= $_SESSION["sess_color"]; 
    $x_clr_code_prd= $_SESSION["sess_clr_code"]; 
    $x_num_prd= $_SESSION["sess_num"]; 
    if($x_id_prd[$x]==$id_prd 
     && $x_size_prd[$x]==$size_prd 
     && $x_color_prd[$x]==$color_prd 
     && $x_clr_code_prd[$x]==$clr_code){ 
     //do something to get $_SESSION["sess_num"]+1; 
    } 
} 

末的註釋代碼

+0

等一下,我會嘗試 – Alex 2012-01-30 19:18:56

+0

我不覺得這個工作我,因爲我有另一種方法來生成$ pid ...並且一切正常。 (原因是我在一個產品中有很多選擇:尺寸,顏色等)我也可以找出哪些產品已經存在於cookie中,但是我不知道如何在corect文件夾中使其+1。看起來像腳本不知道該放哪裏..:S – Alex 2012-01-30 19:24:47

+0

這裏是一些代碼: for($ x = 0; $ x Alex 2012-01-30 21:05:34

相關問題