Search code examples
phpxmlsimplexml

I want to echo out the categories from this XML-file


I have some issues with a XML-file I'm struggling with.

When I do a PRINT_R the output will be this:

SimpleXMLElement Object ( ) SimpleXMLElement Object ( ) SimpleXMLElement Object ( [CATEGORYNAME] => Array ( [0] => Automatik-maskin [1] => Borrmaskin ) ) SimpleXMLElement Object ( )

The XML:

<CATALOG>
<ENGINE>
    <TITLE>Another product</TITLE>
    <ARTNR>75</ARTNR>
    <TEXT>This is another awesome product</TEXT>
    <CATEGORIES></CATEGORIES>
</ENGINE>
<ENGINE>
    <TITLE>Borrmaskin</TITLE>
    <ARTNR>3530</ARTNR>
    <TEXT>This is awesome</TEXT>

    <QTY>10</QTY>
    <CATEGORIES>
            <CATEGORYNAME>Automatik-maskin</CATEGORYNAME>
            <CATEGORYNAME>Borrmaskin</CATEGORYNAME>
    </CATEGORIES>
    <PRICE>10.90</PRICE>
</ENGINE>   

    $xml = simplexml_load_file("../../xml/xml-stad.xml") or die("Could not find the file...");

//Loop for the insert of new product or update an existing.
foreach ($xml as $i){

//Converting the XML-tags to variables:
$type       ='product'; //Define the wordpress post type
$title      = (string)$i->TITLE; //Product-title
$artnumber  = (string)$i->ARTNR; //Product article number
$text       = (string)$i->TEXT; //Product description
$qty        = (string)$i->QTY; //Numbers of products each package
$packqty    = (string)$i->PACKQTY; //Numbers of packages each pallet
$width      = (string)$i->WIDTH; //Width of product
$height     = (string)$i->HEIGHT; //Height of product 
$categories = $i->CATEGORIES; //Categories of the product (this can be multiple)
$price      = (string)$i->PRICE; //Price of product
$campaign   = (string)$i->CAMPAIGN; //If a campaign is present this is declared here,
$image      = (string)$i->IMAGE; //Image source of a product
$branch     = (string)$i->BRANCH; //Branch of product
$date       = date('Y-m-t') .' ' . date('H:i:s');

//Arrays for wp_posts-table
$data_post_table = array(
    'post_type'     => $type,
    'post_title'    => $title,
    'post_status'   => 'publish',
    'post_date'     => $date,
    'post_date_gmt' => $date,
    'post_author'   => '1',
);

    global $wpdb;

    //Check if there is a Post_id for the artnr
    $sql = $wpdb->get_row("SELECT post_id FROM wp_postmeta WHERE meta_key = 'artnr' AND meta_value = '".$artnumber."' ");
    if($wpdb->num_rows > 0) {
        //Declaring variables for the table update.
        $postId = $sql->post_id;

        //Update affected rows
        update_post_meta($postId, 'text', $text);
        update_post_meta($postId, 'qty', $qty);
        update_post_meta($postId, 'packqty', $packqty);
        update_post_meta($postId, 'width', $width);
        update_post_meta($postId, 'height', $height);
        //update_post_meta($postId, 'categories', $categories);
        update_post_meta($postId, 'price', $price);
        update_post_meta($postId, 'campaign', $campaign);
        update_post_meta($postId, 'image', $image);
        update_post_meta($postId, 'branch', $branch);
        print_r($categories);
    } else {

        //If product does not exist in wp_post table.
        //Insert into Database ->  wp_post (the table)
        $wpdb->insert('wp_posts', $data_post_table);

        //Get the post-id of inserted row
        $post_id = $wpdb->insert_id;    

        //Declaring variables for creating a new row in wp_postmeta (the table) with the key preferences for the product.
            $data_post_meta = array(
                'post_id' => $post_id,
                'meta_key' => 'artnr',
                'meta_value' => $artnumber
            );
        $wpdb->insert('wp_postmeta', $data_post_meta);

        //Create new rows based on the generated post_id.
        add_post_meta($post_id, 'text', $text);
        add_post_meta($post_id, 'qty', $qty);
        add_post_meta($post_id, 'packqty', $packqty);
        add_post_meta($post_id, 'width', $width);
        add_post_meta($post_id, 'height', $height);
        add_post_meta($post_id, 'categories', $categories);
        add_post_meta($post_id, 'price', $price);
        add_post_meta($post_id, 'campaign', $campaign);
        add_post_meta($post_id, 'image', $image);
        add_post_meta($post_id, 'branch', $branch);

} //End of IF-statement

} //end of foreach-loop

As you can see in the XML-code, my field for categories is called CATEGORYNAME but I can't figure out how to output this array. It was easy to echo out all the other fields but when i put in the categories as subfield there is an error...

I have tried foreach couple of times...


Solution

  • You can place a loop inside a loop in order to get all the categories from your XML.

    $xml = simplexml_load_string($xml);
    foreach ($xml as $i){
        if(count($i->CATEGORIES->CATEGORYNAME) > 0)
            foreach ($i->CATEGORIES->CATEGORYNAME as $category_name) 
                var_dump((string) $category_name);
    }
    

    The first foreach is taken from your code.