Solved

Simplexml_load_file ForEach Loop Not Working

Posted on 2011-09-07
3
618 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
  • 3
3 Comments
 

Author Comment

by:itadminnek
Comment Utility
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
Comment Utility
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
Comment Utility
Solved it by myself
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now