0

i have this .xml file..

<product>
    <id>4021</id>
    <product_code>1220211</product_code>
    <barcode>1220211</barcode>
    <title>sera Pond granulat - храна на гранули</title>
    <price>71.65</price>
    <meta_title>sera Pond granulat - храна на гранули</meta_title>
    <meta_description>sera Pond granulat, 21 000 мл</meta_description>
</product>

With this php code i want to create meta decsription of all products.

<?php
  $xml=simplexml_load_file("zoo.xml") or die("Error: Cannot create object");
       foreach($xml->children() as $desc) { 
         echo $desc->title . "  "; 
         echo $desc->price . " лв. "; 
       echo ' some text here' . "<br>";
   } 
?>

How to update all meta_description in the same file with created text. Thank you

Martin
  • 13
  • 1
  • What have you tried so far? – hakamairi Jul 26 '18 at 08:51
  • I want to create description of my product base on title and price from product, title+price=description – Martin Jul 26 '18 at 08:54
  • Missing implementation is not really a good programming question. So what approaches have you tried so far to tackle this one? Care to provide a code example where you demonstrate the problem? – hakamairi Jul 26 '18 at 08:57
  • I don't know hot to update field meta_description in the xml file after create the text with php code – Martin Jul 26 '18 at 09:01

1 Answers1

0

If you just need to change the text of the last element, you can access it the same way as you fetch the value for your echo statements...

foreach($xml->product as $desc) {
    echo $desc->title . "  ";
    echo $desc->price . " лв. ";
    $desc->meta_description = $desc->title . "  ".$desc->price . " лв. ";
}

Don't forget to save the data afterwards back to the file

$xml->asXML("zoo.xml");
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Working perfect! Only this error Fatal error: Maximum execution time of 30 seconds exceeded in /home/itbuddiesbg/public_html/import/xml.php on line 12 – Martin Jul 27 '18 at 08:37
  • https://stackoverflow.com/questions/5164930/fatal-error-maximum-execution-time-of-30-seconds-exceeded may help with sorting that out. – Nigel Ren Jul 27 '18 at 08:40