Simple Currency Converter in PHP using OOP Approach
Submitted by nurhodelta_17 on Friday, February 2, 2018 - 22:10.
Getting Started
To improve the visual of this tutorial, I've used CDN for Bootstrap so you need internet connection for it to work.Creating our Class
First, we are going to create our class and we're going to name this as Converter.php.- <?php
- class Converter{
- private $rateValue;
- //I have base these rates on USD :)
- private $rates = [
- 'USD' => 1.0,
- 'GBP' => 0.7,
- 'EUR' => 0.800284,
- 'YEN' => 109.67,
- 'CAN' => 1.23,
- 'PHP' => 51.74,
- ];
- public function setConvert($amount, $currency_from){
- $this->rateValue = $amount/$this->rates[$currency_from];
- }
- public function getConvert($currency_to){
- }
- public function getRates(){
- return $this->rates;
- }
- }
- ?>
Creating our Convert Form
Next step is to create our form that converts our set amount from one currency to another.- <?php
- include_once('Converter.php');
- $converter = new Converter();
- $rates = $converter->getRates();
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Simple Currency Converter in PHP using OOP Approach</title>
- <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
- </head>
- <body>
- <div class="container">
- <h1 class="page-header text-center">Simple Currency Converter in PHP using OOP Approach</h1>
- <div class="row">
- <div class="col-sm-4 col-sm-offset-4">
- <form method="POST" action="getconvert.php">
- <div class="form-group">
- <label>Amount:</label>
- <input type="text" class="form-control" name="amount">
- </div>
- <div class="form-group">
- <label>From:</label>
- <select class="form-control" name="currency_from">
- <?php
- foreach ($rates as $key => $currency){
- ?>
- <option value="<?php echo $key; ?>"><?php echo $key ?></option>
- <?php
- }
- ?>
- </select>
- </div>
- <div class="form-group">
- <label>To:</label>
- <select class="form-control" name="currency_to">
- <?php
- foreach ($rates as $key => $currency){
- ?>
- <option value="<?php echo $key; ?>"><?php echo $key ?></option>
- <?php
- }
- ?>
- </select>
- </div>
- <button type="submit" name="convert" class="btn btn-primary">Convert</button>
- </form>
- <?php
- ?>
- <div class="alert alert-info text-center" style="margin-top:20px;">
- <?php
- echo $_SESSION['value']['amount'].' '.$_SESSION['value']['from'].' is equal to '.$_SESSION['value']['result'].' '.$_SESSION['value']['to'];
- ?>
- </div>
- <?php
- }
- ?>
- </div>
- </div>
- </div>
- </body>
- </html>
Creating our Convert Action
Lastly, we create our PHP code to convert the amount.- <?php
- require_once('Converter.php');
- $amount = $_POST['amount'];
- $currency_from = $_POST['currency_from'];
- $currency_to = $_POST['currency_to'];
- $converter = new Converter();
- $converter->setConvert($amount, $currency_from);
- $result = $converter->getConvert($currency_to);
- $out['amount'] = $amount;
- $out['from'] = $currency_from;
- $out['result'] = $result;
- $out['to'] = $currency_to;
- $_SESSION['value'] = $out;
- }
- else{
- }
- ?>
Add new comment
- 424 views