poolhustler86 Posted March 6, 2008 Share Posted March 6, 2008 I have a table in MySQL that holds the following data: item qty item margin carton qty cartin margin bulk qty bulk margin 1 1.33 50 0.95 100 0.50 when a customer submits via a form the qty they would like, is the best method to write out an if statment to test against which margin / qty it fits into best so i can calculate a price. or is there another way around it? i would just assume you do... example... customer qty = 10 if qty >= 1 get margin 1.33 else if qty >=50 get margin 0.95 else if qty >= 100 get margin 0.50 than caluclate the price... best method? Quote Link to comment Share on other sites More sharing options...
btherl Posted March 6, 2008 Share Posted March 6, 2008 What you're suggesting looks fine. Are those thresholds and margins all in separate columns? Quote Link to comment Share on other sites More sharing options...
poolhustler86 Posted March 6, 2008 Author Share Posted March 6, 2008 yep different columns within the database cause each item could be different margins / qty... i was just wondering if there was a better way to do it... cause that if statement will be going inside of a foreach array... Quote Link to comment Share on other sites More sharing options...
btherl Posted March 7, 2008 Share Posted March 7, 2008 If you think it's too ugly or complicated, you can create a function that does the work. function calculate_margin($qty) { if ($qty < $carton_qty) { return $item_margin; } elseif ($qty < $bulk qty) { return $carton_margin; } else { return $bulk_margin; } } Then your foreach loop just has to call that function each time (the function will need the margin values available to it .. you can make those global or pass them as an argument, depending on how professional you want to be ) Quote Link to comment Share on other sites More sharing options...
poolhustler86 Posted March 7, 2008 Author Share Posted March 7, 2008 how would i then call the function? im new to oop... and then use the return varailbe to give me the price... so i can then sumbit the data to another table. Quote Link to comment Share on other sites More sharing options...
btherl Posted March 8, 2008 Share Posted March 8, 2008 Functions are from traditional programming, oop uses methods You would call it like this $margin = calculate_margin($qty); Where $qty is the quantity you want to find the margin of. If you can show us your current code, we can help you modify it to do what you want. Quote Link to comment 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.