Jump to content

Incrementing values


graham23s

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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 :P Hope this helps ya a little more..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.