Simple Currency Converter in PHP using OOP Approach
Language:
Visitors have accessed this post 637 times.
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; } } ?>
In this class we have define our array of rates and the different methods.
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="<a href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"" rel="nofollow">https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"</a> 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>
In here, we fetch our rates and we put them as options in our select tag.
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{ } ?>
That ends this tutorial. Happy coding :)
- 637 reads
Add new comment