Azrath Posted September 30, 2012 Share Posted September 30, 2012 (edited) I am relatively new to PHP and I have an assignment where I need to write a script that will calculate the correct amount of change when performing a cash transaction. When I run the code I see that the variables obtain the input in the address bar but neither the error shows if the amount paid is too low or the correct change shows if the amount paid is higher than the cost. Below is my code and it is in a file named Semina2_lab1.php <?php if ($_GET['submit'] == 'Calculate Change') { $errorMessage = ""; if (empty($_GET['price'])) { $errorMessage = 'You forgot to enter a price.'; } if (empty($_GET['paid'])) { $errorMessage = 'You forgot to enter a paid amount.'; } $price = $_GET['price']; $paid = $_GET['paid']; if (empty($errorMessage)) { if ($paid < $price) { $alert = 'The amount '.$paid.' will not pay for merchandise costing '.$price.'.'; } else { $total = $paid - $price; $dollars = (int)$total; $tempquarters = ($total - $dollars)/.25; $quarters = (int)$tempquarters; $tempdimes = ($tempquarters - $quarters)/.1; $dimes = (int)$dimes; $tempnickles = ($tempdimes - $dimes)/.5; $nickles = (int)$tempnickles; $pennies = $tempnickles %.5; $alert = 'Here is your change:\n$1 bills: '.$dollars.'\nQuarters: '.$quarters.'\nDimes: '.$dimes.'\nNickles: '.$nickles.'\nPennies: '.$pennies.'\n'; } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[url="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd%22>"]http://www.w3.org/TR...nsitional.dtd">[/url] <html xmlns="[url="http://www.w3.org/1999/xhtml%22>"]http://www.w3.org/1999/xhtml">[/url] <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link type="text/css" rel="stylesheet" href="style.css" /> <title>Seminar 2 Lab 1</title> </head> <body> <div class="container"> <h2>Change Maker</h2><br /> <hr /> <?php echo $price.$paid; if (!empty($errorMessage)) { echo $errorMessage.'\n'; } else { echo $alert; } ?> <div class="content"> Enter the price of the merchandise and the amount paid.<br /><br /> <form action="Seminar2_lab1.php" method="get" name="change"> $<input id="price" name="price" type="text" size="15px"/> Price of Merchandise<br /><br /> $<input id="paid" name="paid" type="text" size="15px"/> Amount Paid<br /><br /> <input id="button" type="submit" value="Calculate Change" /> </form> </div><!-- end of content --> </div><!-- end of container --> </body> </html> Edited October 1, 2012 by PFMaBiSmAd code in code tags please Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 30, 2012 Share Posted September 30, 2012 turn on error reporting, and start adding echo's at various points in the code to track down where your problem is. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 30, 2012 Share Posted September 30, 2012 I'll throw you a softball for the first problem: your <input type="submit" needs a name= attribute. Quote Link to comment Share on other sites More sharing options...
markschum Posted October 1, 2012 Share Posted October 1, 2012 $tempdimes = ($tempquarters - $quarters)/.1; that should be a 10 surely. I dont understand your logic . amount tendered - price = change int(change) = whole dollars change - dollars = change but then your change amount is a decimal 3.76 - 3.00 = 0.76 you cant work easily on that so convert to pennies then use 25,10,5,1 as your coins Int(change/25) = quarters change - (quarters*25) = change int(change/10) = DIMES change - (dimes * 10) = change etc modulo might be easier but I dont know if there is php for that. that gives you how many dollars, quarters, dimes , nickles and pennies you give as change. I dont know if that helps. I am new to php. Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 1, 2012 Share Posted October 1, 2012 (edited) $tempdimes = ($tempquarters - $quarters)/.1; that should be a 10 surely. I dont understand your logic . Int(change/25) = quarters change - (quarters*25) = change int(change/10) = DIMES change - (dimes * 10) = change etc that gives you how many dollars, quarters, dimes , nickles and pennies you give as change. I dont know if that helps. I am new to php. And apparently have never worked a cash register. Edited October 1, 2012 by Jessica Quote Link to comment Share on other sites More sharing options...
markschum Posted October 2, 2012 Share Posted October 2, 2012 sorry Jessica , dont understand your comment. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.