Question

I am trying to retrieve certain elements from a csv file to insert into our client reporting database. I'm trying to write a function that will parse the data and return the necessary information. However, my function is returning an undefined variable and I'm not quite sure how to solve this issue. I'm sure it's a beginner's problem, but I am still new to writing my own functions and arrays in php. I appreciate the help! There is also no API to dial directly in for a more reliable data source.

Code Below:

// Reading the csv file, returning an array of rows
$rows = array();

foreach (file('prerolldaily.csv') as $line){
    $rows[] = str_getcsv($line);
}   

function extractInfo($e){
    $returnImpressionsRow = $rows[$e][4];
    $returnClicksRow = $rows[$e][5];
    $returnCompletedViewsPercentage = $rows[$e][7];
    $returnCPM = $rows[$e][8];
    $returnTotalSpend = $rows[$e][9];
    $clientName = $rows[$e][11];
    $finalPercentage = substr($returnCompletedViewsPercentage, 0, -1);
    $finalCPM = substr($returnCPM, 1);
    $totalSpend = substr($returnTotalSpend, 1);
    return $clientName . " Impressions: ". $returnImpressionsRow . " Clicks: " . $returnClicksRow. " Completed Views: " . $finalPercentage . "%" . " CPM: $" . $finalCPM . " Total Spend: $" . $totalSpend;
}

// Tell browser to treat this as a Plain Text file
header('Content-Type: text/plain');

extractInfo(0);

print_r($rows)

Screen Output: (there are upwards of 15 clients I need to report on) A few elements are removed (client names)

Array
(
    [0] => Array
        (
            [0] => switch active/paused CLIENT NAME CLIENT NAME Sites Ads Pre-roll (30s)
            [1] => Day 12 out of 19 Nov 8, 2013 Nov 26, 2013
            [2] => $20.00
            [3] => $50.00
            [4] => 3,360
            [5] => 12
            [6] => 0.36%
            [7] => 57%
            [8] => $14.46
            [9] => $48.58
            [10] => edit placement copy placement delete see report attach ad
            [11] => CLIENT NAME
        )
Was it helpful?

Solution

$rows is undefined in your function, you need to send it as a parameter:

function extractInfo($rows, $e){
    $returnImpressionsRow = $rows[$e][4];
    $returnClicksRow = $rows[$e][5];
    $returnCompletedViewsPercentage = $rows[$e][7];
    $returnCPM = $rows[$e][8];
    $returnTotalSpend = $rows[$e][9];
    $clientName = $rows[$e][11];
    $finalPercentage = substr($returnCompletedViewsPercentage, 0, -1);
    $finalCPM = substr($returnCPM, 1);
    $totalSpend = substr($returnTotalSpend, 1);
    return $clientName . " Impressions: ". $returnImpressionsRow . " Clicks: " . $returnClicksRow. " Completed Views: " . $finalPercentage . "%" . " CPM: $" . $finalCPM . " Total Spend: $" . $totalSpend;
}

...

extractInfo($rows, 0);

Check about variable scope in the php manual.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top