mallen Posted December 8, 2008 Share Posted December 8, 2008 I am developing page that will allow the user to select quantities of CDs, choose printing choices such as color, jewel case, and packaging. I have never creating anything like this before so here is what I ahve so far. One page called estimate.php and another page called calculate.php <form name="form1" id="form1" method="post" action="calculate.php"> <table width="217" border="1" cellspacing="0" cellpadding="0"> <tr> <td width="114">CD</td> <td width="573"><select name="cdQuantity" id="cdQuantity"> <option value="8.00">1-10</option> <option value="7.00">11-24</option> <option value="6.00">25-49</option> <option value="5.00">50-99</option> <option value="3.00">100-249</option> <option value="2.00">250-499</option> <option value="1.50">500-999</option> <option value="1.15">1,000-2,499</option> <option value=".75">2,500-4,999</option> <option value=".60">5,000-9,999</option> <option>10,000 +</option> </select></td> </tr> <tr> <td>Labelling</td> <td><select name="cdLabel" id="cdLabel"> <option value="0">None</option> <option value=".10">Black on silver</option> <option value=".20">Black on white</option> <option value=".30">Color on Silver</option> <option value=".40">Color on white</option> </select></td> </tr> <tr> <td>Packaging</td> <td><select name="cdPackage" id="cdPackage"> <option value="0">Bulk</option> <option value=".03">Paper Envelope</option> <option value=".23">Clam Shell</option> <option value=".25">Slim Jewel Case</option> <option value=".38">Full Jewl Case</option> <option value=".75">DVD Box</option> </select></td> </tr> <tr> <td>Printing</td> <td><select name="cdPrint" id="cdPrint"> <option value="0">None</option> <option value="1.00">Slim Jewel Case Top</option> <option value=".85">Full Jewel case Top and Back</option> </select></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td><input type="submit" name="Submit" value="Submit" /></td> <td> </td> </tr> </table> </form> And here is the calculate page: <?php // address error handling. ini_set ('display_errors',1); error_reporting (E_ALL & ~E_NOTICE); $cdQuantity = $POST['cdQuantity']; $cdLabel = $POST['cdLabel']; $cdPackage = $POST['cdPackage']; $cdPrint = $POST['cdPrint']; //Calculate total. $total = $cdQuantity * $price; //Print out results. print "You have selected to order $cdQuantity CDs at <br /> $<b>$cdPrice</b> Your estimated cost is $<b>$total;" ?> I assume I need to create separate functions that will assign a value to $price since price changes per quantity. Can someone lookit over and tell me if I am doing this right? Quote Link to comment https://forums.phpfreaks.com/topic/136066-creating-an-estimate-form/ Share on other sites More sharing options...
JonnoTheDev Posted December 8, 2008 Share Posted December 8, 2008 Should be $_POST not $POST Rest is OK. Quote Link to comment https://forums.phpfreaks.com/topic/136066-creating-an-estimate-form/#findComment-709485 Share on other sites More sharing options...
Adam Posted December 8, 2008 Share Posted December 8, 2008 I don't understand the cdQuantity drop down? How does: 5,000-9,999 = .60 ?? You're obviously going to run into problems when multiply that by the price... Perhaps it would be easier to let them enter them enter the amount they wish to purchase, validate that as a number and then multiply it by the price to give them the current total? Since you're using strange values for each of the drop down menus I'd suggest revising what they actually mean.. They're going to prove difficult to work with! Adam Quote Link to comment https://forums.phpfreaks.com/topic/136066-creating-an-estimate-form/#findComment-709486 Share on other sites More sharing options...
mallen Posted December 8, 2008 Author Share Posted December 8, 2008 Here is the function I am trying to figure out the choice they made and assign a price. <?php // functions to generate labeling choice, packaging choice and printing choice. //assign a value for labeling choice price function get_cdLabel () { switch ($cdQuantity) { case 1: if $cdLabel == '1-10' blk_on_silv ='.0'; blk_on_wht ='.01'; col_on_silv ='.5'; col_on_white '.6'; if $cdLabel == '11-24' blk_on_silv ='.1'; blk_on_wht ='.02'; col_on_silv ='.03'; col_on_white '.04'; //and so on..... break; } //assign a value for packaging price function get_cdPackage () { } //assign a value for printing price function get_cdPrint () { } ?> Quote Link to comment https://forums.phpfreaks.com/topic/136066-creating-an-estimate-form/#findComment-709534 Share on other sites More sharing options...
mallen Posted December 8, 2008 Author Share Posted December 8, 2008 5,000-9,999 = .60 would be is they were ordering 5,000 CDs up to 9,999 the cost would be 60 cents each. I don't understand the cdQuantity drop down? How does: 5,000-9,999 = .60 ?? You're obviously going to run into problems when multiply that by the price... Perhaps it would be easier to let them enter them enter the amount they wish to purchase, validate that as a number and then multiply it by the price to give them the current total? Since you're using strange values for each of the drop down menus I'd suggest revising what they actually mean.. They're going to prove difficult to work with! Adam Quote Link to comment https://forums.phpfreaks.com/topic/136066-creating-an-estimate-form/#findComment-709718 Share on other sites More sharing options...
mallen Posted December 8, 2008 Author Share Posted December 8, 2008 Would creating an array be a better approach? Quote Link to comment https://forums.phpfreaks.com/topic/136066-creating-an-estimate-form/#findComment-709785 Share on other sites More sharing options...
mallen Posted December 9, 2008 Author Share Posted December 9, 2008 Can anyone help? I have to get this done in a couple days. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/136066-creating-an-estimate-form/#findComment-710104 Share on other sites More sharing options...
mallen Posted December 9, 2008 Author Share Posted December 9, 2008 I found this info http://ca.php.net/language.variables so now I have to find a way to assign a variable to each one like $cdQuantity, $cdLabel, $cdPackaging, $cdPrinting ??? Quote Link to comment https://forums.phpfreaks.com/topic/136066-creating-an-estimate-form/#findComment-710460 Share on other sites More sharing options...
mallen Posted December 9, 2008 Author Share Posted December 9, 2008 Can't figure out how to post a table here to show the price structure. <table width="430" border="1" cellspacing="0" cellpadding="0"> <tr> <td> </td> <td>CD</td> <td>Label Choice</td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td width="89"> </td> <td width="44"> </td> <td width="67">None</td> <td width="50">blk on <br /> silver </td> <td width="58">blk on <br /> white </td> <td width="50">col on <br /> silv </td> <td width="56">color on white </td> </tr> <tr> <td>1-10 copies </td> <td>$8.00</td> <td>0</td> <td>0</td> <td>0.1</td> <td>0.5</td> <td>0.6</td> </tr> <tr> <td>11-24</td> <td>7.00</td> <td>0</td> <td>0</td> <td>0.1</td> <td>0.5</td> <td>0.6</td> </tr> <tr> <td>25-49</td> <td>6.00</td> <td>0</td> <td>0</td> <td>0.1</td> <td>0.5</td> <td>0.6</td> </tr> <tr> <td>50-99</td> <td>5.00</td> <td>0</td> <td>0</td> <td>0.1</td> <td>0.5</td> <td>0.6</td> </tr> <tr> <td>100-249</td> <td>3.00</td> <td>0</td> <td>0</td> <td>0.08</td> <td>0.4</td> <td>0.45</td> </tr> <tr> <td>250-499</td> <td>2.00</td> <td>0</td> <td>0</td> <td>0.07</td> <td>0.4</td> <td>0.45</td> </tr> <tr> <td>500-999</td> <td>1.50</td> <td>0</td> <td>0</td> <td>0.06</td> <td>0.35</td> <td>0.4</td> </tr> <tr> <td>1,000-2,499</td> <td>1.15</td> <td>0</td> <td>0</td> <td>0.06</td> <td>0.35</td> <td>0.4</td> </tr> <tr> <td>2,500-4,999</td> <td>.75</td> <td>0</td> <td>0.02</td> <td>0.05</td> <td>0.3</td> <td>0.35</td> </tr> <tr> <td>5,000-9,999</td> <td>.60</td> <td>0</td> <td>0.02</td> <td>0.05</td> <td>0.2</td> <td>0.22</td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/136066-creating-an-estimate-form/#findComment-710507 Share on other sites More sharing options...
mallen Posted December 9, 2008 Author Share Posted December 9, 2008 It calculates and prints the price. But I have to find a way to determine individual price becuase if they choose 1-10 CDs the price each for Color on Silver is 0.5 but not if they choose another quantity. So would I need a function to determine this? <?php // address error handling. ini_set ('display_errors',1); error_reporting (E_ALL & ~E_NOTICE); $cdQuantity = $_POST['cdQuantity']; $cdLabel = $_POST['cdLabel']; $cdPackage = $_POST['cdPackage']; $cdPrint = $_POST['cdPrint']; //Calculate the individual price. $price = $cdLabel + $cdPackage + $cdPrint; //Calculate total price. $total = $cdQuantity * $price; //Print out results. print "You have selected to order:</br> $cdQuantity CDs at $$price. Your estimated cost is $$total."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/136066-creating-an-estimate-form/#findComment-710653 Share on other sites More sharing options...
mallen Posted December 13, 2008 Author Share Posted December 13, 2008 I revised the code but still can't get it to work. Here is the part of the form. I assigned 1,2,3 or 4 to keep it simple. <select name="cdLabel" id="cdLabel"> <option value="0">None</option> <option value="1">Black on silver</option> <option value="2">Black on white</option> <option value="3">Color on Silver</option> <option value="4">Color on white</option> </select></td> Here is the page that calculates. <?php // address error handling. ini_set ('display_errors',1); error_reporting (E_ALL & ~E_NOTICE); $cdQuantity = $_POST['cdQuantity']; $cdLabel = $_POST['cdLabel']; $cdPackage = $_POST['cdPackage']; $cdPrint = $_POST['cdPrint']; //calculate the individual price. if ($cdLabel = 1 ) { $price = 8.00; $blkslv = 0; $blkwht = 0.1; $colsil = 0.5; $colorwhite = 0.6; if ($cdLabel = 2 ) { $price = 7.00; $blkslv = 0; $blkwht = 0.1; $colsil = 0.5; $colorwhite = 0.6; if ($cdLabel = 3 ) { $price = 6.00; $blkslv = 0; $blkwht = 0.1; $colsil = 0.5; $colorwhite = 0.6; if ($cdLabel = 4 ) { $price = 5.00; $blkslv = 0; $blkwht = 0.1; $colsil = 0.5; $colorwhite = 0.6; if ($cdLabel = 5 ) { $price = 3.00; $blkslv = 0; $blkwht = 0.08; $colsil = 0.4; $colorwhite = 0.45; if ($cdLabel = 6 ) { $price = 2.00; $blkslv = 0; $blkwht = 0.07; $colsil = 0.4; $colorwhite = 0.45; if ($cdLabel = 7 ) { $price = 1.50; $blkslv = 0; $blkwht = 0.06; $colsil = 0.35; $colorwhite = 0.4; if ($cdLabel = 8 ) { $price = 1.15; $blkslv = 0; $blkwht = 0.06; $colsil = 0.35; $colorwhite = 0.4; if ($cdLabel = 9 ) { $price = .75; $blkslv = 0.02; $blkwht = 0.05; $colsil = 0.3; $colorwhite = 0.35; if ($cdLabel = 10 ) { $price = .60; $blkslv = 0.02; $blkwht = 0.05; $colsil = 0.2; $colorwhite = 0.22; } } } } } } } } } } $subprice = $price + $blkslv + $blkwht + $colsil + $colorwhite; //calculate total price. $total = $subprice * $cdQuantity; //print out results. print "You have selected to order:</br> $subprice CDs at $cdQuantity. Your estimated cost is $total."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/136066-creating-an-estimate-form/#findComment-714730 Share on other sites More sharing options...
mallen Posted December 15, 2008 Author Share Posted December 15, 2008 I change it and now I got this. My calculation is off. See that if they choose case 1: $price = 8.00; $blkslv = 0; $blkwht = 0.1; $colsil = 0.5; $colorwhite = 0.6; break; It is adding up all five choices of color choices when in reality they will only be choosing one. So I need a way to calculate just one choice. Would any array do this? <?php // address error handling. ini_set ('display_errors',1); error_reporting (E_ALL & ~E_NOTICE); $cdQuantity = $_POST['cdQuantity']; $cdLabel = $_POST['cdLabel']; $cdPackage = $_POST['cdPackage']; $cdPrint = $_POST['cdPrint']; //calculate the individual price. switch( $cdLabel ) { case 1: $price = 8.00; $blkslv = 0; $blkwht = 0.1; $colsil = 0.5; $colorwhite = 0.6; break; case 2: $price = 7.00; $blkslv = 0; $blkwht = 0.1; $colsil = 0.5; $colorwhite = 0.6; break; case 3: $price = 6.00; $blkslv = 0; $blkwht = 0.1; $colsil = 0.5; $colorwhite = 0.6; break; case 4: $price = 5.00; $blkslv = 0; $blkwht = 0.1; $colsil = 0.5; $colorwhite = 0.6; break; case 5: $price = 3.00; $blkslv = 0; $blkwht = 0.08; $colsil = 0.4; $colorwhite = 0.45; break; case 6: $price = 2.00; $blkslv = 0; $blkwht = 0.07; $colsil = 0.4; $colorwhite = 0.45; break; case 7: $price = 1.50; $blkslv = 0; $blkwht = 0.06; $colsil = 0.35; $colorwhite = 0.4; break; case 8: $price = 1.15; $blkslv = 0; $blkwht = 0.06; $colsil = 0.35; $colorwhite = 0.4; break; case 9: $price = .75; $blkslv = 0.02; $blkwht = 0.05; $colsil = 0.3; $colorwhite = 0.35; break; case 10: $price = .60; $blkslv = 0.02; $blkwht = 0.05; $colsil = 0.2; $colorwhite = 0.22; break; } $subprice = $price + $blkslv + $blkwht + $colsil + $colorwhite; //calculate total price. $total = $subprice * $cdQuantity; //print out results. print "You have selected to order:</br> $subprice CDs at $cdQuantity. Your estimated cost is $total."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/136066-creating-an-estimate-form/#findComment-715525 Share on other sites More sharing options...
mallen Posted December 16, 2008 Author Share Posted December 16, 2008 creating a new thread Quote Link to comment https://forums.phpfreaks.com/topic/136066-creating-an-estimate-form/#findComment-716359 Share on other sites More sharing options...
mallen Posted December 31, 2008 Author Share Posted December 31, 2008 Ok here is all my new script. It works. I am trying to add one more feature. In addtion to telling them the totla cost, I want to say you selected "jewel case", or "DVD box" etc... <?php ini_set ('display_errors',1); error_reporting (E_ALL & ~E_NOTICE); //multidimensional arrays //accept the quantity: $quantity = $_POST['cdQuantity']; //check if it's valid: if ( !is_numeric($quantity) || intval($quantity) == 0 ) { die("You entered an invalid quantity: '$quantity' must be a whole number."); } //accept the label selection: $label = $_POST['cdLabel']; //also numeric, but with a specific range: if ( !is_numeric($label) || intval($label) == 0 || $label < 0 || $label > 4 ) { die("INVALID SELECTION"); } $package = $_POST['cdPackage']; $printing = $_POST['cdPrint']; //get the row designation based on the quantity: if ( $quantity > 0 && $quantity <= 10 ) { $key = "1-10"; $cdEach = 8; } elseif ( $quantity > 10 && $quantity <= 24 ) { $key = "11-24"; $cdEach = 7; } elseif ( $quantity > 24 && $quantity <= 49 ) { $key = "25-49"; $cdEach = 6; } elseif ( $quantity > 49 && $quantity <= 99 ) { $key = "50-99"; $cdEach = 5; } elseif ( $quantity > 99 && $quantity <= 249 ) { $key = "100-249"; $cdEach = 3; } elseif ( $quantity > 249 && $quantity <= 499 ) { $key = "250-499"; $cdEach = 2; } elseif ( $quantity > 499 && $quantity <= 999 ) { $key = "500-999"; $cdEach = 1.50; } elseif ( $quantity > 999 && $quantity <= 2499 ) { $key = "1,000-2,499"; $cdEach = 1.15; } elseif ( $quantity > 2499 && $quantity <= 4999 ) { $key = "2,500-4,999"; $cdEach = 0.75; } elseif ( $quantity > 4999 && $quantity <= 9999 ) { $key = "5,000-9,999"; $cdEach = 0.60; } else { die("$quantity is an invalid quantity, please phone for a quote."); } //now build the pricing array as a two dimensional array. That means we have an element in the outer //array for each row, and each row contains an array for the columns in that row. //as you can see, I'm using $key from above as the key for the row, and $label as the key for the columns $labelPricing = array( "1-10" => array( 1 => 0, 2 => 0.1, 3 => 0.5, 4 => 0.6, ), "11-24" => array( 1 => 0, 2 => 0.1, 3 => 0.5, 4 => 0.6, ), "25-49" => array( 1 => 0, 2 => 0.1, 3 => 0.5, 4 => 0.6, ), "50-99" => array( 1 => 0, 2 => 0.1, 3 => 0.5, 4 => 0.6, ), "100-249" => array( 1 => 0, 2 => 0.08, 3 => 0.4, 4 => 0.45, ), "250-499" => array( 1 => 0, 2 => 0.07, 3 => 0.4, 4 => 0.45, ), "500-999" => array( 1 => 0.0, 2 => 0.06, 3 => 0.35, 4 => 0.4, ), "1,000-2,499" => array( 1 => 0, 2 => 0.06, 3 => 0.35, 4 => 0.4, ), "2,500-4,999" => array( 1 => 0.20, 2 => 0.50, 3 => 0.30, 4 => 0.35, ), "5,000-9,999" => array( 1 => 0.02, 2 => 0.05, 3 => 0.2, 4 => 0.22, ), ); //end label price array ******** //begin packaging array ***************************************** $packagePricing = array( "1-10" => array( 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0.25, 6 => 0.75, ), "11-24" => array( 1 => 0, 2 => 0, 3 => 0.25, 4 => 0.25, 5 => 0.4, 6 => 0.75, ), "25-49" => array( 1 => 0, 2 => 0, 3 => 0.25, 4 => 0.25, 5 => 0.4, 6 => 0.75, ), "50-99" => array( 1 => 0, 2 => 0, 3 => 0.25, 4 => 0.25, 5 => 0.4, 6 => 0.65, ), "100-249" => array( 1 => 0, 2 => 0, 3 => 0.25, 4 => 0.25, 5 => 0.4, 6 => 0.65, ), "250-499" => array( 1 => 0, 2 => 0, 3 => 0.25, 4 => 0.25, 5 => 0.4, 6 => 0.6, ), "500-999" => array( 1 => 0, 2 => 0, 3 => 0.23, 4 => 0.25, 5 => 0.4, 6 => 0.6, ), "1,000-2,499" => array( 1 => 0, 2 => 0.03, 3 => 0.23, 4 => 0.23, 5 => 0.38, 6 => 0.55, ), "2,500=4,999" => array( 1 => 0, 2 => 0.03, 3 => 0.22, 4 => 0.22, 5 => 0.36, 6 => 0.55, ), "5,000-9,999" => array( 1 => 0, 2 => 0.03, 3 => 0.22, 4 => 0.22, 5 => 0.35, 6 => 0.5, ), ); //end packaging array ******** //begin printing array ***************************************** $printingPricing = array( "1-10" => array( 1 => 0.5, 2 => 1, ), "11-24" => array( 1 => 0.45, 2 => 1, ), "25-49" => array( 1 => 0.45, 2 => 0.85, ), "50-99" => array( 1 => 0.4, 2 => 0.8, ), "100-249" => array( 1 => 0.4, 2 => 0.75, ), "250-499" => array( 1 => 0.4, 2 => 0.6, ), "500-999" => array( 1 => 0.35, 2 => 0.5, ), "1,000-2,499" => array( 1 => 0.3, 2 => 0.35, ), "2,500-4,999" => array( 1 => 0.15, 2 => 0.25, ), "5,000-9,999" => array( 1 => 0.1, 2 => 0.22, ), ); //end printing array ****** //collect what choice they made such as clamshell, silver etc... //display cost and totals. $unitprice= number_format($labelPricing[$key][$label] + $packagePricing[$key][$package] + $printingPricing[$key][$printing] + $cdEach,2); $total= number_format($quantity * $unitprice,2); //You have selected ***** for labels, ****** for printing and ****** for packaging. print "You have selected $quantity CDs at a unit price of \$$unitprice <br/> <br/> Your total is \$$total"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/136066-creating-an-estimate-form/#findComment-727168 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.