Guest User

Untitled

a guest
Nov 29th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.86 KB | None | 0 0
  1. // main.php
  2. <?php
  3. // usage:
  4. // $db = new database();                 creates a connection
  5. // $db->create_table();                  creates the appropriate table (currencies)
  6. //                                       should only run once.
  7. // $db->create('EUR','USD',1.5,FALSE);   creates a row for the pair EUR,USD with the value of 1.5
  8. // $db->create('EUR','USD',1.5,TRUE);    creates a row for the pair EUR,USD as well as the pair USD,EUR
  9. //                                       which will have the inverted value (i.e. 5 will turn to 0.2).
  10. // $db->update('EUR','USD',3.0,FALSE);   updates the pair of EUR,USD to have the value of 3.0.
  11. //                                       will only work on existing rows.
  12. // $db->update('EUR','USD',3.0,TRUE);    updates the pair of EUR,USD to have the value of 3,
  13. //                                       but also updates the pair of USD,EUR to have the value of 1/3.
  14. // echo $db->value('EUR','USD');         echoes the value of the pair EUR,USD.
  15. //
  16. //                                       important note:
  17. //                                       value will return a string error rather than a double
  18. //                                       in case something is wrong with value!
  19.  
  20. // notes:
  21. // the login.php file should be modified.
  22. // the pair EUR,USD having the value 1.25 would imply that 1 EUR = 1.25 USD.
  23. // currency codes should be in uppercase and comply with ISO 4217 standards.
  24. // the database stores the value as a double.
  25.  
  26. class database extends mysqli {
  27.  
  28.     public function __construct() {
  29.         include_once 'login.php';
  30.         $this->mysqli = parent::__construct($hostname,$username,$password,$database);
  31.     }
  32.  
  33.     // creates a new row.
  34.     function create($currency1, $currency2, $value, $invert) {
  35.         // if one of the two currency codes entered isn't a valid ISO 4217 code,
  36.         // throws an exception.
  37.         include_once 'validate.php';
  38.         if (!(validate($currency1) && validate($currency2))) {
  39.             $error = (string) 'Error: Some currency codes are not valid ISO 4217 currency code.\nTested: ' . $currency1 .', ' . $currency2;
  40.             throw new Exception($error);
  41.         }
  42.         // ensures $value is a double
  43.         if (is_double($value)) {
  44.             if ($invert) {
  45.                 // also creates the inverted value to the database (i.e. in the case of EUR/USD, creates USD/EUR).
  46.                 $inverted_value = $this->invert($value);
  47.                 $this->create($currency2, $currency1, $inverted_value, FALSE);
  48.             }  
  49.             $query = "insert into currencies(value, currency1, currency2) values(?, ?, ?)";
  50.             if ($stmt = parent::prepare($query)) {  
  51.                 $stmt->bind_param('dss',$value, $currency1, $currency2);
  52.                 $stmt->execute();
  53.                 $stmt->close();
  54.             }
  55.             else {
  56.                 $error = 'Problem with creating row: ' . $query . '.\n' . $this->mysqli->error;
  57.                 exit($error);
  58.             }
  59.         }
  60.         else {
  61.             $error = "Value $value inserted into function create is not a double!";
  62.             exit($error);
  63.         }
  64.     }
  65.  
  66.     // used to update (EXISTING) rows.
  67.     function update($currency1, $currency2, $value, $invert) {
  68.         // ensures $value is a double
  69.         if (is_double($value)) {
  70.             if ($invert) {
  71.                 $inverted_value = $this->invert($value);
  72.                 $this->update($currency2, $currency1, $inverted_value, FALSE);
  73.             }
  74.        
  75.             $query = "update currencies SET value = ? where currency1 = ? and currency2 = ?";
  76.             if ($stmt = parent::prepare($query)) {  
  77.                 $stmt->bind_param('dss', $value, $currency1, $currency2);
  78.                 $stmt->execute();
  79.                 $stmt->close();
  80.             }
  81.             else {
  82.                 $error = 'Problem with updating row: ' . $query . '.\n' . $this->mysqli->error;
  83.                 exit($error);
  84.             }
  85.         }
  86.         else {
  87.             $error = "Value $value inserted into function update is not a double!";
  88.             exit($error);
  89.         }
  90.     }
  91.  
  92.     function invert($number) {
  93.         return (double) bcpow($number, '-1', 20);
  94.     }
  95.  
  96.     // creates the table. should only run once.
  97.     function create_table() {
  98.     $query =
  99.     "create table currencies (
  100.     currency1   varchar(3),
  101.     currency2   varchar(3),
  102.     value       double,
  103.     primary key (currency1, currency2)
  104.    )";
  105.  
  106.     if ($stmt = parent::prepare($query)) {  
  107.         $stmt->execute();
  108.         $stmt->close();
  109.         }
  110.     else {
  111.             $error = 'Problem with creating table: ' . $query . '.\n' . $this->mysqli->error;
  112.             exit($error);
  113.     }
  114.     }
  115.  
  116.     // returns the value of a currency pair. the returned value will be of type double.
  117.     function value($currency1, $currency2) {
  118.         $query = "SELECT value FROM currencies WHERE currency1 = ? AND currency2 = ?";
  119.         if ($stmt = parent::prepare($query)) {  
  120.             $stmt->bind_param('ss',$currency1, $currency2);
  121.             $stmt->execute();
  122.             $stmt->bind_result($value);
  123.             $stmt->fetch();
  124.             $stmt->close();
  125.             return $value;
  126.         }
  127.         else {
  128.             $error = (string) "Problem with finding value: " . $query . $this->mysqli->error;
  129.             exit($error);
  130.         }
  131.     }
  132.    
  133.     function __destruct() {
  134.         parent::close();
  135.     }
  136. }
  137.  
  138. ?>
  139.  
  140.  
  141. //login.php
  142. <?php
  143.  
  144. // MySQL login data    ↴
  145.  
  146. $username = (string) 'user';
  147. $password = (string) 'pass';
  148. $hostname = (string) 'localhost';
  149. $database = (string) 'db';
  150.  
  151.  
  152. ?>
  153. //validate.php
  154. <?php
  155. // validate checks if $currency is a valid ISO 4217 currency code.
  156. function validate($currency) {
  157.     $currencies = array('AED','AFN','ALL','AMD','ANG','AOA','ARS','AUD','AWG','AZN','BAM','BBD','BDT','BGN','BHD','BIF','BMD','BND','BOB','BOV','BRL','BSD','BTN','BWP','BYR','BZD','CAD','CDF','CHE','CHF','CHW','CLF','CLP','CNY','COP','COU','CRC','CUC','CUP','CVE','CZK','DJF','DKK','DOP','DZD','EEK','EGP','ERN','ETB','EUR','FJD','FKP','GBP','GEL','GHS','GIP','GMD','GNF','GTQ','GWP','GYD','HKD','HNL','HRK','HTG','HUF','IDR','ILS','INR','IQD','IRR','ISK','JMD','JOD','JPY','KES','KGS','KHR','KMF','KPW','KRW','KWD','KYD','KZT','LAK','LBP','LKR','LRD','LSL','LTL','LVL','LYD','MAD','MDL','MGA','MKD','MMK','MNT','MOP','MRO','MUR','MVR','MWK','MXN','MXV','MYR','MZN','NAD','NGN','NIO','NOK','NPR','NZD','OMR','PAB','PEN','PGK','PHP','PKR','PLN','PYG','QAR','RON','RSD','RUB','RWF','SAR','SBD','SCR','SDG','SDR','SEK','SGD','SHP','SLL','SOS','SRD','STD','SVC','SYP','SZL','THB','TJS','TMT','TND','TOP','TRY','TTD','TWD','TZS','UAH','UGX','USD','USN','USS','UYI','UYU','UZS','VEF','VND','VUV','WST','XAF','XAG','XAU','XBA','XBB','XBC','XBD','XCD','XDR','XFU','XOF','XPD','XPF','XPT','XTS','XXX','YER','ZAR','ZMK','ZWL');
  158.     return in_array($currency, $currencies);
  159. }
  160. ?>
Add Comment
Please, Sign In to add comment