views:

43

answers:

3

Here's one that's got me stumped. Working in PHP5.

I am building a store application for a client that makes use of a web service from a larger agency as he is a broker. This web service requires that items for purchase by end users be of a certain quantity or else the order will be considered ineligible for purchase.

I must make dropdown option boxes that contain only eligible quantity numbers. Their rules are as follows:

If a quantity greater than 12 is available, then allow any quantity EXCEPT that which would leave only one item remaining

If a quantity of less than 12 is available and said quantity is even, then allow only even pairs for purchase

If a quantity of of less than 12 is available and said quantity is odd, then allow any quantity except that which would leave only one item remaining.

I'm a bit confused as to how my conditionals to determine dropdown content should be structured to accommodate this. How could I know in advance, via conditionals, whether the final quantity vs the user's requested quantity would leave just one for purchase and thereby deny it?

I can't imagine why the service issuer did not make it so an array of eligible quantities was returned rather than a hard number. Please also note there are two other rules that I've already manage to mete out.

Any help would be greatly appreciated!

A: 

If the quantity available is q:

If a quantity greater than 12 is available, then allow any quantity EXCEPT that which would leave only one item remaining if(q > 12)

//allow all quantities upto q-2

If a quantity of less than 12 is available and said quantity is even, then allow only even pairs for purchase if(q<12 && isEven(q))

//allow all even quantities up to q

If a quantity of of less than 12 is available and said quantity is odd, then allow any quantity except that which would leave only one item remaining. if(q<12 && isOdd(q))

//allow all quantities less than q-2

Tahir Akhtar
+1  A: 
if($avail>12 || $avail%2) {
  $step = 1;  // all qtys for large avail or odd avail
} else {
  $step = 2;  // even qtys for small even avail
}
$options = array();
for($i = $avail-($step==1); $i > 0; $i-=$step) { // skip qty=avail for step=1
  $options[] = $i;
}
Sparr
Nice one. I would up vote you but I'm out of votes for today.
Alix Axel
Thanks @Sparr and all! - This should be what I'm after.
DeaconDesperado
+1  A: 

I don't know the syntax of PHP; here is some pseudocode:

if(quantity >= 12 or quantity%2 == 1)
{
    dropdown.add(quantity);
    for(allowed = quantity-2; allowed >= 1; allowed--)
        dropdown.add(allowed);
}
else
{
    for(allowed = quantity; allowed >= 2; allowed -= 2)
        dropdown.add(allowed);
}
Federico Ramponi