skip to Main Content

i have this question :
i have $number_of_monthes = number of months with value =("1","3","6","12")
i have $number_of_pages = how many page is active on account with value=("1","3","7")
i have $marketing_level = how many ads can the user add into account with value=("25","100","300","500")

if($data['marketing_level'] == 100){
       if($data['number_of_monthes '] == 1)
              $marketing_cost = ( 50  * $number_of_monthes );
       if($data['number_of_monthes '] == 3)
              $marketing_cost = ( 50  * $number_of_monthes * $number_of_pages * 0.85 );
       if($data['number_of_monthes '] == 6)
              $marketing_cost = ( 50  * $number_of_monthes * $number_of_pages * 0.70 );
       if($data['number_of_monthes '] == 12)
              $marketing_cost = ( 50  * $number_of_monthes * $number_of_pages * 0.55 );

as you can see, if the user bought the 3 months package, then he will got a sale (15%)
and if he bought the 6 months package, then he will got a 30% sale

so how to also add the ability’s to got more sale when the number of pages get higher ?
i mean i want also like this :
if the number of months=3 then sale 15% and if the number of pages =3 then the sale will become 20%
if the number of months=3 then sale 15% and if the number of pages =7 then the sale will become 30%

if the number of months=6 then sale 30% and if the number of pages =3 then the sale will become 40%
if the number of months=6 then sale 30% and if the number of pages =6 then the sale will become 45%
and so on
thanks for your help in advanced

i searched a lot about it, but unfortunately found nothing

2

Answers


  1. I suppose you know the percentage of rebate for each Month option and also for each Page number.
    If so, you can do something like this to calculate the rebate coefficient:

    function cumulativeRebateCoeff(int $number_of_months, int $number_of_pages) : float
    {
        // Rebate by months
        $rebateByMonth = [
            1 => 0,
            3 => 0.15,
            6 => 0.30,
            12 => 0.45,
        ];
    
        // Rebate by number of pages
        $rebateByPages = [
            1 => 0,
            3 => 0.05,
            7 => 0.15,
        ];
    
        if (! in_array($number_of_months, $rebateByMonth)) {
            // TODO: handle the case if $number_of_months is not in [1, 3, 6, 12]
        }
        
        if (! in_array($number_of_pages, $rebateByPages)) {
            // TODO: handle the case if $number_of_pages is not in [1, 3, 7]
        }
    
        
        return 1 - $rebateByMonth[$number_of_months] - $rebateByPages[$number_of_pages];
    }
    

    After that you’ll multiply your base price by the value retuned from the function.

    Examples:

    1. For 1 month and 3 pages the function will return you 0.7 (1 – 0 – 0.3) = 70% of base price
    2. For 6 months and 7 pages => 0.55 (1 – 0.3 – 0.15) = 55% of base price
    Login or Signup to reply.
  2. These kind of problems you can solve with a matrix (perhaps not the right word), think of it as a table, but in more than two dimensions:

    PagesMonths | 1 | 3 | 6 | 12 |
    -------------+---+---+---+----+
         1       |   |   |   |    |
    -------------+---+---+---+----+
         3       |   |   |   |    |
    -------------+---+---+---+----+
         7       |   |   |   |    |
    -------------+---+---+---+----+
    

    Each cell now contains the (calculated/intended) value (currently blank). That are 12 "boxes" to fill.

    Now you add another dimension, let’s think you turn it side-wards and it has some little depth and look on it from the side, and then extend (left to right) for the next category, e.g. marketing level, just this time it needs one row of values for each cell, and here in the reduced schema it only shows one, as we look from only one side:

    PM| 25 | 100 | 300 | 500 |
    +-+----+-----+-----+-----+
    | |    |     |     |     |
    +-+----+-----+-----+-----+
    | |    |     |     |     |
    +-+----+-----+-----+-----+
    | |    |     |     |     |
    +-+----+-----+-----+-----+
    

    This are another 12 fields, but of all the stacks only 12 boxes visible but if we look up again, there are another three columns that are not visible, so there are 36 more boxes or in total 48: four times the twelve boxes of the first two dimensions for the third dimension that has four values.

    In PHP that can be created with a multi dimensional array.

    Let’s say the first dimension is Pages:

    $matrix[1]
    $matrix[3]
    $matrix[7]
    

    And the second dimension is Months:

    $matrix[1][1]
    $matrix[1][3]
    $matrix[1][6]
    $matrix[1][12]
    
    $matrix[3][1]
    $matrix[3][3]
    $matrix[3][6]
    $matrix[3][12]
    
    $matrix[7][1]
    $matrix[7][3]
    $matrix[7][6]
    $matrix[7][12]
    

    And the third dimension is Marketing Level (what a dull exmaple btw.):

    $matrix[1][1][25]
    $matrix[1][1][100]
    $matrix[1][1][300]
    $matrix[1][1][500]
    
    $matrix[1][3][25]
    $matrix[1][3][100]
    $matrix[1][3][300]
    $matrix[1][3][500]
    
    $matrix[1][6][25]
    $matrix[1][6][100]
    $matrix[1][6][300]
    $matrix[1][6][500]
    
    $matrix[1][12][25]
    $matrix[1][12][100]
    $matrix[1][12][300]
    $matrix[1][12][500]
    
    $matrix[3][1][25]
    ...
    $matrix[3][3][25]
    ...
    $matrix[3][6]
    
    ...
    
    $matrix[7][12][500]
    

    Now if you have all levels complete, here we stop at three, assign the value of each:

    $matrix[1][1][25] = 0.8276
    

    Then you can always look it up by the integer number in the keys. This allows you to have a different factor / calculation per "box"/field.

    Or if the calculation is extremely expensive, to calculate it once and store it in this form. It then can be quickly looked up in the "table".

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search