Jump to content

[SOLVED] How do I create a cost calculator


tonylees

Recommended Posts

HELP! I am in the process of creating a online shop selling kitchen cupboard doors, drawer fronts etc, BUT these are bespoke products that are not sold in a standard size! So I need the customer to be able to enter the height and the width and then the website provide the final price. Can anyone guide me in the right direction? ;D

 

I am a newbie to this so I apologize if this is basic information!

 

Thanks for your help (in advance)

 

Tony

Link to comment
Share on other sites

Since you're using a variable cost you're best bet is probably finding the total area and multiplying it by a per square inch cost to find a total cost.

 

Simple example:

 

$width = 16;
$height = 35;
$variable = .5;
$area = $width * $height;
$totalCost = $area * $variable; // will result in $280 for the door

Link to comment
Share on other sites

A price per square meter is a variable cost, the variable being how many square meters you're buying, so the same theory applies.  If the cost per square meter is ALWAYS the same you could just hard code it into php.  However if the cost per square meter is different depending on the material being purchased, I'd recommend adding it into a database table.

Link to comment
Share on other sites

I think the database would be the best option.

 

As I said I am New to all this so please be patient.... I have just attempted a quick script

 

<label>Width

<input type="text" name="textfield" /> <?php

$width="";

?>

</label><br />

<label>Height

<input type="text" name="textfield2" /><?php

$height="";

?>

</label><br/>

<label>Cost

<input type="text" name="textfield3" />

</label>

<?php

$variable = 1;

$area = $width * $height;

$totalCost = $area * $variable;

 

?>

 

Where am I going wrong?

 

I have the text boxes that I can type the sizes in but i don't get any results

 

Thanks

Tony

Link to comment
Share on other sites

I think the database would be the best option.

 

As I said I am New to all this so please be patient.... I have just attempted a quick script

 

<label>Width

<input type="text" name="textfield" /> <?php

$width="";

?>

</label><br />

<label>Height

<input type="text" name="textfield2" /><?php

$height="";

?>

</label><br/>

<label>Cost

<input type="text" name="textfield3" />

</label>

<?php

$variable = 1;

$area = $width * $height;

$totalCost = $area * $variable;

 

?>

 

Where am I going wrong?

 

I have the text boxes that I can type the sizes in but i don't get any results

 

Thanks

Tony

 

you should use actaul names for the fields, makes less confusion...

 

also, are you wanting to show them the cost on a new rpage, or on the same page?

 

this is pretty much it.... i think.

 


<?php

if (isset($_POST['submit'])) {

$width = $_POST["width"];
$height = $_POST["height"];

$variable = 1;
$area = $width * $height;
$totalCost = $area * $variable;

echo 'Total Cost=';
echo $totalCost;
}

?>

<form name="cost_finder" method="post">
<label>Width
<input type="text" name="width" />
</label><br />
<label>Height
<input type="text" name="height" />
</label><br/>
<input type="submit" name="submit" value="Calculate Cost!"/>
</form>


Link to comment
Share on other sites

Of course, when you use echo you're actually printing html code.

 

Simple Example:

 

// All you calculations were done above

echo "<p style=\"color: #ccc; font-weight: bold;\">$totalCost</p>"; // you can have whatever styles you want.  You can display any html, just remember to escape double quotes

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.