2011-12-05 3 views
0

カートの情報を保持するために配列内の構造体を使用しているショッピングカートを現在構築中です。製品が既にカートに追加されていてカートに追加ボタンが再度押された場合は、構造体の数量を更新し、別の商品をカートに追加しないでください。カートに追加ボタンが押されると、まずアレイが空であるかどうかをチェックし、そうでなければ、フォームによって提出された製品IDを探してアレイをループします。 、次に変数addNew = noを設定します。製品が見つからない場合はcfelseを使用しましたが、変数addNew = yesを設定しました。カートが複数の製品を持っている場合、私の問題は何かを理解しています。ループは継続しています。明らかに製品IDが見つからず、変数addNew = newを設定しています。更新数量が新しい構造体を追加しない

このように配列と構造体を使用するのは初めてで、私のコードが正しいかどうかはわかりますが、新しい構造体に製品を追加してください。効率的ではない私は謝罪する。どのポインタも大いに感謝しています。

<cfif arrayLen(session.mycart) GT 0> 
    <cfloop index="i" from="1" to="#arrayLen(session.mycart)#"> 
     <!---check for existance of the id submitted---> 
     <cfif session.mycart[i].itemID eq form.itemID> 
     <!---if the id is matched update the quantity---> 
      <cfset session.mycart[i].quantity = form.quantity+session.mycart[i].quantity> 
      <cfset myTotal = form.itemCost*session.mycart[i].quantity> 
      <cfset session.mycart[i].totalPrice = myTotal> 
      <!---this will tell the add to cart function not add a new item---> 
      <cfset addNew = "no"> 
     <cfelse> 
     <!---as this is a new item tell the add to cart function to add it---> 
      <cfset addNew ="yes"> 
     </cfif> 
    </cfloop> 
<cfelse> 
<!---as the array is empty tel the add to cart function insert the product---> 
    <cfset addNew ="yes"> 
</cfif> 

答えて

3

私には2つのことが考えられます。

まず、配列の外側の長さチェックを取り除くことで、コードを単純化することができます。ループはそれをカバーします。

第2に、数量(ループ内のIFの真部分)を見つけて更新すると、その時点でアイテムの検索が停止されます。あなたがすでに見つけたものを探してアレイの残りの部分を続けていくことには意味がありません。その時点でCFBREAKを使用してループを終了します。

覚えておくべきもう1つのことは、まだ設定されていない場合に限り、CFPARAMを使用して変数を設定できることです。したがって、addNewをtrueに設定した後でCFPARAMを呼び出すと、その変数はそのままにします。しかし、この場合、CFBREAKの方が良いです。

+0

ありがとうございましたアダム、私はブレークメソッドを使用し、それは完全に動作します。 –

1

あなたの必要とするサウンドは、カート内の各アイテムのブール値です。既存の構造に追加することもできます。また、ColdFusionではブール値として "yes"と "no"という文字列を使用できますが、代わりにtrueとfalseを使用する方が良いと思います。

 <cfif session.mycart[i].itemID eq form.itemID> 
     <!---if the id is matched update the quantity---> 
      <cfset session.mycart[i].quantity = form.quantity+session.mycart[i].quantity> 
      <cfset myTotal = form.itemCost*session.mycart[i].quantity> 
      <cfset session.mycart[i].totalPrice = myTotal> 
      <!---this will tell the add to cart function not add a new item---> 
      <cfset session.mycart[i].addNew = FALSE> 
     <cfelse> 
     <!---as this is a new item tell the add to cart function to add it---> 
      <cfset session.mycart[i].addNew =TRUE> 
     </cfif> 
+0

返信いただきありがとうございます。私はポップアウトしなければなりませんでした。私が戻ったらすぐにあなたの治療法を試みます!どうもありがとう。 –

2

最初にフラグをtrueに設定してから、ループしたときに製品が見つかった場合はフラグをfalseに設定するだけです。

<!--- first set the flag to add item to cart ---> 
<cfset addNew = true> 

<cfloop index="i" from="1" to="#arrayLen(session.mycart)#"> 
    <!---check for existance of the id submitted---> 
    <cfif session.mycart[i].itemID eq form.itemID> 
    <!---if the id is matched update the quantity---> 
     <cfset session.mycart[i].quantity = form.quantity+session.mycart[i].quantity> 
     <cfset myTotal = form.itemCost*session.mycart[i].quantity> 
     <cfset session.mycart[i].totalPrice = myTotal> 

     <!---if the item is already in the cart, tell the add to cart function not add a new item---> 
     <cfset addNew = false> 
    </cfif> 
</cfloop> 
+0

ありがとうございました!私はbreakメソッドを使用し、変数を真に設定しました。それは完璧に動作します。 –

関連する問題