jeudi 4 août 2016

PHP - How to perform array sorting and automatically check each value in array and sum up?

Objective:

  1. User selects value from Camp A drop-down list that generated from SQL.
  2. Each value in Camp A needs to check in the coding and determine Camp B result.
  3. Camp A require at least $50. Camp B require at least $200.
  4. User can obtain 1 point for each $50 in Camp A. User can obtain only 1 ticket after meet the requirement in Camp B.
  5. Each item can be used for 1 Camp only.

Data retrieved from MySQL:

Item | Name       | Price($)
----- ------------ --------
1    | Adidas     | 180.00
2    | Nike Run   |  70.00
3    | Puma       |  15.00
4    | Nike Women |  20.00
5    | NB         |   5.00

From the data shown above, the Camp A drop-down list shows maximum 5 points and Camp B shows 1 ticket. But the message box has to check in back-end whether both Camps are meet requirement.

Example A:

  1. User selects 5 points in Camp A, user is not allowed to select an ticket from Camp B.
  2. Explanation: Item 1 ($180) + Item 2 ($70) = $250. $250 divided by $50, user obtains 5, which is 5 points shown in drop-down list. Total of the remaining items are not meet $200 for Camp B. Thus message box will warn user either select Camp A or Camp B, cannot select both.

Example B:

  1. User selects 4 points in Camp A, user is not allowed to select an ticket from Camp B.
  2. Explanation: Item 1 ($180) + Item 4 ($20) = $200. $200 divided by $50, user obtains 4, which is 4 points shown in drop-down list. Total of the remaining items are not meet $200 for Camp B. Thus message box will again warn user either select Camp A or Camp B, cannot select both.

Example C:

  1. User selects 1 points in Camp A, user is able to select an ticket from Camp B.
  2. Explanation (i): Item 2 ($70) + Item 4 ($20) = $90. $90 divided by $50, user obtains 1, which is 1 point shown in drop-down list. Total of the remaining items are $200 = $180 + $15 + $5. Thus this time the message box will not prompt warning but allow user select both Camp A or Camp B at same time.

How to use for-loop of while and check the value passing from drop-down list in if-statement.

Aucun commentaire:

Enregistrer un commentaire