• We just launched and are currently in beta. Join us as we build and grow the community.

Simple Currency Converter in PHP using OOP Approach

fox1

CTR Booster
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
160
Likes
171
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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.

  1. <?php

  2. class

    Converter{

  3. private

    $rateValue

    ;

  4. //I have base these rates on USD :)
  5. private

    $rates

    =

    [
  6. 'USD'

    =>

    1.0

    ,
  7. 'GBP'

    =>

    0.7

    ,
  8. 'EUR'

    =>

    0.800284

    ,
  9. 'YEN'

    =>

    109.67

    ,
  10. 'CAN'

    =>

    1.23

    ,
  11. 'PHP'

    =>

    51.74

    ,
  12. ]

    ;

  13. public

    function

    setConvert(

    $amount

    ,

    $currency_from

    )

    {
  14. $this

    ->

    rateValue

    =

    $amount

    /

    $this

    ->

    rates

    [

    $currency_from

    ]

    ;
  15. }

  16. public

    function

    getConvert(

    $currency_to

    )

    {
  17. return

    round

    (

    $this

    ->

    rates

    [

    $currency_to

    ]

    *

    $this

    ->

    rateValue

    ,

    2

    )

    ;
  18. }

  19. public

    function

    getRates(

    )

    {
  20. return

    $this

    ->

    rates

    ;
  21. }
  22. }

  23. ?>

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.

  1. <?php

  2. session_start

    (

    )

    ;

  3. include_once

    (

    'Converter.php'

    )

    ;

  4. $converter

    =

    new

    Converter(

    )

    ;

  5. $rates

    =

    $converter

    ->

    getRates

    (

    )

    ;

  6. ?>
  7. <!DOCTYPE html>
  8. <html>
  9. <head>
  10. <title>Simple Currency Converter in PHP using OOP Approach</title>
  11. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  12. </head>
  13. <body>
  14. <div class="container">
  15. <h1 class="page-header text-center">Simple Currency Converter in PHP using OOP Approach</h1>
  16. <div class="row">
  17. <div class="col-sm-4 col-sm-offset-4">
  18. <form method="POST" action="getconvert.php">
  19. <div class="form-group">
  20. <label>Amount:</label>
  21. <input type="text" class="form-control" name="amount">
  22. </div>
  23. <div class="form-group">
  24. <label>From:</label>
  25. <select class="form-control" name="currency_from">
  26. <?php
  27. foreach

    (

    $rates

    as

    $key

    =>

    $currency

    )

    {
  28. ?>
  29. <option value="<?php

    echo

    $key

    ;

    ?>

    "><?php

    echo

    $key

    ?>

    </option>
  30. <?php
  31. }

  32. ?>
  33. </select>
  34. </div>
  35. <div class="form-group">
  36. <label>To:</label>
  37. <select class="form-control" name="currency_to">
  38. <?php
  39. foreach

    (

    $rates

    as

    $key

    =>

    $currency

    )

    {
  40. ?>
  41. <option value="<?php

    echo

    $key

    ;

    ?>

    "><?php

    echo

    $key

    ?>

    </option>
  42. <?php
  43. }

  44. ?>
  45. </select>
  46. </div>
  47. <button type="submit" name="convert" class="btn btn-primary">Convert</button>
  48. </form>

  49. <?php
  50. if

    (

    isset

    (

    $_SESSION

    [

    'value'

    ]

    )

    )

    {
  51. ?>
  52. <div class="alert alert-info text-center" style="margin-top:20px;">
  53. <?php
  54. echo

    $_SESSION

    [

    'value'

    ]

    [

    'amount'

    ]

    .

    ' '

    .

    $_SESSION

    [

    'value'

    ]

    [

    'from'

    ]

    .

    ' is equal to '

    .

    $_SESSION

    [

    'value'

    ]

    [

    'result'

    ]

    .

    ' '

    .

    $_SESSION

    [

    'value'

    ]

    [

    'to'

    ]

    ;
  55. ?>
  56. </div>
  57. <?php
  58. unset

    (

    $_SESSION

    [

    'value'

    ]

    )

    ;
  59. }
  60. ?>
  61. </div>
  62. </div>
  63. </div>
  64. </body>
  65. </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.

  1. <?php

  2. session_start

    (

    )

    ;

  3. require_once

    (

    'Converter.php'

    )

    ;

  4. if

    (

    isset

    (

    $_POST

    [

    'convert'

    ]

    )

    )

    {
  5. $amount

    =

    $_POST

    [

    'amount'

    ]

    ;
  6. $currency_from

    =

    $_POST

    [

    'currency_from'

    ]

    ;
  7. $currency_to

    =

    $_POST

    [

    'currency_to'

    ]

    ;

  8. $converter

    =

    new

    Converter(

    )

    ;
  9. $converter

    ->

    setConvert

    (

    $amount

    ,

    $currency_from

    )

    ;
  10. $result

    =

    $converter

    ->

    getConvert

    (

    $currency_to

    )

    ;

  11. $out

    =

    array

    (

    )

    ;
  12. $out

    [

    'amount'

    ]

    =

    $amount

    ;
  13. $out

    [

    'from'

    ]

    =

    $currency_from

    ;
  14. $out

    [

    'result'

    ]

    =

    $result

    ;
  15. $out

    [

    'to'

    ]

    =

    $currency_to

    ;

  16. $_SESSION

    [

    'value'

    ]

    =

    $out

    ;
  17. header

    (

    'location:index.php'

    )

    ;

  18. }
  19. else

    {
  20. header

    (

    'location:index.php'

    )

    ;
  21. }

  22. ?>

That ends this tutorial. Happy coding :)


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

336,529

336,537

Top