Witam,
popełniłem kiedys taki oto prosty skrypt wymiany walut, w pierwszej częsci krótki opis, w drugiej kod źródłowy, w trzeciej wywołanie(tu zdaję sobie sprawę, że jest niechlujnie zrobione). Proszę o ocene kodu, opinie, komentarze.
1) Moduł ma sie połączyć z jakimś API wymiany walut (dowolne), pobrać kurs za dany dzien, przeliczyć i wyświetlić wynik. Wprowadzam kod ISO waluty (np USD), wybieram date, naciskam Convert - plugin pokazuje
mi przeliczona walute. Napisane proceduralnie.
2)
<?php
// main operating function, takes currency code id and chosen date
function currency($code, $chosen_date){
//if variables not empty function proceed
//taking currency file id`s
//document as string
foreach ($exchange as $exchange2) {
$new_date= explode('-', $chosen_date); $y=$new_date[0];
$dmonth= $new_date[1];
$dday = $new_date[2];
$mix=$y2;
$mix.=$dmonth;
$mix.=$dday;
$year = '20'.substr($exchange2, 5
,2
); $month = substr($exchange2, 7
,2
); $day = substr($exchange2, 9
,2
);
//all currency files starts with a, also checking by date using strpos
if((substr($exchange2,0,1)== 'a')&&(strpos($exchange2,$mix))) { $K = $exchange2;}
}
// if variable K is not set it means that there were no data from that day, currency value will be taken from previous day
$chosen_date=$chosen_date - 86400;
$chosen_date=date('Y-m-d', $chosen_date); $chosen_date .'<br>';
currency($code, $chosen_date);
return 0;
}
//checing if variable $K is set, and rounding it up
$date = 'http://www.nbp.pl/kursy/xml/'.$K.'.xml';
}
$ch = curl_init();
//setting URL and options
curl_setopt($ch, CURLOPT_URL, $date);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// due to setting CURLOPT_RETURNTRANSFER =true, curl_exec will return results
$info = curl_exec($ch);
$info = curl_exec($ch);
//close cURL
curl_close($ch);
$xml = new SimpleXMLElement($info);
foreach($xml as $pozycja) {
$a = $pozycja->kod_waluty;
$b = $pozycja->kurs_sredni;
echo '1 '. $a." on ".$chosen_date." was worth ". $b. " PLN <br>" ; }
};
}
}
else echo "Please provide currency code and date.";
}
?>
3)
<?php
//view file used to take informations from user
//start time
$n=date('Y-m-d');
$d = strtotime($n);
$start_time=1325484000;
?>
<form action="NBPhistory.php" method="POST"> <input type="text" name="code"/> <?php
include './kurs.php';
for ($i=$start_time;$i<=$d+86400; $i=$i+86400){
$w=date("Y-m-d",$i);
echo '<option value='.$w.'>'.$w.'
</option>';
}
echo '
<input type="submit" value="Convert!"/></form>';
if (!isset($_POST['datez'])){
$_POST['datez']='';
}
$chosen_date =$_POST['datez'];
if (!isset($_POST['code'])){
$_POST['code']='';
}
$code= strtoupper($_POST['code']);
currency($code,$chosen_date);
?>