PHP Currency Converter using OOP API

PHP Currency Converter using OOP API

In this Post We Will Explain About is PHP Currency Converter using OOP API With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Currency Converter in PHP using OOP Example

In this post we will show you Best way to implement PHP Currency Converter using OOP, hear for PHP Object Oriented Programming using Currency Converter with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.




Creating simple Class

First of all, I am going to make simple class and I am going to name this as currency_converter.

Also Read :   Magento2 Get Logged in Customer Data using Email

<?php
 
class Converter{
 
	private $rateValue;
 
	//We have main config base these rates on live 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, $live_form_currency){
		$this->rateValue = $amount/$this->rates[$live_form_currency];
	}
 
	public function fetchConvert($live_to_currency){
                return round($this->rates[$live_to_currency] * $this->rateValue, 2);
	}
 
	public function getRates(){
		return $this->rates;
	}
}
 
?>

In this PHP class we have simple List of the define simple PHP array of main rates and the different PHP methods.

Creating simple Convert Form

And then step is to create simple form that converts simple set amount from one currency to another.

Also Read :   Login with Facebook using PHP SDK with mysql download

<?php
 
session_start();
 
include_once('currency_converter');
 
$cur_converter = new Converter();
 
$rates = $cur_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-left">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="live_form_currency">
						<?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="live_to_currency">
						<?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-success">Convert</button>
			</form>
 
			<?php
				if(isset($_SESSION['value'])){
					?>
					<div class="alert alert-info text-left" style="margin-top:22px;">
						<?php 
							echo $_SESSION['value']['amount'].' '.$_SESSION['value']['from'].' is equal to '.$_SESSION['value']['result'].' '.$_SESSION['value']['to']; 
						?>
					</div>
					<?php
					unset($_SESSION['value']);
				}
			?>
		</div>
	</div>
</div>
</body>
</html>

In here, we Retrive simple rates and we put them as options in simple select tag.

Also Read :   Simple Currency Converter in PHP API

Creating simple Convert Action

Last step, we create simple PHP source code to convert the rs- amount.

<?php
 
session_start();
 
require_once('currency_converter');
 
if(isset($_POST['convert'])){
	$amount = $_POST['amount'];
	$live_form_currency = $_POST['live_form_currency'];
	$live_to_currency = $_POST['live_to_currency'];
 
	$cur_converter = new Converter();
	$cur_converter->setConvert($amount, $live_form_currency);
	$result = $cur_converter->fetchConvert($live_to_currency);
 
	$results = array();
	$results['amount'] = $amount;
	$results['from'] = $live_form_currency;
	$results['result'] = $result;
	$results['to'] = $live_to_currency;
 
	$_SESSION['value'] = $results;
	header('location:index.php');
 
}
else{
//redirect page
	header('location:index.php');
}
 
?>

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example





Example

I hope you have Got What is Simple Currency Converter in PHP using OOP Approach And how it works.I would Like to have FeedBack From My Blog(live24u.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(live24u.com) Are Most Always Welcome.