Revin cu un howto mai tarziu.Cod:<? /* === === === === === === === === === === === === === === === === === === === === === CREATE TABLE `currency` ( `id` mediumint(8) unsigned NOT NULL auto_increment, `date_stamp` date NOT NULL default '0000-00-00', `usd` varchar(10) NOT NULL default '', `eur` varchar(10) NOT NULL default '', PRIMARY KEY (`id`) ); === === === === === === === === === === === === === === === === === === === === === */ $currency_url = 'http://www.bnro.ro/Ro/Info/default.asp'; $db_host = ''; $db_username = ''; $db_password = ''; $db_database = ''; $db_table = ''; $debug = 0; /* === === === === === === === === === === === === === === === === === === === === === */ function convert_to_mysql_date($date, $debug = 0) { $mysql_date = ''; $date = str_replace('.', ' ', $date); $date_exploded = explode(' ', $date); $day = $date_exploded[0]; $month = $date_exploded[1]; $year = $date_exploded[2]; $month_array = array('ian', 'feb', 'mar', 'apr', 'mai', 'iun', 'iul', 'aug', 'sep', 'oct', 'nov', 'dec'); $month = array_search($month, $month_array) + 1; if(checkdate($month, $day, $year)) { $mysql_date = sprintf("%04d", $year).'-'.sprintf("%02d", $month).'-'.sprintf("%02d", $day); if($debug) echo($date.' was converted to '.$mysql_date.'<br>'."n"); } else { if($debug) echo('There was an error converting '.$date.' to a MySQL date.<br>'."n"); } return($mysql_date); } function get_currency_page_contents($currency_url, $debug = 0) { $currency_page_contents = ''; $fh = @fopen($currency_url, 'r'); if($fh) { if($debug) echo('Successfully opened provided URL.<br>'."n"); while(!feof($fh)) { $currency_page_contents .= fread($fh, 8192); } fclose($fh); } else { if($debug) echo('There was an error opening the provided URL.<br>'."n"); } return($currency_page_contents); } function get_date_array($currency_page_contents, $debug = 0) { $date_arr = array(); preg_match("'simbol</th>(.*?)</tr>'si", $currency_page_contents, $match_date); preg_match_all("'<th>(.*?)</th>'si", $match_date[1], $date_array); for($i = 0; $i < count($date_array[0]); $i++) { $date_arr[] = trim(convert_to_mysql_date($date_array[1][$i], $debug)); if($debug) echo('date_arr['.$i.'] = '.$date_arr[$i].'<br>'."n"); } return($date_arr); } function get_usd_array($currency_page_contents, $debug = 0) { $usd_arr = array(); preg_match("'usd</td>(.*?)</tr>'si", $currency_page_contents,$match_usd); preg_match_all("'<td.*?>(.*?)</td>'si", $match_usd[1], $usd_array); for($i = 0; $i < count($usd_array[0]); $i++) { $usd_arr[] = trim(str_replace(' ', '', $usd_array[1][$i])); if($debug) echo('usd_arr['.$i.'] = '.$usd_arr[$i].'<br>'."n"); } return($usd_arr); } function get_eur_array($currency_page_contents, $debug = 0) { $eur_arr = array(); preg_match("'eur</td>(.*?)</tr>'si", $currency_page_contents, $match_eur); preg_match_all("'<td.*?>(.*?)</td>'si", $match_eur[1], $eur_array); for($i = 0; $i < count($eur_array[0]); $i++) { $eur_arr[] = trim(str_replace(' ','', $eur_array[1][$i])); if($debug) echo('eur_arr['.$i.'] = '.$eur_arr[$i].'<br>'."n"); } return($eur_arr); } if(trim($db_host) != '' && trim($db_username) != '') { $db_link = @mysql_connect($db_host, $db_username, $db_password) or die('DB Error - Could not connect to database: '.mysql_error()); mysql_select_db($db_database) or die('DB Error - Could not select database: '.mysql_error()); $currency_page_contents = get_currency_page_contents($currency_url, $debug); if($currency_page_contents) { $date_array = get_date_array($currency_page_contents, $debug); $usd_array = get_usd_array($currency_page_contents, $debug); $eur_array = get_eur_array($currency_page_contents, $debug); foreach($date_array as $key => $date) { $usd = $usd_array[$key]; $eur = $eur_array[$key]; $query = "SELECT usd, eur FROM $db_table WHERE date_stamp = '$date'"; $result = mysql_query($query) or die('DB Error - Query failed: ' . mysql_error()); if($result) { $date_exists = mysql_num_rows($result); if($date_exists) { $row = mysql_fetch_array($result); list($db_usd, $db_eur) = $row; if($db_usd != $usd or $db_eur != $eur) { if($debug) echo('Difference found between the stored and online values of EUR and USD. Updating...<br>'."n"); $query = "UPDATE $db_table SET usd = '$usd', eur = '$eur' WHERE date_stamp = '$date'"; $result = mysql_query($query) or die('DB Error - Query failed: '.mysql_error()); } } else { if($debug) echo('Inserting new row...<br>'."n"); $query = "INSERT INTO $db_table (date_stamp, usd, eur) VALUES ('$date', '$usd', '$eur')"; $result = mysql_query($query) or die('DB Error - Query failed: '.mysql_error()); } } } } } else { if($debug) echo('No database host or no username entered.<br>'."n"); } ?>



Răspunde cu citat