Jump to content

Calculating costs from database


radagast

Recommended Posts

  • Hello
     
    Please can someone PLEASE PLEASE help me.
     
    I have a form that the user inputs dimension and weights of a parcel. They then request a price on request I do the following:
     
    $volume_mass = 50(dimm1) *50 (dimm2) * 50 (dimm3);
    so that means $volume_mass = 125000
    I then take the total of the weight - $actual_mass = 20(weight)
     
    I am doing the following query to see what service are available
     
      $data = "SELECT * FROM rates WHERE hub_from = 'nxd' AND hub_to = 'auk'";
      $result = mysql_query($data)
      or die ("Error in query: $data. " . mysql_error());
     
    if (mysql_num_rows($result) > 0)
    {
     
    while($row = mysql_fetch_object($result))
     
       
                                 
        } 
                  ?>
     
    So I get multiple results and I need to now work out the cost per result.
     
    Example of results from rates table
     
    $service $volume $base $min_kg $per_kg
    AIR       5000       70.00   5             4
    ECO       4000       80.00   10     4
    LOX       5000       45.00     1             35
    LSE       5000       45.00     1             35
    OVN       5000       70.00     10     4
     
    Know I need to do the following per result (per service)
     

  1. need to the volume factor from the service -  &totalv = $volume_mass / $volume (from the rates table)
  2. Check if which is now greater between $actual_mass and $totalv - $charge_mass = $actual_mass <> $totalv
  3. then need to calculate the costs - if $charge_mass <= $min_kg then $total_cost = $base echo "$total_cost" else if $charge_mass > $min_kg then $total_cost = ($charge_mass - $min_kg) *$per_kg + $base  echo "$total_cost"

[*]

 

   

 

Any help would be greatly appreciated

 

Link to comment
Share on other sites

It doesn't look like you need an if/else. You can simply calculate the price based upon the base plus the "overage" times the rate. the overage being the amount of the $charge_mass over the minimum. If the $charge_mass is less than the minimum, the overage would be 0.

 

Here is the verbose solution:

$data = "SELECT *, ($volume_mass/`volume`) as totalv
         FROM rates
         WHERE hub_from = 'nxd'
           AND hub_to = 'auk'";
  $result = mysql_query($data)
  or die ("Error in query: $data. " . mysql_error());

while($row = mysql_fetch_object($result))
{   
    $charge_mass = max($actual_mass, $row['totalv']);
    $overage = max($charge_mass - $row['min_kg'], 0);
    $total_cost = $row['base'] + ($overage * $row['per_kg']);
    echo "{$row['service']}: {$total_cost}<br>\n";
} 

 

Or you can make it more compact as follows (use the query from above):

while($row = mysql_fetch_object($result))
{   
    $total_cost = $row['base'] + (max(max($actual_mass, $row['totalv']) - $row['min_kg'], 0) * $row['per_kg']);
    echo "{$row['service']}: {$total_cost}<br>\n";
} 

 

Personally, I'd go with the verbose code with comments

Link to comment
Share on other sites

  • 4 weeks later...
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.