Jump to content

Best method!


poolhustler86

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/94640-best-method/
Share on other sites

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 :) )

Link to comment
https://forums.phpfreaks.com/topic/94640-best-method/#findComment-485602
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/94640-best-method/#findComment-486646
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.