0

I have a home work from the PHP class, and the teacher ask me to calculate the revenue for the retailer based on the product they sold and country of the buyer. Below are the products and the country, let's call them as Tier:

ProductTier = Apple, Orange, Banana, Watermelon, Grape, Pear

CountryTier:
    CountryTier1 = US, CA
    CountryTier2 = FR, UK
    CountryTier3 = JP, SG

RevenueTier:
    RevenueTier1 = 2, 4 ,6, 8, 10, 12
    RevenueTier2 = 1, 3, 5, 7, 9, 11
    RevenueTier3 = 1, 1, 1, 1, 1, 1

With that table, what i have to do is get the buyer country, check what they bought and print revenue for the retailer. I have wrote a small function to check the buyer country and then return their CountryTier, but i don't know what to do next. Assuming that there are a buyer come from the UK and they bought the Orange so the retailer will have $3 for the revenue, do you have any suggestion for me to do this exercise with less of code.

ps: Please correct/ask me if something not clearly.

Duong Truong
  • 29
  • 1
  • 5
  • 9
  • 2
    I have a suggestion: do not put YOUR homework up here in the hopes of someone else doing it for you. – Tularis Feb 16 '14 at 12:11
  • I would suggest to use SQL to calculate revenue, PHP is a good way to publish this report as HTML document. I think you do not need any classes for reporting purposes. Star Schema is a typical solution to this problem http://en.wikipedia.org/wiki/Star_schema this article in Wikipedia uses very similar example too. – jbaliuka Feb 16 '14 at 12:19
  • Excuse me @Tularis, I'm not ask for someone to do my homework, i just need a suggestion for the best way to do it. – Duong Truong Feb 16 '14 at 13:05
  • The best way I can think of is putting the countries in an associative array as such `$countries["{COUNTRY_NAME}"] = {TEIR}` then you can pass the country code in, and get the tier. From there you can have an array of your products, return the index and use that on the revenue table. Inline with what @Tularis said (for which I agree) this is pretty basic problem solving bro, all you need to do is re-arrange the data to suit what you want to do. – Michael Coxon Feb 16 '14 at 13:12

1 Answers1

0

Store your revenues like :

$revenue = array();
$revenue[1] =  array(2, 4 ,6, 8, 10, 12);
$revenue[2] =  array(1, 3, 5, 7, 9, 11);
$revenue[3] =  array(1, 1, 1, 1, 1, 1);

Store the ProductTiers as

$productTiers = array('Apple', 'Orange', 'Banana', 'Watermelon', 'Grape', 'Pear');

Now get the key for ProductTier using

$key = array_search($fruitName, $productTiers);

Now you can get the value using

$revenue[$countryTier][$key];
Akshat Singhal
  • 1,801
  • 19
  • 20