Solved

Simplexml_load_file ForEach Loop Not Working

Posted on 2011-09-07
3
619 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
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL, Updating Statistics take too long for 60GB database 5 104
php mysql if then statement syntax 4 36
php image upload 3 37
WPF - Tooltips for ComboBox items 5 40
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

863 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

22 Experts available now in Live!

Get 1:1 Help Now