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)
  1. <?php
  2. // main operating function, takes currency code id and chosen date
  3. function currency($code, $chosen_date){
  4.  
  5. //if variables not empty function proceed
  6. if((!empty($code))&&(!empty($chosen_date))){
  7.  
  8. //taking currency file id`s
  9. $exchange = explode("\n",file_get_contents('http://www.nbp.pl/kursy/xml/dir.txt'));
  10.  
  11. //document as string
  12. foreach ($exchange as $exchange2) {
  13.  
  14. $new_date= explode('-', $chosen_date);
  15. $y=$new_date[0];
  16. $y2= substr($y, 2,2);
  17.  
  18. $dmonth= $new_date[1];
  19.  
  20. $dday = $new_date[2];
  21. $mix=$y2;
  22. $mix.=$dmonth;
  23. $mix.=$dday;
  24.  
  25. $year = '20'.substr($exchange2, 5,2);
  26. $month = substr($exchange2, 7,2);
  27. $day = substr($exchange2, 9,2);
  28.  
  29. //all currency files starts with a, also checking by date using strpos
  30. if((substr($exchange2,0,1)== 'a')&&(strpos($exchange2,$mix))) {
  31. $K = $exchange2;}
  32. }
  33. // if variable K is not set it means that there were no data from that day, currency value will be taken from previous day
  34. if(!isset($K)) {
  35. $chosen_date=strtotime($chosen_date);
  36. $chosen_date=$chosen_date - 86400;
  37. $chosen_date=date('Y-m-d', $chosen_date);
  38. $chosen_date .'<br>';
  39. currency($code, $chosen_date);
  40. return 0;
  41. }
  42.  
  43. //checing if variable $K is set, and rounding it up
  44. if(isset($K)){
  45. $K=trim($K);
  46. $date = 'http://www.nbp.pl/kursy/xml/'.$K.'.xml';
  47. }
  48.  
  49.  
  50. $ch = curl_init();
  51. //setting URL and options
  52. curl_setopt($ch, CURLOPT_URL, $date);
  53. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  54. // due to setting CURLOPT_RETURNTRANSFER =true, curl_exec will return results
  55. $info = curl_exec($ch);
  56. $info = curl_exec($ch);
  57. //close cURL
  58. curl_close($ch);
  59.  
  60. if(isset ($date)){
  61. $xml = new SimpleXMLElement($info);
  62.  
  63. foreach($xml as $pozycja) {
  64.  
  65. $a = $pozycja->kod_waluty;
  66. $b = $pozycja->kurs_sredni;
  67.  
  68. if(strstr($a, $code)){
  69. echo '1 '. $a." on ".$chosen_date." was worth ". $b. " PLN <br>" ;
  70. }
  71. };
  72. }
  73. }
  74. else echo "Please provide currency code and date.";
  75.  
  76. }
  77.  
  78. ?>



3)
  1. <?php
  2. //view file used to take informations from user
  3.  
  4. //start time
  5. $n=date('Y-m-d');
  6. $d = strtotime($n);
  7. $start_time=1325484000;
  8.  
  9. ?>
  10. <form action="NBPhistory.php" method="POST">
  11. <input type="text" name="code"/>
  12. <select name="datez">
  13. <option value="">Select date</option>
  14. <?php
  15. include './kurs.php';
  16. for ($i=$start_time;$i<=$d+86400; $i=$i+86400){
  17. $w=date("Y-m-d",$i);
  18. echo '<option value='.$w.'>'.$w.' </option>';
  19. }
  20. echo '<input type="submit" value="Convert!"/></form>';
  21.  
  22. if (!isset($_POST['datez'])){
  23. $_POST['datez']='';
  24. }
  25. $chosen_date =$_POST['datez'];
  26.  
  27. if (!isset($_POST['code'])){
  28. $_POST['code']='';
  29. }
  30.  
  31. $code= strtoupper($_POST['code']);
  32. currency($code,$chosen_date);
  33. ?>