Jump to content

I'm Stuck With A Php Assignment


Azrath

Recommended Posts

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 by PFMaBiSmAd
code in code tags please
Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

$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 by Jessica
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.