0

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
        )
1
  • Could you show us the error message. Commented Nov 19, 2013 at 15:36

1 Answer 1

4

$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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your help and pointing over to the php manual! Such a simple solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.