Guys, please tell me. There is a code that collects data of a particular country in the header on the site:

<?php $today = date("d/m/Y"); $fp = fopen('cb.xml', 'w'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,'http://www.cbr.ru/scripts/XML_daily.asp?date_req='.$today); curl_setopt($ch, CURLOPT_FILE, $fp); curl_exec($ch); fclose($fp); curl_close ($ch); ?> <script src="https://code.jquery.com/jquery-latest.js"></script> <script language="javascript" type="text/javascript"> jQuery(document).ready(function () { jQuery.ajax({ type: "GET", async : false, url: "cb.xml", dataType: "xml", success: Kurs }); function Kurs (xml) { jQuery(xml).find("Valute").each(function(){ if(jQuery(this).attr('ID')=='R01235') { usd = jQuery(this).find("Value").text(); jQuery(".rub span").text(parseInt(jQuery('.usd span').text())*parseInt(usd)); } }); } });</script> <script language="javascript" type="text/javascript">jQuery.noConflict();</script> <div id="banner_g1"> <?php $adres = $_SERVER['REQUEST_URI']; $parts = parse_url($adres); parse_str($parts['query'], $query); $need_id = $query['id']; include "php/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); $bg_head_pic = mysql_query("SELECT * FROM law_content WHERE id = $need_id"); while($row=mysql_fetch_assoc($bg_head_pic)) { if ( $row['bg_head_pic']!= '' ) { echo '<img id="img_bb" alt="'.$row['logo_alt'].'" src="/images/'.$row['bg_head_pic'].'" />'; echo '</div><div id="banner_g2"><div id="banner_g2t1">'.$row['main_head_string'].'</div><marquee><div class="lenta-rbk">{module 177}</div></marquee><div id="banner_g2b">'; echo '<img alt="'.$row['logo_alt'].'" src="/images/'.$row['logo_pic'].'" />'; echo '<div id="banner_g2t2"><p>СТОИМОСТЬ ГОТОВОЙ КОМПАНИИ:</p><ul><li class="usd">от <span>'.$row['cost'].'</span> <span class="new_head_curr">'.$row['currency'].'</span></li>'; echo '<li class="rub">от <span>100</span> рублей (по курсу ЦБРФ)</li></ul><p><a href="/gotovie-kompanii-s-otkritim-schetom.html" target="">Компании со счетом</a></p></div></div>'; } } mysql_close($dbcnx); ?> </div> 

The code was written a couple of years ago in a hurry and we see that the central bank first parses. But in this case only the dollar is substituted! A piece answers for this:

  if (jQuery(this).attr('ID')=='R01235') { usd = jQuery(this).find("Value").text(); jQuery(".rub span").text(parseInt(jQuery('.usd span').text())*parseInt(usd)); } 

There are also cases with EURO, it looks like this:

 if (jQuery(this).attr('ID')=='R01239') { eur = jQuery(this).find("Value").text(); jQuery(".rub span").text(parseInt(jQuery('.eur span').text())*parseInt(eur)); } 

How can I make the EUR or USD variant be used within this code? PS: It depends on $ row ['currency'], which matters either "eur" or "usd".

Thank you in advance !

  • trash what and not code. - teran
  • At the moment, I clearly understand that jQuery can be completely abandoned here! Because: - Parsim with php (initially); - we multiply by the course on the site already over the load with the help of jQuery (forced measure); - in this case, we have compiled a common module, which allows us not to use JS, but I still don’t understand how to manage pure PHP here ... = \ - berTalino
  • Well, you also get the lines echo <li class="rub">от <span>100</span> рублей , which are then replaced by jquery, here, instead of 100 enter a currency check and a conversion rate. Rather, in the cycle, first count in rubles, and then substitute 100 for the desired value. To do this, you need to extract the values ​​of the course from the xml-file obtained through the сurl . In general, the approach is bad. It is better to add a task to cron which once a day will receive the current exchange rate and stuff it into the database with the current date, and then you can read the current price directly in the request. - teran
  • "It is better to add a task to cron which will receive the current exchange rate once a day and stuff it into the database with the current date, and then you can read the current price directly in the request" (c) I also thought about this, because the module is undergoing improvements in the normal side ... - berTalino
  • one
    the main question here is that the exchange rate itself does not change more than once a day, so there’s no point in extracting it every time you visit a page. Secondly, if the cost is shown at the current rate, it may be useful to keep a history of changes in the course, in order to avoid disputes with customers. Yes, and recalculate the price in JS there is no sense, you must initially display the desired value. You can do without cron, just when you first visit, check if there is a course in the database, if not, then query and stuff into the database, and then count with it. - teran

0