The situation is the following:

<?php include "dbc.php"; $dbcnx = @mysql_connect($DB_HOST,$DB_USER,$DB_PASSWORD); mysql_query("SET character_set_results = 'utf8'", $dbcnx); mysql_set_charset('utf8'); mysql_select_db($DB_NAME, $dbcnx); $date = date("d/m/Y"); // Сегодняшняя дата в необходимом формате $link = "http://www.cbr.ru/scripts/XML_daily.asp?date_req=$date"; // Ссылка на XML-файл с курсами валют $content = file_get_contents($link); // Скачиваем содержимое страницы $dom = new domDocument("1.0", "cp1251"); // Создаём DOM $dom->loadXML($content); // Загружаем в DOM XML-документ $root = $dom->documentElement; // Берём корневой элемент $childs = $root->childNodes; // Получаем список дочерних элементов $data = array(); // Набор данных for ($i = 0; $i < $childs->length; $i++) { $childs_new = $childs->item($i)->childNodes; // Берём дочерние узлы for ($j = 0; $j < $childs_new->length; $j++) { /* Ищем интересующие нас валюты */ $el = $childs_new->item($j); $code = $el->nodeValue; if (($code == "USD") || ($code == "EUR")) $data[] = $childs_new; // Добавляем необходимые валюты в массив } } /* Перебор массива с данными о валютах */ for ($i = 0; $i < count($data); $i++) { $list = $data[$i]; for ($j = 0; $j < $list->length; $j++) { $el = $list->item($j); /* Выводим курсы валют */ if ($el->nodeName == "Name") { // echo $el->nodeValue; if ($el->nodeValue == "Доллар США") $cur = "USD"; if ($el->nodeValue == "Евро") $cur = "EUR"; echo $cur; } elseif ($el->nodeName == "Value") { // echo $el->nodeValue."<br>"; $val = $el->nodeName; } mysql_query("UPDATE `law_all_currency` SET `$cur` = '$val'"); } } mysql_close($dbcnx); ?> 

Everything is painfully clear, but I cannot humanly shove the value of USD in the USD column and the value of EUR in the EUR column of the "law_all_currency" table

I ask for help. Thanks in advance!

  • but what does "humanly?" - Chad
  • And I do not know what should be stored in your table, but as you write update, it changes the values ​​of the field with the name from $ cur to the value from $ val in all table entries, because the conditions for selecting records are not specified - Mike
  • So that worked. Before my edits, the code was like this: if ($ el-> nodeName == "Name") {echo $ el-> nodeValue. "-"; } elseif ($ el-> nodeName == "Value") {echo $ el-> nodeValue. "<br>"; } I haven’t yet been able to combine currency and value into one UPDATE request - berTalino
  • I think you need to show the structure of the law_all_currency table and clarify which fields and entries (selection criteria) need to be updated. - Akina
  • one
    why do you write the name of the node "value" to the database instead of $ val = $ el-> nodeName; this is $ val = $ el-> nodeValue; - L. Vadim

2 answers 2

It is necessary to write instead:

 $val = $el->nodeName; 

this

 $val = $el->nodeValue; 

    I will add here a variant of the code slightly simpler than yours for data acquisition, it can be useful.

    First, we get the exchange rate in XML for the current date (without a parameter):

     $url = "http://www.cbr.ru/scripts/XML_daily.asp"; $content = file_get_contents($url); 

    Further we use SimpleXML for work.

     $xml = new SimpleXMLElement($content); 

    Since currency ID do not change, we need to extract nodes with R01235 and R01239 . For these purposes, we use an XPath expression of the form "/ValCurs/Valute[@ID='R01235']/Value" . To get the textual value of a node, you must explicitly or implicitly cast it to the string type:

     $cfg = ['usd' => 'R01235', 'eur' => 'R01239']; $data = []; foreach($cfg as $k => $id){ $v = (string) $xml->xpath("/ValCurs/Valute[@ID='$id']/Value")[0]; $data[$k] = floatVal(str_replace(',', '.', $v)); } 

    If necessary, replace the comma in the value with the separator we need. At the output we have an array:

     Array ( [usd] => 59.9533 [eur] => 63.5445 ) 

    If you need to use DOM , the code will be as follows:

     $xml = new DOMDocument(); $xml->loadXML($content); $xp = new DOMXPath($xml); foreach($cfg as $k => $id){ $data[$k] = $xp->query("/ValCurs/Valute[@ID='$id']/Value")->item(0)->nodeValue; }