Solved

Simplexml_load_file ForEach Loop Not Working

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Work with App store 7 94
Upgrade MySQL 5.5 to MySQL 5.6 on Windows 13 80
How to extract database info from current month and year 7 34
restriction of entering a a page 5 34
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

733 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