?
Solved

Simplexml_load_file ForEach Loop Not Working

Posted on 2011-09-07
3
Medium Priority
?
626 Views
Last Modified: 2012-06-22
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."','".$order->ShippingName."')");
}

mysql_close($link);
exit(0);


XML FILE

  <?xml version="1.0" encoding="UTF-8" ?>
- <GoECartAPIDocList serial-number="10001">
  <DocVersion>1.1</DocVersion>
- <APIDoc>
  <APIType>OrderReport</APIType>
  <TimeStamp>9/7/2011 10:02:40 AM</TimeStamp>
- <User>
  <UserName>APIUser</UserName>
  </User>
- <APIStatusDoc>
  <StatusCode>001</StatusCode>
- <StatusDetails>
- <![CDATA[ Response from API is success ]]>
  </StatusDetails>
  </APIStatusDoc>
- <APIResponseDoc>
- <Orders>
- <Order>
  <OrderId>1001</OrderId>
  <CustomerId>1</CustomerId>
  <CustomerFirstName>GoECart</CustomerFirstName>
  <CustomerLastName>Support</CustomerLastName>
  <CustomerPhone />
  <CustomerEmail>support@goecart.com</CustomerEmail>
  <CustomerCompanyName />
  <BillingStreet1>10 Middle Street</BillingStreet1>
  <BillingStreet2 />
  <BillingCity>Bridgeport</BillingCity>
  <BillingState>CT</BillingState>
  <BillingZip>06604</BillingZip>
  <BillingCountry>United States</BillingCountry>
  <PaymentType>Purchase Order</PaymentType>
  <ShippingSameAsBilling>FALSE</ShippingSameAsBilling>
  <ShippingName>USPS Priority Mail</ShippingName>
  <ShippingFirstName>GoECart</ShippingFirstName>
  <ShippingLastName>Support</ShippingLastName>
  <ShippingCompanyName />
  <ShippingStreet1>10 Middle Street</ShippingStreet1>
  <ShippingStreet2 />
  <ShippingCity>Bridgeport</ShippingCity>
  <ShippingState>CT</ShippingState>
  <ShippingZip>06604</ShippingZip>
  <ShippingCountry>United States</ShippingCountry>
  <OrderStatus>New</OrderStatus>
  <OrderDateTime>1/24/2011 12:38:00 PM</OrderDateTime>
  <OrderNotes>test order</OrderNotes>
  <OrderTax>5.25</OrderTax>
  <OrderShippingCost>8.00</OrderShippingCost>
  <OrderSubtotal>87.47</OrderSubtotal>
  <QuantityDiscounts>0.00</QuantityDiscounts>
  <OrderTotal>100.72</OrderTotal>
  <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</DiscountAmount>
  <ShipDate />
  <DiscountCookie>FALSE</DiscountCookie>
  <GiftWrapAmount>0</GiftWrapAmount>
  <GiftWrapMessage />
  <PaymentSurchargePercent>0.00</PaymentSurchargePercent>
  <TaxSource>System Tax</TaxSource>
  <CookieDiscountCode />
  <DiscountAdminNotes />
  <UserIPAddress>123.252.205.160</UserIPAddress>
  <SiteReferrer />
  <OrderFinalized>1</OrderFinalized>
  <OrderType>Online</OrderType>
  <GiftCertificateAmountRedeemed>0</GiftCertificateAmountRedeemed>
  <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>
0
Comment
Question by:itadminnek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 

Author Comment

by:itadminnek
ID: 36497627
I have found out that it has something to do with either the xml file itself or my structure of my foreach loop...testing a different xml file and php script proved that what I am trying to do will work with my system but when I replace the test xml file with the xml file I am working with and change the foreach loop accordingly it stops working. Please help!
0
 

Accepted Solution

by:
itadminnek earned 0 total points
ID: 36497921
Solved! For some reason I had to leave out the first node of the xml file in the foreach loop to get it to work, not sure why though, but as long as its working. Here is my code after my discovery for future reference:

<?php
$xml = simplexml_load_file("results.xml");

$link = mysql_connect('ipAddress', 'username', 'password');
if (!$link) {die('Could not connect: ' . mysql_error());}
echo 'Connected successfully';

mysql_select_db("test", $link);


foreach($xml->APIDoc->APIResponseDoc->Orders->Order as $order)
{
mysql_query("INSERT INTO jake(test2, test3) Values ('".$order->OrderTotal."','".$order->ShippingFirstName."')");
}

mysql_close($link);
exit(0);
?>
0
 

Author Closing Comment

by:itadminnek
ID: 36497942
Solved it by myself
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question