Jump to content

Foreach loop


graham23s

Recommended Posts

Hi Guys,

 

I use this code below to query amazon's api and display remote shopping carts:

 

<?php

  // Check if the customer has an amazon order in the database, if so show it
  if (amazon_cart_not_empty($cusSessionID))
  {
   
   // If the cart isn't empty retrieve the cartID and HAC ID
   $cartID = amazon_unique_cart_id($cusSessionID);
   $cartHI = amazon_unique_hwac($cusSessionID);
   
   // Display top half
   print '<form onSubmit="return updateCartCheck();" action="cart.php?do=update" name="fcsShoppingCart" method="POST" />
           <input type="hidden" name="cusSessionID" value="" />
          <table border="1" class="CartContents Stylize General" cellspacing="0" cellpadding="0">
	         <thead>
			    <tr>
					<th colspan="2">Cart Items</th>
					<th style="text-align: center; white-space: nowrap;">Qty</th>
					<th style="text-align: center; white-space: nowrap;">Item Price</th>
					<th style="text-align: right; white-space: nowrap;">Item Total</th>
				 </tr>
			    </thead>
	      <tbody>
		  <tr>';
  
       // Credentials
      $PublicKey   = "xxxxxxxxxxxxx";
      $PrivateKey  = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";

      // Prarameters we need to send to the function
      $parameters = array(
      'AssociateTag'    => 'firschoishop-21',
      'Operation'       => 'CartGet',
      'CartId'          => $cartID,   
      'HMAC'            => $cartHI,   
      );

      // Execute the function to return our URL
      $url = amazon_signed_request("co.uk", $parameters, $PublicKey, $PrivateKey);
      print $url;
      // Load the results and get the contents    
      $response = simplexml_load_file($url);
      
       
      // Loop through the xml nodes
      foreach ($response->Cart as $item) 
      {     
      
        // Whats our cart id?
        $cartID = $item->CartId;
        $cartNM = $item->CartItems->CartItem->Title;
        $cartQY = $item->CartItems->CartItem->Quantity;
        $cartPR = $item->SubTotal->FormattedPrice;
        
        // What product group
        $cartPG = $item->CartItems->CartItem->ProductGroup;
        print $cartPG;
        // We need to use the function to determine what item number we need
        if ($cartPG == "Book"){
           $identificationNumber = $item->CartItems->CartItem->ASIN;
        } elseif($cartPG == "DVD"){
           $identificationNumber = $item->ItemAttributes->ISBN;
        }
      
        print '<td class="CartThumb">';
        print ' <img src="imgProducts/img-th/thumb-Eczema-Free-Forever-1250421912.jpg" border=\"0\"/></a>';
      print '  </td>';
      print '	 <td class="ProductName" colspan="1">';
      print ' 	'.amazon_friendly_url($cartNM, $identificationNumber).' <b>- </b>(<a href=\"cart.php?do=remove&product-id=$cartProdID&customer-id=$cusSessionID\" onclick=\"return confirm(\'Are you sure you want to delete this item?\')\"><span class=\"fcs-message-intro-w\">X</span></a>)';
        print '	  <br />';
      print '   </td>';
      print '	    </td>';			
	    print '<td align="center">'.$cartQY.'</td>';
	    print '<td align="right"><em class="ProductPrice">'.$cartPR.'</em></td>';
      print '</tr>';

      }  
    
        // Display bottom
        print '<table border="0" cellpadding="5" cellspacing="0" />';
        print ' <tr>';
        print '  <td class="fcs-proceed-checkout-border" align="right" colspan="2"><input type="image" src="img/img-update-cart.png" alt="Update Cart" /></span><a href="checkout.php" onClick="return cartCheck()" title="Click here to proceed to checkout"><img src="img/img-proceed-checkout.png" alt="Click here to proceed to checkout" border="0" /></a></td>';
        print ' </tr>';
        print '</table>';
    
   // We don't want the script to execute any further
   include("inc/inc-footer.php");
   exit;
   
   } 

?>

 

This works great, when i test the xml in the browser it shows up 2 products in the cart, but only 1 is showing up in my cart on site.

 

the foreach here:

 

      // Loop through the xml nodes
      foreach ($response->Cart as $item) 
      {     
      
        // Whats our cart id?
        $cartID = $item->CartId;
        $cartNM = $item->CartItems->CartItem->Title;
        $cartQY = $item->CartItems->CartItem->Quantity;
        $cartPR = $item->SubTotal->FormattedPrice;
        
        // What product group
        $cartPG = $item->CartItems->CartItem->ProductGroup;
        print $cartPG;
        // We need to use the function to determine what item number we need
        if ($cartPG == "Book"){
           $identificationNumber = $item->CartItems->CartItem->ASIN;
        } elseif($cartPG == "DVD"){
           $identificationNumber = $item->ItemAttributes->ISBN;
        }
      
        print '<td class="CartThumb">';
        print ' <img src="imgProducts/img-th/thumb-Eczema-Free-Forever-1250421912.jpg" border=\"0\"/></a>';
      print '  </td>';
      print '	 <td class="ProductName" colspan="1">';
      print ' 	'.amazon_friendly_url($cartNM, $identificationNumber).' <b>- </b>(<a href=\"cart.php?do=remove&product-id=$cartProdID&customer-id=$cusSessionID\" onclick=\"return confirm(\'Are you sure you want to delete this item?\')\"><span class=\"fcs-message-intro-w\">X</span></a>)';
        print '	  <br />';
      print '   </td>';
      print '	    </td>';			
	    print '<td align="center">'.$cartQY.'</td>';
	    print '<td align="right"><em class="ProductPrice">'.$cartPR.'</em></td>';
      print '</tr>';

      }  

 

seems to only loop though table html once, waht would be the best way to loop the other table out?

 

thanks guys

 

Graham

 

 

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.