/    Sign up×
Community /Pin to ProfileBookmark

php function question….

Hello you guys! I am working on a function for an online product catalog and I cant seem to figure out how to go about doing the following.

  • 1. Individual products on the cart have the ability for the user to choose the quantity.

  • 2. My client asked me to give discounts of 20 dollars when they purchase two items.

  • 3. My question was, if they got three items, then they would get off 20 dollars for two items, but one item would be priced at regular price. What if they buy 4 items then they would get 40 dollars off. Do you guys see the pattern?
  • this is my task and perhaps I can get some input on how to go about doing this:

    – I would have to discount 20 when 2 products are chosen, 40 when 4 products are chosen, 60 when 6 are chosen, etc.,
    – the function would also have to discount odd quantity numbers for example: 1 would equal regular price, 3 would egual 20 dollars off but one at regular price, 5 would egual 40 dollars off but one at regular price, etc.,

    Can this be done?? any suggestions are greatly welcomed ?

    to post a comment
    PHP

    8 Comments(s)

    Copy linkTweet thisAlerts:
    @NogDogJun 02.2005 — [code=php]
    $discountInDollars = round(20 * (floor($numberOfItems / 2)));
    [/code]
    Copy linkTweet thisAlerts:
    @vicpal25authorJun 02.2005 — humm..what if $number of items = 3 then wouldnt that just givey ou decimal of the discount amount? I wrote a function down in notepad with just the theory..is there a simpler way of writting the following..

    [I]if quantity = 2 & special = yes then specamount - 20.00



    if quantity = 3 & special = yes then specamount - 20.00



    if quantity = 4 & special = yes then specamount - 40.00



    if quantity = 5 & special = yes then specamount - 40.00



    if quantity = 6 & special = yes then specamount - 60.00



    if quantity = 7 & special = yes then specamount - 60.00



    if quantity = 8 & special = yes then specamount - 80.00



    if quantity = 9 & special = yes then specamount - 80.00

    [/I]


    and so forth...
    Copy linkTweet thisAlerts:
    @fozeratorJun 02.2005 — No. That should do it...

    But I don't understand why the round() is used... Since the value of the floor() expression will always be an integer, the resulting product will be an integer as well...

    [code=php]
    $discountInDollars = 20 * (floor($numberOfItems / 2));
    [/code]
    Copy linkTweet thisAlerts:
    @NogDogJun 02.2005 — round() probably is redundant, but I put it in just in case there are any floating point errors resulting from the multiplication (so you don't get a result like 19.999999999 or 20.0000000001).
    Copy linkTweet thisAlerts:
    @RTE_LogicJun 02.2005 — If I'm reading your second post correctly...

    There's probably a much slicker way of doing this, but here's how I'd do it.
    -----------------------------


    My thought process:

    if quantity is even, then discount=10*quantity

    if it's odd, then subtract one to get to the lower qty break level and multiply that by 10.

    (For example:

    if the qty is 1, 1-1 = 0 0*10 = 0

    if the qty is 4, 4*
    10=40

    if the qty is 7, 7-1=6 6*10=60)

    I would use the modulus operator (%) to test for even-ness/odd-ness

    something *LIKE* this (I have not tested this)



    ------------------------------------------------
    $quantity = your total count of products ordered

    if(($quantity % 2)==0) { // Ordered quantity is even

    $qty_break = $quantity;

    } ELSE { // Ordered quantity is odd

    $qty_break = $quantity-1;

    }

    $discount = $qty_break * 10;


    -------------------------------

    You could then go on to subtract the discount from the order's total dollar amount.

    Hope that helps - good luck
    Copy linkTweet thisAlerts:
    @RTE_LogicJun 02.2005 — sorry - just saw nogdog's post - I don't know how I missed those posts before posting mine -but I definitely like his(her?) way better of using floor qty/2 etc.

    Ignore mine.
    Copy linkTweet thisAlerts:
    @vicpal25authorJun 02.2005 — yeah, that flooor function seems cool....i never new about that..i thought i seen it in javascript..
    Copy linkTweet thisAlerts:
    @fozeratorJun 03.2005 — round() probably is redundant, but I put it in just in case there are any floating point errors resulting from the multiplication (so you don't get a result like 19.999999999 or 20.0000000001).[/QUOTE]

    Good point...
    ×

    Success!

    Help @vicpal25 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...