SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP form calculations - desperate for help !!!



    OK, I seriously need massive help with this (just really starting with PHP).

    I don't want a JavaScript solution - it has to be server-side and only four users will ever be accessing it so any overhead to the server will be next to nil.

    So, here we go:

    The left hand column on this form has indexed numbers to identify the rows - actual finished form will not have this.

    Row 1 (Select area) will have a list of area names - each one has a numerical value when selected. At present the list is a standard HTML drop down option selection. It may turn into a dynamic drop down populated from a database table but for now it is along these lines:

    HTML Code:
    <select name="area">
            <option value="10" selected>Option1</option>
            <option value="100">Option2</option>
    </select>
    Row 2 (Input unit value X) is crucial.
    A value will be typed into this text box which will be used as a basis for calculations throughout the rest of the form.

    Row 3 (Fee)
    This is a hard one - in Excel I do it like this:

    =IF(CELL-No>=500001,"445",IF(CELL-No>=300001,"310",IF(CELL-No>=150001,"290",IF(CELL-No>0,"280"))))

    Basically Row 2 (Input unit value X) is the number to be used, so if it is Greater than or Equal To 500001 then Row 3 (Fee) will be 445 and so on.

    Row 4 (Yes/No1)
    Selecting No has a zero value, selecting Yes equals the value of 100

    Row 5 (Yes/No2)
    Same as above in Row 4, where selecting No has a zero value, selecting Yes equals the value of 100

    **Note, the above two could just as easily be Check Boxes.

    Row 6 (VAT)
    VAT is calculated based on value of Row 3 (Fee) + value of Row 4 + value of Row 5 multiplied by 0.175

    Row 7 (Amount1)
    If Row 2 (Input unit value X) is greater than or equal to 500001 then Row 7 (Amount1) = 445, If Row 2 (Input unit value X) is greater than or equal to 300001 then Row 7 (Amount1) = 310, If Row 2 (Input unit value X) is greater than or equal to 150001 then Row 7 (Amount1) = 290, If Row 2 (Input unit value X) is less than or equal to 150000 then Row 7 (Amount1) = 280,

    Row 8 (Amount2)
    Value of this text box will be determined by selecting from Row 1 (Select area) where the selected option has a numerical value. If area 51 is selected in Row 1 (Select area) then the value of this text box will be 51.

    Row 9 (Amount3)
    In Excel, I did this: =IF(CELL>=1000001,"700",IF(CELL>=500001,"420",IF(CELL>=200001,"220",IF(CELL>=100001,"150",IF(CELL>=80001,"100",IF(CELL>=50001,"60",IF(CELL<=50000,"40"))))))) and CELL is the value typed into Row 2 (Input unit value X) on this form.

    Row 10 (Amount4)
    Always = number 6

    Row 11 (Amount5)
    Always = number 40

    Row 12 (Amount6)
    Selecting No = Zero but selecting Yes = 39.95

    Row 13 (Total)
    Total value of all above numbers

    Row 14 (Start)

    Total of Row 8 (Amount2) + Row 11 (Amount5) + Row 12 (Amount6)

    A Reset button will be at the bottom of the form, as will a send (to database), email, and print button. I can do those bits, but the form itself is something entirely different.

    I've started reading the PHP & MySQL Web Development book and some of its content hints at most of what I want to be able to do with the above, but actually doing it when I'm just starting in PHP is something else.

    For the curious, this is a mortgage purchase calculator of sorts and is vital for a small project I am working on. I am happy to outsource this at cost but I need something very quickly to demonstrate to the client.

    Any suggestions, other than to go jump in a large body of water would be most appreciated. As you can probably imagine my head's in bits over this...

    Help !?!?!

  2. #2
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WOW... that's an extremely large post.. Not sure if anyone will even have the time to read if fully... I know I didn't

  3. #3
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    D4A,

    In the same order, mostly:

    1. You didn't say why the options have different values, why they have numeric values, and whether it matters.

    I'm doing this in order, so I don't know if it will matter. More later.

    PHP Code:
    $AREA 10 
    2. You have no contraints on this input value? Must be less than 10000? Must be even? Must be a prime number?

    PHP Code:
    $UNIT_VALUE 3.141592
    3. This is derived from the row 2 value:

    Long version (use this until you understand it):
    [PHP]
    if ($UNIT_VALUE >= 500001) {$FEE = 445; }
    else if ($UNIT_VALUE >= 300001) { $FEE = 310; }
    else if ($UNIT_VALUE >= 150001) { $FEE = 290; }
    else if ($UNIT_VALUE > 0) { $FEE = 280; }
    else
    die("Bogus value for UNIT_VALUE: $UNIT_VALUE");
    [PHP]

    Array version (easier to update later):
    PHP Code:
    $fee_structure = array(
      
    // Value (x1000) => Fee
      
    500 => 445,
      
    300 => 310,
      
    150 => 290,
        
    => 280
    );

    $FEE = -1;
    foreach (
    rsort(array_keys($fee_structure)) as $unit_val) {
      if (
    $UNIT_VALUE > ($unit_val 1000)) {
        
    $FEE $fee_structure[$unit_val];
        break;
      }
    }

    if (
    $FEE 0)
      die(
    "Bogus value for UNIT_VALUE: $UNIT_VALUE"); 
    Ternary structure, quite similar to the Excel IF(c,t,f) macro:
    PHP Code:
    $FEE $UNIT_VALUE 500000 445
            
    $UNIT_VALUE 300000 310
            
    $UNIT_VALUE 150000 290
            
    $UNIT_VALUE >      280
            
    : die("Bogus value for UNIT_VALUE: $UNIT_VALUE"); 
    All three should have the same behavior, unless I've just entered buggy code -- I'm not testing this stuff.

    4. If you know every single value that can possibly occur, a [fphp]switch[fphp] statement works well:

    PHP Code:
    switch ($_POST['yes_or_no_1']) {
    case 
    'Yes'$YES_OR_NO_1 100; break;
    case 
    'No'$YES_OR_NO_1 0; break;
    default:
      die(
    "Bogus value for YES_OR_NO_1: " $_POST['yes_or_no_1']);

    5. This is the same as 4, but there are some other ways:

    "if" statement:
    PHP Code:
    if ($_POST['yes_or_no_2'] == 'Yes') {
      
    $YES_OR_NO_2 100;

    else {
      
    $YES_OR_NO_2 0;

    The ternary operator, already mentioned once:
    PHP Code:
    $YES_OR_NO_2 $_POST['yes_or_no_2'] == 'Yes' 100 0
    Math that relies on false being zero and true being one:
    PHP Code:
    $YES_OR_NO_2 = ($_POST['yes_or_no_2'] == 'Yes') * 100
    6. Vat = 17.5%. Sounds like England.

    PHP Code:
    $VAT_RATE 0.175;

    $VAT_SUBTOTAL $FEE $YES_OR_NO_1 $YES_OR_NO_2;

    $VAT $VAT_SUBTOTAL $VAT_RATE
    7. This is just the fee from row 3 all over again. WTF?

    $AMOUNT1 = $FEE;

    8. I assume you mean that this field has the numeric value from row 1 always, and not just when it's 51. Remember that HTML select statements only post the value, not the select text.

    $AMOUNT2 = $AREA;

    9. You've seen how I did row 3, $FEE. This is the same thing.

    10. $AMOUNT4 = 6;
    11. $AMOUNT5 = 40; // Hours per week?

    12. See lines 4 & 5.

    13. I'll bet you're lying on this one. The sum of ALL the numbers?

    $TOTAL = $AREA + $UNIT_VALUE + $FEE + $YES_OR_NO_1 + $YES_OR_NO_2 + $VAT + $AMOUNT1 + $AMOUNT2 + $AMOUNT3 + $AMOUNT4 + $AMOUNT5 + $AMOUNT6;

    14. See 13.

    You'll need to know how to get the data from the form to the PHP script. It comes in via the $_POST[] array.

    =Austin

    PS: For a large fee, I'll do it.
    Austin Hastings - Principal Consultant - Longacre, Inc.

    Anything you can do, you can do better.

  4. #4
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow Austin lol.. Your a beast.... Or just extremly bored...

    Im guessing it's a mixture of the two. Either way.. GOOD JOB with the help

  5. #5
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mav3n,

    Thanks.

    Actually, I'm competing for the free car that Sitepoint is giving away.

    =Austin
    Austin Hastings - Principal Consultant - Longacre, Inc.

    Anything you can do, you can do better.

  6. #6
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm he austin whast the free car about pm me of it

  7. #7
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, it's the usual--most informative posts, has to be good info, blah blah blah. The catch is you have to go to Australia to get the car but I figure I'll take a vacation and then abandon the car at the airport when I leave -- saves me the cost of a rental. The details are posted someplace..check the home page.

    =Austin
    Austin Hastings - Principal Consultant - Longacre, Inc.

    Anything you can do, you can do better.

  8. #8
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow !

    Been away yesterday - didn't expect much of anything in response to such a complex post, but hey, nice surprise.

    Thanks for the input Austin - very interesting and it has possibilities even to a PHP dunce like me.

    PM on its way incidentally.

    Will re-read and attempt to digest.

    Cheers.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •