Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing multiple xml files

Status
Not open for further replies.

tewari68

Programmer
Jan 25, 2005
87
US
Hi,
I have written a php routine which parses a xml file and puts the data in the database the script goes into a certain folder looks for .xml files and then parses them one by one and does the database entry.
I have written this code so far however I am not able to get the right sql queries .
Each xml file will have one order id and several images associated with that orderid and there maybe several such xml files. Now for each orderid/xml file I have to insert the order id and imagenames and other data into the database.
HERE IS MY CODE.



Code:
<?php
include("/home/caskie/[URL unfurl="true"]www/demos/laserlight/includes/global_vars.php");[/URL]
require_once("/home/caskie/[URL unfurl="true"]www/demos/laserlight/includes/cls_db.php");[/URL]
$db=new db(_DB_HOST_,_DB_NAME_,_DB_USER_NAME_,_DB_USER_PASSWORD_,true);
                $db->db_connect();
                $db->db_select_db();

$newDirectory = "/home/caskie/[URL unfurl="true"]www/demos/laserlight/samples/php/photo/";[/URL]

$xml_orderid_key = "*ORDER*ORDERID";
$xml_ordernumber_key = "*ORDER*ORDERNUMBER";
$xml_custname_key = "*ORDER*CUSTOMER*NAME";
$xml_qty_key = "*ORDER*ITEMS*ITEM*QUANTITY";
$xml_img_key = "*ORDER*ITEMS*ITEM*IMAGES*IMAGE*NAME";
$xml_retailsku_key = "*ORDER*ITEMS*ITEM*PRODUCT*RETAILSKU";
$xml_coaddress1_key = "*ORDER*SHIPTOADDRESS*LINE1";
$xml_coaddress2_key = "*ORDER*SHIPTOADDRESS*LINE2";
$xml_cocity_key = "*ORDER*SHIPTOADDRESS*CITY";
$xml_costate_key = "*ORDER*SHIPTOADDRESS*STATE";
$xml_cozipcode_key = "*ORDER*SHIPTOADDRESS*ZIPCODE";
 

$story_array = array();

$counter = 0;
class xml_story{
    var $orderid, $ordernumber, $custname, $qty, $imgname, $retailsku, $coaddress1, $coaddress2, $cocity, $costate, $cozipcode;
}

function startTag($parser, $data){
    global $current_tag;
    $current_tag .= "*$data";
    //echo "<BR>Start Tag = " . $current_tag."<BR>";
}

function endTag($parser, $data){
    global $current_tag;
    $tag_key = strrpos($current_tag, '*');
    $current_tag = substr($current_tag, 0, $tag_key);
    //echo "<BR>End Tag =" . $current_tag."<BR>";
}

function contents($parser, $data){
    global $current_tag, $xml_orderid_key, $xml_ordernumber_key, $xml_custname_key, $xml_qty_key, $xml_img_key, $xml_retailsku_key, $xml_coaddress1_key, $xml_coaddress2_key, $xml_cocity_key, $xml_costate_key, $xml_cozipcode_key, $counter, $story_array;

    switch($current_tag){
        case $xml_orderid_key:
            $story_array[$counter] = new xml_story();
            $story_array[$counter]->ORDERID = $data;
            break;
        case $xml_ordernumber_key:
            $story_array[$counter]->ORDERNUMBER = $data;
             break;
        case $xml_custname_key:
           $story_array[$counter]->NAME = $data;
           break;
        case $xml_qty_key:
            $story_array[$counter]->QUANTITY = $data;
            break;
        case $xml_cozipcode_key:
             $story_array[$counter]->ZIPCODE = $data;
              break;
        case $xml_retailsku_key:
            $story_array[$counter]->RETAILSKU = $data;
            break;
        case $xml_coaddress1_key:
            $story_array[$counter]->LINE1 = $data;
            break;
        case $xml_coaddress2_key:
            $story_array[$counter]->LINE2 = $data;
            break;
        case $xml_cocity_key:
            $story_array[$counter]->CITY = $data;
            break;
        case $xml_costate_key:
            $story_array[$counter]->STATE = $data;
            break;
        case $xml_img_key:
            $story_array[$counter]->NAME = $data;
            $counter++;
            break;
    }
}
foreach (glob('*.xml') as $filename)
{
    //unset($data);
    $xml_parser = xml_parser_create();

    xml_set_element_handler($xml_parser, "startTag", "endTag");

    xml_set_character_data_handler($xml_parser, "contents");

    $fp = fopen($filename, "r") or die("Could not open file");

    $data = fread($fp, filesize($filename)) or die("Could not read file");


    if(!(xml_parse($xml_parser, $data, feof($fp))))
        {
            die("Error on line " . xml_get_current_line_number($xml_parser));

            
         }

xml_parser_free($xml_parser);
    fclose($fp);
    //echo "<BR>File Opened = ".$filename."<BR>".$data."<BR>";
    $newfile = str_replace("xml", "ack", $filename);
    $cmd = "mv ".$filename." ".$newfile;
    //echo "Command to be executed". $cmd;
    system($cmd);
}
?>

    <html>
    <head>
    <title>Parsed Order Details </title>
    </head>
    <body bgcolor="#FFFFFF">
    <?php
        //echo "Start Before";
        //for($l=0;$l<count($Image_Name);$l++)
        //        echo "<BR>".$Image_Name[$l]."<BR>";
       //echo "End Before<hr><BR>";
       
        unset($Image_Name);
        //unset($Order_ID);
        unset($Order_Number);
        unset($Customer_Name);
        unset($Quantity);
        unset($RetailSku);
        unset($Company_Addr1);
        unset($Company_Addr2);
        unset($Company_City);
        unset($Company_Zipcode);
            //echo "Image = " . $Image_Name[0];
        for($x=0;$x<count($story_array);$x++)
            {
                if($story_array[$x]->ORDERID != '')
                $Order_ID[] = $story_array[$x]->ORDERID;
                $Order_Number[]=$story_array[$x]->ORDERNUMBER;
                $Customer_Name[] =$story_array[$x]->NAME;
                $Quantity[] = $story_array[$x]->QUANTITY;
                $Image_Name[] = $story_array[$x]->NAME;
                $RetailSku[] = $story_array[$x]->RETAILSKU;
                $Company_Addr1[] = $story_array[$x]->LINE1;
                $Company_Addr2[] = $story_array[$x]->LINE2;
                $Company_City[] = $story_array[$x]->CITY;
                $Company_State[] = $story_array[$x]->STATE;
                $Company_Zipcode[] = $story_array[$x]->ZIPCODE;
    
            }
            //echo "<br>Start after";
            //for($l=0;$l<count($Image_Name);$l++)
    //{
                
    //            echo "<BR>".$Image_Name[$l]."<BR>";
    //            
    //}
    //echo "End after<br>";
    for($j=0;$j< count($Image_Name); $j++)
    {
        if($RetailSku[$j] == 'laso2rdpers')
        $Color = "Red";
        elseif($RetailSku[$j] == 'laso2rdperss')
        {
            $Color = "Red Sample";
            $special_inst = "SAMPLE ORDER";
        }
        elseif($RetailSku[$j] == 'laso2gdpers')
            $Color = "Gold";
        elseif($RetailSku[$j] == 'laso2gdperss')
        {
            $Color = "Gold Sample";
            $special_inst = "SAMPLE ORDER";
        }
    $sql = "insert into tbl_cart (order_id, final_image, prod_type, Quantity, Color, Size, special_instructions, cust_id) values('".$Order_ID[0]."', '". $Image_Name[$j]."', 'Photo_Images', '".$Quantity[$j]."', '".$Color."', '3 1/4', '".$special_inst."', '')";
       echo "<BR>".$sql."<BR>";
    //unset($story_array);  
    $movefiles =  "mv ".$Image_Name[$j]." ".$newDirectory;
    $newfile = str_replace("xml", "ack", $filename);
    $cmd = "mv ".$filename." ".$newfile;
    //echo "Command to be executed". $cmd;
    //system($cmd);
    //unset($Image_Name);

    }//End for loop
unset ($data);
//unset($Image_Name);
?>


</body>
</html>
<?
//}//end foreach loop
//unset($Image_Name)
?>
However my arrays are not being initialized properly and so I get the values from my previous file when I parse the next file and so duplicate enteries are being made into the database.
Appreciate if anybody can help me in this.
 
Move the line "$story_array = array();" to right after the fopen.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top