1

In CakePHP, with the following code i am trying to Export users email in CSV. I am getting Errors.

Code Refrence site

Error:

Notice (8): Undefined offset: 0 [APP\View\Frontusers\admin_exportemails.ctp, line 12]

Warning (2): fputcsv() expects parameter 2 to be array, null given [APP\View\Helper\CsvHelper.php, line 36]

Notice (8): Undefined offset: 1 [APP\View\Frontusers\admin_exportemails.ctp, line 12]

Warning (2): fputcsv() expects parameter 2 to be array, null given [APP\View\Helper\CsvHelper.php, line 36]

admin_exportemails.ctp

$line= $useremails[0]['Frontuser'];
$this->CSV->addRow(array_keys($line));

foreach ($useremails as $key => $useremail)
{
      $line =$useremail[$key]['Frontuser'];
       $this->CSV->addRow($line);
}
$filename='useremails';
echo  $this->CSV->render($filename);
1
  • When posting errors it is advised to highlight the line in your code on which it is triggered! Commented Jul 30, 2014 at 11:29

5 Answers 5

2

You're messing up your foreach. You split it up in the $key and the $useremail sub-array, which is OK. But then you iterate over it and try to access $useremail[$key]['Frontuser'] again, which is nonexistent at that point.

foreach ($useremails as $key => $useremail)

This causes the [0] and [1] in the original $useremails array to be set as $key, but you iterate over all the items over the $useremails, so you can simply:

$line = $useremail['Frontuser'];

You don't need the $key, since that's not part of the iterated item, e.g. the first time your foreach runs, it sees this:

[Frontuser] => Array
    (
        [name] => Rash
        [email] => rash.com
    )

And on the second iteration it sees this:

[Frontuser] => Array
    (
        [name] => John
        [email] => [email protected]
    )

So there is no [0] or [1] index anymore.

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

Comments

1
$useremail[$key]['Frontuser'];

should be

$useremail['Frontuser'];

Actually there's no need for your code to include the key in the foreach loop at all.

That's PHP 101, so for further information please refer to the manual: http://php.net/foreach

Comments

1

You can simply pass your data array in that function and your csv generated in webroot folder. Note:- 1. You should put blank csv file at webroot folder. 2. you should store all information in a sub array which contain only values not Model index.

function generate_csv($data_array=array()){
//        pr($data_array);die;

        foreach ($data_array as $key => $value) { 
            $newdata[] = $key.','.$value;
        }

//        pr($newdata);die;
    $f = fopen(APP.'webroot/csv_file.csv', 'w+');

    foreach ($newdata as $line) { 
        fputcsv($f, array($line), ','); 
    }

    fseek($f, 0);

    fclose($f);
    }

Comments

1

you made mistake in the for loop and iterated it in wrong way.just ommit the below line from the foreach loop.

$line =$useremail[$key]['Frontuser'];

Comments

0

Here have tow options
1. save csv file in folder not sent output to browser and
2. send output to browser

function generate_csv($data_array = array()) {

    //$f = fopen(APP . 'webroot/files/unsaved_bars.csv', 'w+'); // for save csv file in folder not sent output to browser 
    $f = fopen("php://output", "a"); // for send output to browser 

    $headers[] = array('id','name','other_info'); // for header row
    $data_array = array_merge($headers, $data_array);

    foreach ($data_array as $line) {
        fputcsv($f, $line, ',');
    }
    //fseek($f, 0);
    fclose($f);
}

Comments

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.