grlayouts Posted November 23, 2007 Share Posted November 23, 2007 say i have a record that changes from 1 - 10000 and i want it to create a price between 25-300 depending on how many there are. ie 5000 price is 300 any idea's? Quote Link to comment https://forums.phpfreaks.com/topic/78567-fluctuation/ Share on other sites More sharing options...
revraz Posted November 23, 2007 Share Posted November 23, 2007 You need to have a formula first. Quote Link to comment https://forums.phpfreaks.com/topic/78567-fluctuation/#findComment-397550 Share on other sites More sharing options...
grlayouts Posted November 23, 2007 Author Share Posted November 23, 2007 well i've had a few( one posted yesterday) i was thinking an algorithm but i never wrote one. Quote Link to comment https://forums.phpfreaks.com/topic/78567-fluctuation/#findComment-397556 Share on other sites More sharing options...
boushley Posted November 23, 2007 Share Posted November 23, 2007 Its really not very hard to write one... 1=25 & 10000=300... so you've got a difference of 9,999 and 275. So, it would be something like price=25+(num*(275/9999)) And then can you do it from there? Quote Link to comment https://forums.phpfreaks.com/topic/78567-fluctuation/#findComment-397667 Share on other sites More sharing options...
Barand Posted November 23, 2007 Share Posted November 23, 2007 You could allocate on a straight line. If qty is 10000, price is 25 each, if 1 then 300 each [pre] 300+ \ | \ | \ 150+ .\ | . \ | . \ 25+ . \ +-+----+-- 1 . 10000 . 2000 = 245 P = -275 * Q + 300 -------- 10000 [/pre] <?php function price ($q) { return ceil($q * -275 / 10000) + 300; } for ($i = 0; $i <= 10000; $i+=1000) { $q = $i==0 ? 1 : $i; echo "$q :" . price($q) . '<br>'; } --> 1 :300 1000 :273 2000 :245 3000 :218 4000 :190 5000 :163 6000 :135 7000 :108 8000 :80 9000 :53 10000 :25 Quote Link to comment https://forums.phpfreaks.com/topic/78567-fluctuation/#findComment-397672 Share on other sites More sharing options...
grlayouts Posted November 24, 2007 Author Share Posted November 24, 2007 barand thats exactly what i was looking for. will it work if the quantitys are 333? or 659? Quote Link to comment https://forums.phpfreaks.com/topic/78567-fluctuation/#findComment-398060 Share on other sites More sharing options...
Barand Posted November 24, 2007 Share Posted November 24, 2007 Any value from 1 to 10000. If it's 20,000 you pay them 250 to take them away try it! <?php function price ($q) { return ceil($q * -275 / 10000) + 300; } $qtys = array(1, 333, 659, 1000, 5000, 10000); foreach ($qtys as $q) { echo "$q :" . price($q) . '<br>'; } /* --> 1 :300 333 :291 659 :282 1000 :273 5000 :163 10000 :25 */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/78567-fluctuation/#findComment-398062 Share on other sites More sharing options...
grlayouts Posted November 24, 2007 Author Share Posted November 24, 2007 do i have to set $q? as my product ammount? $q = mysql_fetch_row(mysql_query("SELECT SUM(product) FROM players;")); Quote Link to comment https://forums.phpfreaks.com/topic/78567-fluctuation/#findComment-398065 Share on other sites More sharing options...
grlayouts Posted November 24, 2007 Author Share Posted November 24, 2007 Barand i can't thank you enough mate.. thanks for the help i ended up with <? include('config.php'); $q2 = mysql_fetch_row(mysql_query("SELECT SUM(drugs) FROM players;")); function price ($q) { return ceil($q * -275 / 20000) + 300; } $qtys = $q2; foreach ($qtys as $q) { echo "$q :" . price($q) . '<br>'; } mysql_query('UPDATE players SET drugs = drugs + (drugfact*20), dpayout = (drugfact*20);'); mysql_query('UPDATE players SET credits = credits - (employees*wages)'); $sql = "UPDATE price SET cost = $q where ITEM = 'drugs'"; mysql_query($sql) or die($sql . ' : ' . mysql_error()); ?> works a treat!! Quote Link to comment https://forums.phpfreaks.com/topic/78567-fluctuation/#findComment-398068 Share on other sites More sharing options...
Barand Posted November 24, 2007 Share Posted November 24, 2007 Any var name you like (meaningful ones are best) <?php function price ($q) { if ($q > 10000) return 25; return ceil($q * -275 / 10000) + 300; } $res = mysql_query("SELECT SUM(product) FROM players"); $quantity = mysql_result($res, 0); echo 'Qty : ' . $quantity . ' at ' . price($quantity) . ' each'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/78567-fluctuation/#findComment-398070 Share on other sites More sharing options...
grlayouts Posted November 24, 2007 Author Share Posted November 24, 2007 now if i have 4000 of (product) at price 240 and 3999 are sold instead of it going back up to the max at 300 return maybe 290 Quote Link to comment https://forums.phpfreaks.com/topic/78567-fluctuation/#findComment-398101 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.