vendredi 2 février 2018

How to design the code so I dont repeat code. Keep it DRY [on hold]

Currently I am fetching data from a table like this:

array:3 [▼
  0 => array:20 [▼
    "id" => 1
    "campaign_id" => 1042982552
    "total_cost" => "0.00"
    "average_cpc" => "0.000"
    "clicks" => 0
    "average_cost" => "0.00"
    "average_position" => "0.00"
    "percentage_shown" => "0.00"
    "interaction_frequency" => "0.00"
    "interactions" => 0
    "ctr" => "0.00"
    "conversions" => "0"
    "ad_type" => "Ad Group #3"
    "label" => "--"
    "final_dest_url" => "--"
    "status" => "enabled"
    "impressions" => 0
    "api_slug" => "google-adwords"
    "created_at" => "2018-02-02 09:30:13"
    "updated_at" => "2018-02-02 09:30:13"
  ]
  1 => array:20 [▼
    "id" => 2
    "campaign_id" => 2323
    "total_cost" => "22.00"
    "average_cpc" => "1.120"
    "clicks" => 2222
    "average_cost" => "1.23"
    "average_position" => "1.32"
    "percentage_shown" => "122.00"
    "interaction_frequency" => "1.25"
    "interactions" => 45
    "ctr" => "44.00"
    "conversions" => "85"
    "ad_type" => "Ad Group #3"
    "label" => "--"
    "final_dest_url" => "--"
    "status" => "enabled"
    "impressions" => 8555
    "api_slug" => "google-adwords"
    "created_at" => "2018-02-02 09:30:13"
    "updated_at" => "2018-02-02 09:30:13"
  ]
  2 => array:20 [▼
    "id" => 3
    "campaign_id" => 2323
    "total_cost" => "22.00"
    "average_cpc" => "1.120"
    "clicks" => 2222
    "average_cost" => "1.23"
    "average_position" => "1.32"
    "percentage_shown" => "122.00"
    "interaction_frequency" => "1.25"
    "interactions" => 45
    "ctr" => "44.00"
    "conversions" => "85"
    "ad_type" => "Ad Group #3"
    "label" => "--"
    "final_dest_url" => "--"
    "status" => "enabled"
    "impressions" => 8555
    "api_slug" => "google-adwords"
    "created_at" => "2018-02-02 09:30:13"
    "updated_at" => "2018-02-02 09:30:13"
  ]
]

Actual state: I display those data on a table each row.

Table actual state:

clicks |  interactions | total_cost
______________________________________
23     |   34          |  344 usd
43     |   54          |  233 usd

Point I want to go I want to have another table which is going to contain the same columns but the data its going to be sum of all the three arrays or sum of each column in first table.

What I want to achieve

sum(clicks) |  sum(interactions) | sum(total_cost)
__________________________________________________
    66      |   88               |  577 usd

Now I know how I could do this by iterating the array and sum the specific columns.

Issue

I have to do this to at least 5 different tables, so is there any way to abstract or any suggestion in design pattern on how should I structure the code ?

What pattern do you suggest so I don't repeat my code by iterating each table every time ?

Aucun commentaire:

Enregistrer un commentaire