0

I've this xml file.

<resources>
  <resource>
    <code>THB</code>
    <rate>35.570000</rate>
    <ts>1480703728</ts>
  </resource>
  <resource>
    <code>HKD</code>
    <rate>65.570000</rate>
    <ts>1480447028</ts>
  </resource>
</resources>

How do I find the specific code name and update/replace/edit the rate using PHP?

For an example, I would like to update HKD's rate and timestamp.

I've tried using this method but it didn't work out.

foreach ($Ratexml->resource as $resource) //loop thru every resource
{
    $RateCode = $resource->code; //get the code
    if($RateCode == "HKD") //find the match
    {
        $resource->resource->rate= "123"; //replace the value with desired value but failed 
    }

}

This happened instead when I try to replace the value of the HDK..

 <resources>
    <resource> //Start HKD
         <code>HKD</code>
         <rate>7.755150</rate>
         <ts>1480703739</ts>
     <resource><rate>123</rate></resource>
   </resource> //end HKD
</resources>

The output that I actually wanted is something like this. The HKD's rate is replaced to 123.

<resources>
  <resource>
    <code>THB</code>
    <rate>35.570000</rate>
    <ts>1480703728</ts>
  </resource>
  <resource>
    <code>HKD</code>
    <rate>123</rate>
    <ts>1480447028</ts>
  </resource>
</resources>

Thanks to whoever that help me out as I've been stuck here for so long. Cheers.

halfer
  • 19,824
  • 17
  • 99
  • 186
Oscar
  • 1
  • 3
  • Have you tried php's [simple xml](http://php.net/manual/en/book.simplexml.php) library? – Ahsan Dec 03 '16 at 13:30
  • Have you tried `$resource->rate= "123";` – RiggsFolly Dec 03 '16 at 13:33
  • Have you tried looking at the PHP error log. Its real useful when things are going wrong. Or, Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Dec 03 '16 at 13:34
  • @RiggsFolly Omg. It did worked. It was embarrassing to make a mistake like this. I'm new to this xml thingy and to stackoverflow too. Really thanks a lot btw. – Oscar Dec 03 '16 at 13:43

0 Answers0