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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.