2017-02-28 5 views
0

私は苦労しているXMLファイルにいくつかの問題があります。このXMLファイルからカテゴリをエコーアウトしたい

私がしますprint_rを行うと出力はこのようになります。

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

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 

} // foreachのループ

の終わり

XMLコードでわかるように、カテゴリの私のフィールドはCATEGORYNAMEと呼ばれていますが、この配列の出力方法を知ることはできません。他のすべてのフィールドをエコーするのは簡単だったが、私はサブフィールドなどのカテゴリに入れたときにエラーが発生した...

私は...

+0

を試してみてください、お願いします。 – JazZ

答えて

0

を倍のforeachのカップルを試してみましたあなたは、内側のループを配置することができますあなたの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); 
} 

最初のforeachはコードから取得されます。

0

簡体字コードは、カテゴリ名を取得する:あなたのコードを表示するこの

$xml = '<?xml version="1.0" encoding="UTF-8"?><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></CATALOG>'; 



$xmlcont = new SimpleXMLElement($xml); 

foreach ($xmlcont as $value) { 
    $val = $value->CATEGORIES->CATEGORYNAME; 
    foreach ($val as $key) { 
     echo $key."<br/>"; 
    } 

} 

OUTPUT

AUTOMATIK-マスキン

Borrmaskin

関連する問題