Link to home
Start Free TrialLog in
Avatar of doctorbill
doctorbillFlag for United Kingdom of Great Britain and Northern Ireland

asked on

xml and php

I am trying to import the attached xml file into a mysql database using php
The problem is that there are records that are repeated in the file

Can someone please give me some pointers please
Purchase-Order-FI2-4098098.xml
SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of doctorbill

ASKER

I haven't started that yet.
I am in the process of setting this up
How does the php code take the xml data and place it in the database? Can you show me using a dummy script so I get the just of the workings
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is the data I need inserting:
[body] => SimpleXMLElement Object
        (
            [ORDER] => SimpleXMLElement Object
                (
                    [ORDERHEAD] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [ORDERTYPE] => PUO
                                    [FUNCCODE] => FIO
                                )

                            [ORDERDESC] => Purchase Order
                            [FUNCDESC] => Firm Order
                            [CURRENCY] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [CURRTYPE] => ORD
                                            [CURRCODE] => GBP
                                        )

                                    [CURRTYPEDESC] => Order Currency
                                    [CURRCODESC] => United Kingdom Pounds
                                )

                        )

                    [REFERENCE] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [REFTYPE] => CUR
                                        )

                                    [REFDESC] => Customers Reference
                                    [REFCODE] => STKPO434968
                                )

                            [1] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [REFTYPE] => CCN
                                        )

                                    [REFDESC] => Customers Customer Name
                                    [REFCODE] => Mrs V. M BAKER
                                )

                            [2] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [REFTYPE] => ACR
                                        )

                                    [REFDESC] => Alternate Customer Reference
                                    [REFCODE] => 001A002130
                                )

                        )

My table is just consisting of col1,col2 etc...
It is called orders_db
database: postorders

Sorry to appear so vague but this is totally new to me. If I can have a pointer as to the structure of the php code to do this I could then work on it. Is it possible to put the code (above) together to show me how it all works as a unit ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ray - thanks so much for the code - it really explains exactly what is going on
Final question:
If my database has a name "dbname" and columns called "col1", "col2" etc ..., what would the insert php code look like to take the above echoed variables into the databse
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
basically how to use a sql INSERT statement to put the variables into a database
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Last question I promise:
How would the $r variables be inserted as repeats into the table  on line 22-23?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is my attempt to get 2 variables into the database (sorry - I know it is one more question):
-------------------------------
<?php
//Connect to the database
$dbh = new PDO("mysql:host=localhost;dbname=1callorders", "name", "password");

error_reporting(E_ALL);
echo '<pre>';

// ACQUIRE THE XML AND MAKE AN OBJECT
$url = 'http://localhost/comwebsites/1callorders/site/xmlimport/Purchase-Order-FI2-4098098.xml';
$xml = file_get_contents($url);
$obj = simplexml_load_string($xml);

// SHOW HOW TO ACCESS SOME PARTS OF THE OBJECT
$x = (string)$obj->header->delivery->message->messageID;
echo "messageID: $x";
echo PHP_EOL;

$x = (string)$obj->header->manifest->document->name;
echo "name: $x";
echo PHP_EOL;

// SHOW HOW TO USE AN ITERATOR TO ACCESS REPEATED PARTS OF THE OBJECT
foreach ($obj->body->ORDER->REFERENCE as $r)
{
    echo ' REFTYPE: ' . $r['REFTYPE'];
    echo ' REFCODE: ' . $r->REFCODE;
    echo PHP_EOL;
}

foreach ($obj->body->ORDER->SUPPLIER->ADDRESS->NAME as $n)
{
    echo "NAME: $n";
    echo PHP_EOL;
}

// USE AN ITERATOR TO ACCESS A NESTED OBJECT
foreach ($obj->body->ORDER->ORDERLINE as $o)
{
    // TEASE OUT PARTS OF THE NESTED OBJECT
    $p = (string)$o->PRODUCT->DESCRIPTION;
    echo "PRODUCT DESCRIPTION: $p";
    echo PHP_EOL;
}

// ACTIVATE THIS TO SHOW THE ENTIRE OBJECT
// print_r($obj);

//prepare the query
$stmt = $dbh->prepare("INSERT INTO retailers (ret_id, ret_name) VALUES (:$x, :$p)");


      $stmt->execute();

?>

Result:
I just get this printed to the page:

messageID:
name:
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that works !!!

I have tried this for an extra variable but it gives an error:
Name: P-ORD-EXPDesc: PHP Warning: PDOStatement::execute() [function.PDOStatement-execute]: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in I:\CommercialWebsites\1callorders\site\xmlimport.php on line 20

Extra variable I am trying to add:
<ORDERDESC>Purchase Order</ORDERDESC>

---------
<?php
$dbh = new PDO("mysql:host=localhost;dbname=1callorders", "user", "password");
            
$xml = file_get_contents('xmlimport/Purchase_Order_FI2-4098098.xml');
$obj = simplexml_load_string($xml);
            
$name = (string)$obj->header->manifest->document->name;
$orderdesc = (string)$obj->header->manifest->document->orderdesc;
echo "Name: " . $name;
echo "Desc: " . $orderdesc;

//prepare the query with a named parameter called 'name'
$stmt = $dbh->prepare("INSERT INTO retailers (ret_name, ret_add) VALUES (:name, :orderdesc)");

//bind the named parameter to a PHP variable (called $name)
$stmt->bindParam('name', $name);
$stmt->bindParam('desc', $orderdesc);

//execute the query
$stmt->execute();      
?>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks to all - I really do appreciate the time you have all spent teaching me in this exercise.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent learning experience