Naresh
Naresh

Reputation: 2781

Export mysql data in CSV in CakePHP

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);

Upvotes: 1

Views: 1263

Answers (5)

SohanLal Saini
SohanLal Saini

Reputation: 458

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);
}

Upvotes: 0

Ketan
Ketan

Reputation: 428

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'];

Upvotes: 1

Oldskool
Oldskool

Reputation: 34837

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.

Upvotes: 2

SohanLal Saini
SohanLal Saini

Reputation: 458

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);
    }

Upvotes: 1

ndm
ndm

Reputation: 60463

$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

Upvotes: 1

Related Questions