Solved

Simplexml_load_file ForEach Loop Not Working

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

765 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