Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 562369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:32:50+00:00 2026-05-13T12:32:50+00:00

I have to calculate a price based on a rate structure along these lines:

  • 0

I have to calculate a price based on a rate structure along these lines:

$303.00 fixed price up to 500 units
$0.023 additional per unit from 501-10,000 units
$0.022 additional per unit from 10,001-25,000 units
$0.021 additional per unit from 25,001-50,000 units

I’m a little lost on setting up a database structure and algorithm (the larger sticking point) for calculating this. Has anyone done this? Is there a nice, elegant way of calculating this sort of thing?

edit: As an example, a 25,100 unit run would cost $303.00 for the first 500 units, $218.50 for the next 9,500 units, $330.00 for the next 15,000 units, and $2.10 for the next 100 units, for a total of $853.60.

It wouldn’t be a simple 25,100 * $0.021 calculation – I’m well aware of how to select and calculate that.

Similar to the way income tax is assessed – on a marginal basis.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:32:50+00:00Added an answer on May 13, 2026 at 12:32 pm

    What I wound up doing:

    size  units     fixed     per
    1       500   303.000   0.000
    1     10000     0.000   0.023
    1     25000     0.000   0.022
    1     50000     0.000   0.021
    
    
    
    function calculate_price($size, $quantity) {
      global $db;
    
      $price = 0;
      $count = 0;
    
      // fetch rates from the database
      // note: $size is already sanitised by the calling function
      $query = "SELECT units, flat, per FROM rates WHERE size={$size} ORDER BY units ASC";
      $result = $db->query($query);
    
      // step through the rates
      while($rate = $result->fetch_object()) {
        // figure out how many of our units fall within this tier
        $tier_count = max(0, min($quantity - $count, $rate->units - $count));
    
        // calculate the price for this tier, including any flat rate
        $tier_price = $rate->flat + ($rate->per * $tier_count);
    
        // add tier price and count to the totals
        $price += $tier_price;
        $count += $tier_count;
    
        // store the last, largest number of units rate for any leftovers outside our tiers
        $last_rate = $rate;
      }
    
      // if some of our units fall outside our defined tiers, use the last tier's values for them
      if($count < $quantity) {
        $tier_count = $quantity - $count;
        $tier_price = $last_rate->flat + ($last_rate->per * $tier_count);
        $price += $tier_price;
        $count += $tier_count;
      }
    
      return $price;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method which calculates loan payments based on car price, interest rate
I have a procedure that calculates the price of travel based on some values
Im using a javascript to enable disable textboxes and to calculate a price based
I have the following code which I'm using to calculate the price of a
I have problems to calculate price with jquery in various price formats (depending on
I have a function that calculate price for a product. I'm not JavaScript developer
I have a form that uses PHP to calculate a total based on the
I have to following problem to solve. I have to calculate the bidding price
I have to calculate the return of a vector that gives a historical price
I have to calculate the Specular Highlights (phong) of an Image. the normal Vector

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.