itadminnek
asked on
Simplexml_load_file ForEach Loop Not Working
I am trying to load an xml file into php, pulls informationfrom it, and store that information into my MySQL database. The following code works for me up until the foreach loop...if I take that out and put in a Insert by itself, the Insert works fine and the data gets put in my database. My thought is that there is something wrong with the foreach loop but everything I have found on the web suggest that my code is correct. Could someone take a look and let me know if I have everything correct. When I run the code it gets to the foreach its like it just skips over the foreach loop. Below is my code and the xml file and am pulling from. Let me know if you need anymore information.
MY CODE
file_put_contents('results .xml', $result);
$url = "xml->results.xml";
$xml = simplexml_load_file($url);
$link = mysql_connect('ipAddress', 'username', 'password');
if (!$link) {die('Could not connect: ' . mysql_error());}
echo 'Connected successfully';
mysql_select_db("test", $link);
foreach($xml->Order as $order)
{
echo("INSERT INTO jake(test2, test3) Values ('".$order->ShippingName." ','".$orde r->Shippin gName."')" );
}
mysql_close($link);
exit(0);
XML FILE
<?xml version="1.0" encoding="UTF-8" ?>
- <GoECartAPIDocList serial-number="10001">
<DocVersion>1.1</DocVersio n>
- <APIDoc>
<APIType>OrderReport</APIT ype>
<TimeStamp>9/7/2011 10:02:40 AM</TimeStamp>
- <User>
<UserName>APIUser</UserNam e>
</User>
- <APIStatusDoc>
<StatusCode>001</StatusCod e>
- <StatusDetails>
- <![CDATA[ Response from API is success ]]>
</StatusDetails>
</APIStatusDoc>
- <APIResponseDoc>
- <Orders>
- <Order>
<OrderId>1001</OrderId>
<CustomerId>1</CustomerId>
<CustomerFirstName>GoECart </Customer FirstName>
<CustomerLastName>Support< /CustomerL astName>
<CustomerPhone />
<CustomerEmail>support@goe cart.com</ CustomerEm ail>
<CustomerCompanyName />
<BillingStreet1>10 Middle Street</BillingStreet1>
<BillingStreet2 />
<BillingCity>Bridgeport</B illingCity >
<BillingState>CT</BillingS tate>
<BillingZip>06604</Billing Zip>
<BillingCountry>United States</BillingCountry>
<PaymentType>Purchase Order</PaymentType>
<ShippingSameAsBilling>FAL SE</Shippi ngSameAsBi lling>
<ShippingName>USPS Priority Mail</ShippingName>
<ShippingFirstName>GoECart </Shipping FirstName>
<ShippingLastName>Support< /ShippingL astName>
<ShippingCompanyName />
<ShippingStreet1>10 Middle Street</ShippingStreet1>
<ShippingStreet2 />
<ShippingCity>Bridgeport</ ShippingCi ty>
<ShippingState>CT</Shippin gState>
<ShippingZip>06604</Shippi ngZip>
<ShippingCountry>United States</ShippingCountry>
<OrderStatus>New</OrderSta tus>
<OrderDateTime>1/24/2011 12:38:00 PM</OrderDateTime>
<OrderNotes>test order</OrderNotes>
<OrderTax>5.25</OrderTax>
<OrderShippingCost>8.00</O rderShippi ngCost>
<OrderSubtotal>87.47</Orde rSubtotal>
<QuantityDiscounts>0.00</Q uantityDis counts>
<OrderTotal>100.72</OrderT otal>
<OrderTrackingNumber />
<AdminNotes>(TESTMODE) The merchant login ID or password is invalid or the account is inactive.. AVS not applicable for this transaction</AdminNotes>
<DiscountName />
<DiscountCode />
<DiscountAmount>0.00</Disc ountAmount >
<ShipDate />
<DiscountCookie>FALSE</Dis countCooki e>
<GiftWrapAmount>0</GiftWra pAmount>
<GiftWrapMessage />
<PaymentSurchargePercent>0 .00</Payme ntSurcharg ePercent>
<TaxSource>System Tax</TaxSource>
<CookieDiscountCode />
<DiscountAdminNotes />
<UserIPAddress>123.252.205 .160</User IPAddress>
<SiteReferrer />
<OrderFinalized>1</OrderFi nalized>
<OrderType>Online</OrderTy pe>
<GiftCertificateAmountRede emed>0</Gi ftCertific ateAmountR edeemed>
<FraudScoreAlertDismissed />
<FraudRiskScore />
- <Items>
- <Item OrderItemId="5">
<MfgName>Did you know - Manufacturer Name</MfgName>
<ProductName>Did you know? - Product Name</ProductName>
<Price>10.00</Price>
<Weight>1.00</Weight>
<Quantity>1</Quantity>
<MfgSku />
<Sku>didyouknow</Sku>
<Status>New</Status>
<ShipDate />
<TrackingNumber />
<Notes />
<AdminNotes />
- <Personalization>
- <![CDATA[
]]>
</Personalization>
</Item>
- <Item OrderItemId="6">
<MfgName />
<ProductName>Girls Hipster Boot-Cut Jeans</ProductName>
<Price>19.49</Price>
<Weight>1.00</Weight>
<Quantity>1</Quantity>
<MfgSku />
<Sku>girl-jean</Sku>
<Status>New</Status>
<ShipDate />
<TrackingNumber />
<Notes />
<AdminNotes />
- <Personalization>
- <![CDATA[
]]>
</Personalization>
</Item>
- <Item OrderItemId="7">
<MfgName />
<ProductName>Did you know - Product Group item 1</ProductName>
<Price>19.00</Price>
<Weight>1.00</Weight>
<Quantity>1</Quantity>
<MfgSku />
<Sku>PGSKU001-1</Sku>
<Status>New</Status>
<ShipDate />
<TrackingNumber />
<Notes />
<AdminNotes />
- <Personalization>
- <![CDATA[
]]>
</Personalization>
</Item>
- <Item OrderItemId="8">
<MfgName />
<ProductName>Girls Velveteen Peacoats</ProductName>
<Price>19.49</Price>
<Weight>1.00</Weight>
<Quantity>2</Quantity>
<MfgSku />
<Sku>girl-vpcoat</Sku>
<Status>New</Status>
<ShipDate />
<TrackingNumber />
<Notes />
<AdminNotes />
- <Personalization>
- <![CDATA[
]]>
</Personalization>
</Item>
</Items>
</Order>
</Orders>
</APIResponseDoc>
</APIDoc>
</GoECartAPIDocList>
MY CODE
file_put_contents('results
$url = "xml->results.xml";
$xml = simplexml_load_file($url);
$link = mysql_connect('ipAddress',
if (!$link) {die('Could not connect: ' . mysql_error());}
echo 'Connected successfully';
mysql_select_db("test", $link);
foreach($xml->Order as $order)
{
echo("INSERT INTO jake(test2, test3) Values ('".$order->ShippingName."
}
mysql_close($link);
exit(0);
XML FILE
<?xml version="1.0" encoding="UTF-8" ?>
- <GoECartAPIDocList serial-number="10001">
<DocVersion>1.1</DocVersio
- <APIDoc>
<APIType>OrderReport</APIT
<TimeStamp>9/7/2011 10:02:40 AM</TimeStamp>
- <User>
<UserName>APIUser</UserNam
</User>
- <APIStatusDoc>
<StatusCode>001</StatusCod
- <StatusDetails>
- <![CDATA[ Response from API is success ]]>
</StatusDetails>
</APIStatusDoc>
- <APIResponseDoc>
- <Orders>
- <Order>
<OrderId>1001</OrderId>
<CustomerId>1</CustomerId>
<CustomerFirstName>GoECart
<CustomerLastName>Support<
<CustomerPhone />
<CustomerEmail>support@goe
<CustomerCompanyName />
<BillingStreet1>10 Middle Street</BillingStreet1>
<BillingStreet2 />
<BillingCity>Bridgeport</B
<BillingState>CT</BillingS
<BillingZip>06604</Billing
<BillingCountry>United States</BillingCountry>
<PaymentType>Purchase Order</PaymentType>
<ShippingSameAsBilling>FAL
<ShippingName>USPS Priority Mail</ShippingName>
<ShippingFirstName>GoECart
<ShippingLastName>Support<
<ShippingCompanyName />
<ShippingStreet1>10 Middle Street</ShippingStreet1>
<ShippingStreet2 />
<ShippingCity>Bridgeport</
<ShippingState>CT</Shippin
<ShippingZip>06604</Shippi
<ShippingCountry>United States</ShippingCountry>
<OrderStatus>New</OrderSta
<OrderDateTime>1/24/2011 12:38:00 PM</OrderDateTime>
<OrderNotes>test order</OrderNotes>
<OrderTax>5.25</OrderTax>
<OrderShippingCost>8.00</O
<OrderSubtotal>87.47</Orde
<QuantityDiscounts>0.00</Q
<OrderTotal>100.72</OrderT
<OrderTrackingNumber />
<AdminNotes>(TESTMODE) The merchant login ID or password is invalid or the account is inactive.. AVS not applicable for this transaction</AdminNotes>
<DiscountName />
<DiscountCode />
<DiscountAmount>0.00</Disc
<ShipDate />
<DiscountCookie>FALSE</Dis
<GiftWrapAmount>0</GiftWra
<GiftWrapMessage />
<PaymentSurchargePercent>0
<TaxSource>System Tax</TaxSource>
<CookieDiscountCode />
<DiscountAdminNotes />
<UserIPAddress>123.252.205
<SiteReferrer />
<OrderFinalized>1</OrderFi
<OrderType>Online</OrderTy
<GiftCertificateAmountRede
<FraudScoreAlertDismissed />
<FraudRiskScore />
- <Items>
- <Item OrderItemId="5">
<MfgName>Did you know - Manufacturer Name</MfgName>
<ProductName>Did you know? - Product Name</ProductName>
<Price>10.00</Price>
<Weight>1.00</Weight>
<Quantity>1</Quantity>
<MfgSku />
<Sku>didyouknow</Sku>
<Status>New</Status>
<ShipDate />
<TrackingNumber />
<Notes />
<AdminNotes />
- <Personalization>
- <![CDATA[
]]>
</Personalization>
</Item>
- <Item OrderItemId="6">
<MfgName />
<ProductName>Girls Hipster Boot-Cut Jeans</ProductName>
<Price>19.49</Price>
<Weight>1.00</Weight>
<Quantity>1</Quantity>
<MfgSku />
<Sku>girl-jean</Sku>
<Status>New</Status>
<ShipDate />
<TrackingNumber />
<Notes />
<AdminNotes />
- <Personalization>
- <![CDATA[
]]>
</Personalization>
</Item>
- <Item OrderItemId="7">
<MfgName />
<ProductName>Did you know - Product Group item 1</ProductName>
<Price>19.00</Price>
<Weight>1.00</Weight>
<Quantity>1</Quantity>
<MfgSku />
<Sku>PGSKU001-1</Sku>
<Status>New</Status>
<ShipDate />
<TrackingNumber />
<Notes />
<AdminNotes />
- <Personalization>
- <![CDATA[
]]>
</Personalization>
</Item>
- <Item OrderItemId="8">
<MfgName />
<ProductName>Girls Velveteen Peacoats</ProductName>
<Price>19.49</Price>
<Weight>1.00</Weight>
<Quantity>2</Quantity>
<MfgSku />
<Sku>girl-vpcoat</Sku>
<Status>New</Status>
<ShipDate />
<TrackingNumber />
<Notes />
<AdminNotes />
- <Personalization>
- <![CDATA[
]]>
</Personalization>
</Item>
</Items>
</Order>
</Orders>
</APIResponseDoc>
</APIDoc>
</GoECartAPIDocList>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Solved it by myself
ASKER