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) { $y=$new_date[0]; $dmonth= $new_date[1]; $dday = $new_date[2]; $mix=$y2; $mix.=$dmonth; $mix.=$dday; //all currency files starts with a, also checking by date using strpos $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 .'<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; } }; } } } ?>
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"/> <select name="datez"> <?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>'; } if (!isset($_POST['datez'])){ $_POST['datez']=''; } $chosen_date =$_POST['datez']; if (!isset($_POST['code'])){ $_POST['code']=''; } $code= strtoupper($_POST['code']); currency($code,$chosen_date); ?>