graham23s Posted December 20, 2009 Share Posted December 20, 2009 Hi Guys, I am trying to show the user how many items they have in their shopping cart, i get the quantities from an amazon aws xml file, i tried: <?php $response = simplexml_load_file($url); foreach($response->Cart as $item) { $cartQY = $item->CartItems->CartItem->Quantity; $array = array($cartQY); } print count($array); ?> $cartQY = $item->CartItems->CartItem->Quantity; this reads each node and returns the quantity of each item in the cart, the way i have it now the foreach overwrites the quantity instead of adding to it, i'm not sure how to incrememnt the value. any help would be appreciated thanks guys Graham Link to comment https://forums.phpfreaks.com/topic/185775-incrementing-values/ Share on other sites More sharing options...
rajivgonsalves Posted December 20, 2009 Share Posted December 20, 2009 a sample of your xml file will help. Link to comment https://forums.phpfreaks.com/topic/185775-incrementing-values/#findComment-980933 Share on other sites More sharing options...
graham23s Posted December 20, 2009 Author Share Posted December 20, 2009 Hi Mate, Here it is: <?xml version="1.0" encoding="UTF-8" ?> - <CartGetResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2009-10-01"> - <OperationRequest> - <HTTPHeaders> <Header Name="UserAgent" Value="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SU 3.23; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)" /> </HTTPHeaders> <RequestId>0WQF79V8AA6Q5BDXZHQZ</RequestId> - <Arguments> <Argument Name="Service" Value="AWSECommerceService" /> <Argument Name="Operation" Value="CartGet" /> <Argument Name="Version" Value="2009-10-01" /> <Argument Name="Timestamp" Value="2009-12-20T13:47:59Z" /> <Argument Name="AssociateTag" Value="firschoishop-21" /> <Argument Name="Signature" Value="=" /> <Argument Name="HMAC" Value="" /> <Argument Name="CartId" Value="277-8907303-9128266" /> <Argument Name="AWSAccessKeyId" Value="AKIAI4FF5RJXRBLUF6FQ" /> </Arguments> <RequestProcessingTime>0.0379190444946289</RequestProcessingTime> </OperationRequest> - <Cart> - <Request> <IsValid>True</IsValid> - <CartGetRequest> <CartId>277-8907303-9128266</CartId> <HMAC>5ETnH+45f5IRoHePIEoyT2XcEiw=</HMAC> </CartGetRequest> </Request> <CartId>277-8907303-9128266</CartId> <HMAC>5ETnH+45f5IRoHePIEoyT2XcEiw=</HMAC> <URLEncodedHMAC>5ETnH%2B45f5IRoHePIEoyT2XcEiw%3D</URLEncodedHMAC> <PurchaseURL>https://www.amazon.co.uk/gp/cart/aws-merge.html?cart-id=277-8907303-9128266%26associate-id=firschoishop-21%26hmac=5ETnH%2B45f5IRoHePIEoyT2XcEiw=%26SubscriptionId=AKIAI4FF5RJXRBLUF6FQ%26MergeCart=False</PurchaseURL> - <SubTotal> <Amount>2156</Amount> <CurrencyCode>GBP</CurrencyCode> <FormattedPrice>£21.56</FormattedPrice> </SubTotal> - <CartItems> - <SubTotal> <Amount>2156</Amount> <CurrencyCode>GBP</CurrencyCode> <FormattedPrice>£21.56</FormattedPrice> </SubTotal> - <CartItem> <CartItemId>U3CJYU4J8670SU</CartItemId> <ASIN>B0027UY836</ASIN> <MerchantId>A3P5ROKL5A1OLE</MerchantId> <SellerId>A3P5ROKL5A1OLE</SellerId> <SellerNickname>Amazon.co.uk</SellerNickname> <Quantity>1</Quantity> <Title>The Haunting In Connecticut [DVD] [2009]</Title> <ProductGroup>DVD</ProductGroup> - <Price> <Amount>1258</Amount> <CurrencyCode>GBP</CurrencyCode> <FormattedPrice>£12.58</FormattedPrice> </Price> - <ItemTotal> <Amount>1258</Amount> <CurrencyCode>GBP</CurrencyCode> <FormattedPrice>£12.58</FormattedPrice> </ItemTotal> </CartItem> - <CartItem> <CartItemId>U1PTXH2CW08KEJ</CartItemId> <ASIN>B001PR1ZOC</ASIN> <MerchantId>A3P5ROKL5A1OLE</MerchantId> <SellerId>A3P5ROKL5A1OLE</SellerId> <SellerNickname>Amazon.co.uk</SellerNickname> <Quantity>1</Quantity> <Title>Fast & Furious [DVD] [2009]</Title> <ProductGroup>DVD</ProductGroup> - <Price> <Amount>898</Amount> <CurrencyCode>GBP</CurrencyCode> <FormattedPrice>£8.98</FormattedPrice> </Price> - <ItemTotal> <Amount>898</Amount> <CurrencyCode>GBP</CurrencyCode> <FormattedPrice>£8.98</FormattedPrice> </ItemTotal> </CartItem> </CartItems> </Cart> </CartGetResponse> I really just need to add up all the quantities. thanks mate Graham Link to comment https://forums.phpfreaks.com/topic/185775-incrementing-values/#findComment-980936 Share on other sites More sharing options...
Buddski Posted December 20, 2009 Share Posted December 20, 2009 If you only want the quantities why not something like this. <?php $response = simplexml_load_file($url); $count = 0; foreach($response->Cart as $item) { $count += $item->CartItems->CartItem->Quantity; } print $count; ?> or am I over simplifying it? Link to comment https://forums.phpfreaks.com/topic/185775-incrementing-values/#findComment-980937 Share on other sites More sharing options...
rajivgonsalves Posted December 20, 2009 Share Posted December 20, 2009 Yes something like Buddski said with some modifications <?php $response = simplexml_load_file($url); $count = 0; foreach($response->Cart->CartItems->CartItem as $item) { $count += $item->Quantity; } print $count; ?> Link to comment https://forums.phpfreaks.com/topic/185775-incrementing-values/#findComment-980943 Share on other sites More sharing options...
Buddski Posted December 20, 2009 Share Posted December 20, 2009 And because its better to learn where you went wrong than to just have the answer given.. The reason your code didnt work was because you were always overwriting $array with array($cartQY); $array[] = array($cartQY); would have been what you were after but using count() will just count how many items there are in the array and NOT the actual values in them Hope this helps ya a little more.. Link to comment https://forums.phpfreaks.com/topic/185775-incrementing-values/#findComment-980948 Share on other sites More sharing options...
graham23s Posted December 20, 2009 Author Share Posted December 20, 2009 Hi Guys, Thanks a ton Graham Link to comment https://forums.phpfreaks.com/topic/185775-incrementing-values/#findComment-980953 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.