Advertisement
Guest User

k + sjuk - sj

a guest
Sep 18th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.81 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html lang="sv">
  4.  
  5. <head>
  6. <title>Cash Calculator</title>
  7. <meta charset="UTF-8">
  8. <link rel="stylesheet" type="text/css" href="cashcalculator.css">
  9. <link href="https://fonts.googleapis.com/css?family=Roboto+Condensed&display=swap" rel="stylesheet">
  10. </head>
  11.  
  12. <body>
  13.  
  14. <h3>Beräkna summan av dina kontanter</h3>
  15. <p>Icke-ifyllda fält/icke-numeriska värden kommer att ersättas med 0.</p>
  16.  
  17. <form action="cashcalculator.php" method="post">
  18.     <input type="text" placeholder="1000kr-sedlar..." name="onethousand">
  19.     <input type="text" placeholder="500kr-sedlar..." name="fivehundred">
  20.     <input type="text" placeholder="200kr-sedlar..." name="twohundred">
  21.     <input type="text" placeholder="100kr-sedlar..." name="onehundred">
  22.     <input type="text" placeholder="50kr-sedlar..." name="fifty">
  23.     <input type="text" placeholder="20kr-sedlar..." name="twenty">
  24.     <input type="text" placeholder="10kr-mynt..." name="ten">
  25.     <input type="text" placeholder="5kr-mynt..." name="five">
  26.     <input type="text" placeholder="2kr-mynt..." name="two">
  27.     <input type="text" placeholder="1kr-mynt..." name="one">
  28.     <button type="submit" name="submit">Beräkna</button>
  29. </form>
  30.  
  31. <br>
  32.  
  33. <?php
  34.  
  35. if(isset($_POST['submit'])) {
  36.  
  37.     $amountOfBills = array(
  38.         $_POST['onethousand'],
  39.         $_POST['fivehundred'],
  40.         $_POST['twohundred'],
  41.         $_POST['onehundred'],
  42.         $_POST['fifty'],
  43.         $_POST['twenty']
  44.     );
  45.  
  46.     $amountOfCoins = array(
  47.         $_POST['ten'],
  48.         $_POST['five'],
  49.         $_POST['two'],
  50.         $_POST['one']
  51.     );
  52.  
  53.     for($i = 0; $i < count($amountOfBills); $i++) {
  54.         if(!is_numeric($amountOfBills[$i])) {
  55.             $amountOfBills[$i] = 0;
  56.         }
  57.     }
  58.  
  59.     for($i = 0; $i < count($amountOfCoins); $i++) {
  60.         if(!is_numeric($amountOfCoins[$i])) {
  61.             $amountOfCoins[$i] = 0;
  62.         }
  63.     }
  64.  
  65.     $billDenominations = array(1000, 500, 200, 100, 50, 20);
  66.     $valueOfBills = array();
  67.  
  68.     for($i = 0; $i < count($amountOfBills); $i++) {
  69.         array_push($valueOfBills, $amountOfBills[$i] * $billDenominations[$i]);
  70.     }
  71.  
  72.     $coinDenominations = array(10, 5, 2, 1);
  73.     $valueOfCoins = array();
  74.  
  75.     for($i = 0; $i < count($amountOfCoins); $i++) {
  76.         array_push($valueOfCoins, $amountOfCoins[$i] * $coinDenominations[$i]);
  77.     }
  78.  
  79.     $totalAmountOfBills = array_sum($amountOfBills);
  80.     $totalAmountOfCoins = array_sum($amountOfCoins);
  81.     $totalValueOfBills = array_sum($valueOfBills);
  82.     $totalValueOfCoins = array_sum($valueOfCoins);
  83.     $totalAmountOfCash = $totalValueOfBills + $totalValueOfCoins;
  84.  
  85.     echo 'Sedlar: ' . $totalValueOfBills . 'kr (' . $totalAmountOfBills . 'st sedlar)<br>';
  86.     echo 'Mynt: ' . $totalValueOfCoins . 'kr (' . $totalAmountOfCoins . 'st mynt)<br>';
  87.     echo 'Kontanter: ' . $totalAmountOfCash . 'kr';
  88.  
  89. }
  90.  
  91. ?>
  92.  
  93. </body>
  94.  
  95. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement