Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2011
    Thanked 1 Time in 1 Post

    Exclamation How do you make an automatic price averager? (Read)

    I'm having a bit of an issue, in the real world there's an economy of goods and prices, an ever changing one, The average price on a jar of honey may be $3 today but in 4 days it could be $3.25, everything changes constantly, is there a way where i could do something like this with tables? I want to be able to let users put in what the think a good price for the item would be and it takes the other prices and averages them automatically and displays the price. For example,

    I have listed an apple, the starting value of the apple is $3
    User 1 thinks the apple is worth around $4
    ($4 + $3)x .5 = $3.50 (Average cost of an Apple)
    This price is now displayed to the community, that the average price of an apple is $3.50, now say another user comes around,
    User 2 thinks the apple is somewhere around $2.75
    ($4 + $3 + $2.75) x .5 = $4.87
    Now, the average price of an apple that would be displayed to the community is $4.87, is there something like this that i could use on my website? Whether it is open source or not doesn't matter, something that will check the MySQL databases for previous data and average out the information/prices/data to give an end average price between the submitted prices?

    I've been having quite some trouble with this issue, i'm looking to create a price guide for online items in a gaming community, is there an easy way to do this?

  2. #2
    Supreme Master coder!
    Join Date
    Jun 2003
    Cottage Grove, Minnesota
    Thanked 1,167 Times in 1,158 Posts
    Because you don't know how many items there will be, or how many values
    are placed on those items, I would think you would have one table in your MySQL
    database that stores all of them. Each time someone gives a value to an apple,
    it would insert a new line. There could be 50 rows for apples, 30 rows for oranges,
    and 100 rows for bananas. You query for a particular item, you know how many
    rows it found, and you SUM the values ... now you can determine the average.

    If you also add a column with a timestamp, you could query the table for an item
    within a certain amount of time (days, months, years). You may want to know the
    average price for the apples only during the past month.

    That's my take on it anyhow.


  3. Users who have thanked mlseim for this post:

    skill3d (02-24-2012)

  4. #3
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Thanked 130 Times in 129 Posts
    in your example that average goes up after the third entry even if the new entry was lower than the previous average.

    You can't just divide the sum by 2 each time a new entry has come in.
    Then if the next 97 entries all are $3.50 then you would have average of

    (4 + 3 + 2 + 97x3.50)*.5 = 174,63.

    You only need to store the number of entries along with the count of entries.

    After the first 2 entries .
    you will have

    EntrySum = 7;
    EntryCount = 2;

    And then for each new entry:

    EntrySum += NewEntry;

    After the 3 third it would be:

    EntrySum = 9.75;
    EntryCount = 3;

    And then the average would be EntrySum(9.75) / EntryCount(3) = 3.25.

    You could for the statistics also store the highest and the lowest entry.

    And use:
    EntryMax = Max(EntryMax, NewEntry)
    EntryMin = Min(EntryMin, NewEntry)
    to set the new value

    But if you also want to know the what entry was entered most times, then you will need to store each and every entry seperately.

  5. Users who have thanked Lerura for this post:

    skill3d (02-24-2012)

  6. #4
    New Coder
    Join Date
    Oct 2011
    Thanked 1 Time in 1 Post
    Thank you both very much, hopefully with your replies and some other information on coding this, i'll be able to get it done with-in the next couple days. Your information was very useful. Thanked, -Skill3d


Posting Permissions

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