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
https://forums.phpfreaks.com/topic/248631-calculating-costs-from-database/
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

  • 4 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.