Forum Moderators: coopster

Message Too Old, No Replies

PHP Script to set assignment based on weight score

         

artie2004

4:44 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



for example: if there are 4 stores and if store #1 is assigned a weight score of 10 - it would get 10% of the orders. if store #2 is assigned a weight score of 30 - it would get 30% of the orders and so forth, until it equals out to 100%. Is there a PHP function that i could use for this or would i have to just manually code this? Could someone please show me how would i go about doing this? Thanks.

bfillmer

5:17 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



Are you wanting it to compute the remainder available if they only input 3 of them or something similar?

I'm not sure I understand the question.

artie2004

5:38 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



Hi bfillmer. I'm not sure what you mean. I have an order form from my site. When my customer submits the order form, it calls on my script to enter my customer's info into a database. I have several suppliers(store) and i would like my script to assign this order to one of my supplier based on a weight score that i give that store.

for example: if there are 4 stores and if store #1 is assigned a weight score of 10 - it would get 10% of all orders. if store #2 is assigned a weight score of 30 - it would get 30% of all orders. if store #3 is assigned a weight score of 40 - it would get 40% of all orders. if store #4 is assigned a weight score of 20 - it would get 20% of all orders.

bfillmer

5:57 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



Ah. That's quite a bit more complex then what I thought you were wanting.

It seems like you would have to query the db and determine what percentage each store is currently at and add the new order to the store that is furthest from the max percentage you have assigned it.

It'll be some pretty messy code though. I'll keep thinking on it and see if I can come up with something cleaner.

Salsa

6:03 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



Gosh, while it seems like PHP has a function for almost everything, I don't think that's one of them. Assuming you have total orders to date for each store in a file or database, and you could put the totals in an array, like:

$sales_to_date = array('store1' => 53, 'store2' => 158, ...);

...and you also have arrays set for store ids and assigned percetages, like:

$store_id = array('store1' => 1, 'store2' => 2, ...); 
$store_percentages = array('store1' => 10, 'store2' => 30, ...);

...you could:

$total_sales = array_sum($sales_to_date); 
foreach($sales_to_date AS $k => $v) {
if ($v / $total_sales < $store_percentages[$k]/100){
echo "store $store_id[$k] gets the order<br>\n";
$sales_to_date[$k]++;
break;
}
}

I hope this helps.

artie2004

12:18 am on Dec 15, 2004 (gmt 0)

10+ Year Member



Thanks for the help you guys. I'll give your suggestion a try Salsa.

artie2004

4:24 pm on Dec 15, 2004 (gmt 0)

10+ Year Member



Hi Salsa. The script works. How do i modify it so that it will still work when the sum of the total order percentage of all stores equal 100%?

When i run this script, it stops only after 10 orders because when the line

if ($v / $total_sales < $store_percentages[$k]/100)

is no longer true, no store gets assign the order.

Salsa

5:23 pm on Dec 15, 2004 (gmt 0)

10+ Year Member



My mistake. I really only ment for it to work when $store_percentages is 100, and probably just changing the if condition from < to <= will do the trick:

if ($v / $total_sales <= $store_percentages[$k]/100){

If you are testing it with low numbers, and you have two stores weighted at 30 and 70, after ten orders, neither of them is going to be less than... They'll both be equal in the condition. The same thing will happen at 100, 1000.... So adding the = should give the order to the first store in that case.

artie2004

11:42 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



Thanks Salsa and Happy Holiday!