vendredi 9 janvier 2015

PHP Display Array Elements

I'm trying to have a PHP file format an XML file that is generated by my site then strip all the redundant information and output a CSV file. I have everything working fine except if there are multiple line-items only the first item is being submitted. Here's the XML and PHP I'm working with and I can't figure out why it's not looping with the foreach command.


XML



<line-items type="array">
<line-item>
<fulfillment-service>manual</fulfillment-service>
<fulfillment-status nil="true"/>
<gift-card type="boolean">false</gift-card>
<grams type="integer">5000</grams>
<id type="integer">56789</id>
<price type="decimal">199.99</price>
<product-id type="integer">123456</product-id>
<quantity type="integer">1</quantity>
<requires-shipping type="boolean">true</requires-shipping>
<sku>SKU2006-001</sku>
<taxable type="boolean">true</taxable>
<title>Sledgehammer</title>
<variant-id type="integer">654321</variant-id>
<variant-title nil="true"/>
<vendor nil="true"/>
<name>Sledgehammer</name>
<variant-inventory-management nil="true"/>
<properties type="array">
</properties>
<product-exists type="boolean">true</product-exists>
<fulfillable-quantity type="integer">1</fulfillable-quantity>
<tax-lines type="array"/>
</line-item>
<line-item>
<fulfillment-service>manual</fulfillment-service>
<fulfillment-status nil="true"/>
<gift-card type="boolean">false</gift-card>
<grams type="integer">500</grams>
<id type="integer">98765</id>
<price type="decimal">29.95</price>
<product-id type="integer">12345</product-id>
<quantity type="integer">1</quantity>
<requires-shipping type="boolean">true</requires-shipping>
<sku>SKU2006-020</sku>
<taxable type="boolean">true</taxable>
<title>Wire Cutter</title>
<variant-id type="integer">54321</variant-id>
<variant-title nil="true"/>
<vendor nil="true"/>
<name>Wire Cutter</name>
<variant-inventory-management nil="true"/>
<properties type="array">
</properties>
<product-exists type="boolean">true</product-exists>
<fulfillable-quantity type="integer">1</fulfillable-quantity>
<tax-lines type="array"/>
</line-item>


And the PHP to format it;



$line_items = $order->{'line-items'};
$line_item = $order->{'line-items'}->{'line-item'};
$count_line_item = count($line_item);
$item_sku = $order->{'line-items'}->{'line-item'}->{'sku'};
$item_qty = $order->{'line-items'}->{'line-item'}->{'quantity'};
$ship_to_email = $order->{'email'};
$item_price = $order->{'line-items'}->{'line-item'}->{'price'};

$item_array = array($line_item);

foreach($item_array as $line_item) {

$csv =
$order_number.','.
/*$order_contact_id.','.*/
$first_name.','.
$last_name.','.
$order_date.','.
$shipping_address1.','.
$shipping_address2.','.
$shipping_city.','.
$shipping_province.','.
$shipping_country.','.
$shipping_zip.','.
$shipping_phone.','.
$item_sku.','.
$item_qty.','.
$ship_to_email.','.
$item_price;

};


I'm looking to output something like this 1234,Steve,Shipper,2015-01-09 08:40:30,123 Shipping Street,,Shippington,KY,US,K2P0S0,555-555-SHIP,SKU2006-001,1,jon@doe.ca,199.99 but continuous if there's multiple items.


Thoughts?


Aucun commentaire:

Enregistrer un commentaire