FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


displaing error





AlexandruDan
Hello.

I tried to read the values for euro and USD from the site of the Ro central bank, and when I display the result, it it not how it should be. Sad
The code is:

Code:
<?php
function cursvalutar($valuta) {
   
        $file = fopen("http://www.bnro.ro/Ro/Info/", "r");
        $today = date("d-m-Y");
        $numeValuta = array("Euro","Dolarul SUA");
   
        while (!feof($file)){
            $line = fgets($file, 1024);
            if (eregi('<TD class="bold">(.*)</TD>', $line, $out))     
            {
                $cursvalutar = each($out);
                while (list($key,$val) = each($numeValuta)){           
                    if ($val == "Euro") {               
                        $euro = str_replace(',','',$cursvalutar[1]);
                    }
                    if ($val == "Dolarul SUA"){               
                        $dolar = str_replace(',','',$cursvalutar[1]);
                    }
                break;
                }
            }
        }
       
        fclose($file);
       
        if($valuta == "dolar") return $dolar;
        elseif($valuta == "euro") return $euro;
        else return false;
               
    }
    echo "1 USD este ";
    echo cursvalutar('dolar')."<br />";
    echo "1 EURO este ";
    echo cursvalutar('euro');
  ?> 

it displays:
1 USD este 23621
1 EURO este 37155

it should display 1:
1 USD este 2,3621
1 EURO este 3,7155
with a comma ,

When I view the source code from http://www.bnro.ro/Ro/Info/ the value for USD/euro is with a comma (,). and I can't understand why my code displays it without the comma.

Can you help me?
Alexandru
sonam
Hmmm, I don't understand good your code but in this line you are define $dolar variable and you are replace coma with "nothing".
Code:
$dolar = str_replace(',','',$cursvalutar[1]);


The same is with euro. Maybe you just need:
Code:
$dolar = $cursvalutar[1];


Sonam
rvec
just divide the value by 10000 just before outputting it.
AlexandruDan
Correct. There is still a problem: when i divide the value, it displays 0. So, i think that what i have is a text divided by a number and i need to transform the text into a number.
Can you tell me what is the syntax for transforming a text into a number?

Thanks,
Alexandru
rvec
you can use int like this to make something a number:
Code:

$valuta = (int) $valuta;


But I think adding this after fclose($file); should do:
$valuta /= 10000;
AlexandruDan
I figure it out, I have used substr:

Code:
$a = cursvalutar ('dolar');
$b = substr($a, 1);
$c = substr ($b,-10,1);
echo "  1 USD este ";
echo $c;
echo ".";
echo substr ($b,-9,4);
echo " lei";

//afisare euro
echo "<br />";
$d = cursvalutar ('euro');
$e = substr($d, 1);
$f = substr ($e,-10,1);
//echo $euro;
//substr ($b);
echo "  1 euro este ";
    echo $f;
    echo ".";
    echo substr ($e,-9,4);
    echo " lei";


It could have been coded easier, you don't have to use so many variables ($a ... $e).
Thanks,
Alexandru

edit by rvec: please use code tags
rvec
to make it even shorter
Code:
echo "  1 euro este ";
    echo $f;
    echo ".";
    echo substr ($e,-9,4);
    echo " lei";

is the same as
Code:

echo '  1 euro este '. $f .'.'. substr ($e,-9,4) .' lei';
Related topics
Help: SQL error after uploading new files(thread updated)
Error 404 ..... How to Hide ...
fantastico error
ERROR: Nameserver ns1.frihost.net is not authoritative
Custom error pages using .htaccess
error with cash mod
509 b/w error
got this error..
FTP Error
Email Error
error
FTP error codes -- What they mean
Forum error when posting
Error Posting...
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.