XML processing

stargateatlantis
stargateatlantis used Ask the Experts™
on
If you go to the following URL here

http://www.romancart.com/help/xmlcallback.xml

There is order-items node. Now within order-items you can have ten items or more in it.  I need a loop that will put the items into a string
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014
Commented:
>I need a loop that will put the items into a string

Could you give an example ? One string by items ?
Try the attached code (below). The only tricky bit is that your XML names contain dashes (like order-item) and PHP does not allow dashes in variable names so any such name has to be bracketted up with {''}. Names without dashes are ok and will convert automatically.

Like Leakim971, I am not sure how you want four items shoehorned into one string, but hopefully the code is clear enough that you can adapt it easily.
$xml = simplexml_load_file( "http://www.romancart.com/help/xmlcallback.xml" );

     $result = "";

     foreach( $xml->{'order-items'} as $anOrderItem ) {

          // Convert items to string
          //
          $result .= (string) $anOrderItem->{'order-item'}->{'item-code'} . " " . 
                     (string) $anOrderItem->{'order-item'}->{'item-name'} . " " .
                     (string) $anOrderItem->{'order-item'}->price . " " .
                     (string) $anOrderItem->{'order-item'}->quantity . "<br>";
     } 

     echo $result;

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
Interesting... Can you tell us why you want to put the items into a string?  It does not match any design pattern I am aware of.  An array, an object, a data base, any of those would make sense, but a string of items is an unusual construct.  Maybe if we understand your application needs we can suggest a better approach.  Best, ~Ray

Author

Commented:
the end result would be to process the elements in a loop and create a sql statement with it.
In that case, something along these lines (UNTESTED) ......
$xml = simplexml_load_file( "http://www.romancart.com/help/xmlcallback.xml" );


     foreach( $xml->{'order-items'} as $anOrderItem ) {

          $code  = mysql_real_escape_string( (string) $anOrderItem->{'order-item'}->{'item-code'} );
          $name  = mysql_real_escape_string( (string) $anOrderItem->{'order-item'}->{'item-name'} );
          $price = mysql_real_escape_string( (string) $anOrderItem->{'order-item'}->price );
          $quant = mysql_real_escape_string( (string) $anOrderItem->{'order-item'}->quantity );

          $sql = "INSERT INTO aTable 
                         ( orderItem, itemName, price, quantity )
                         VALUES ( '$code', '$name', '$price', '$quant' ) ";

          mysql_query( $sql );
     }

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
"to process the elements in a loop"

That would seem to suggest an array, right?  It is an iterator pattern.

Author

Commented:
what i was going to do is build one big insert statement then insert into the database but the other solution on the top is good as well

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial