Search code examples
phparraysjsondrupal-7

How to generate the CSV and PDF selecting two dates json value in Drupal7


drupal_add_http_header('Content-Type', 'text/csv');
drupal_add_http_header('Content-Disposition','attachment;filename=csvfile.csv');
 // code indentation    
 $fp = fopen('php://transactions', 'w')
 foreach ($result->response as $transaction) {
     fputcsv($fp, $transaction);
 }
 fclose($fp);
 drupal_exit();

The (result->response as transaction) contains all the data, with that only i arrange in table format to view for the customer. This same data i am selecting (from and to->date result) to be download in CSV.

Print of transaction: In stdClassObject([card]=>1234,[fromdate]=>4/1/2016,[amount]=>5000)

Suggestion Needed.


Solution

  • Instead of

    fputcsv($fp, $transaction);
    

    Use

    fputcsv($fp, array($transaction->card, $transaction->fromdate, $transaction->amount));
    

    for formatting the numbers you can use as below:

    $number = (float) str_replace( ',', '', '5,000.00');
    echo number_format($number, '2','.', '');
    

    Thanks