Posted By

rodrigo on 08/09/08


Tagged

mysql class php db array php5 csv reports resulset


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

romanos


CSV Reports


 / Published in: PHP
 

URL: http://reactor.cl

  1. <?php
  2.  
  3. /* Usage:
  4. /*
  5. /* $report = new Reports_Core; (In Kohana just new Reports;)
  6. /* print $report->csv($result_set_or_array, 'a title', ';');
  7. /*
  8. /*--------------------------------------------------------------*/
  9.  
  10. class Reports_Core
  11. {
  12. function csv($lines = FALSE, $title = '', $delimiter = ';')
  13. {
  14. $output = '';
  15. if($title) $output .= $title."\n";
  16.  
  17. if($lines)
  18. {
  19. $report_line = array();
  20.  
  21. foreach($lines as $line)
  22. {
  23. $line = implode($delimiter, (array) $line);
  24. $report_line[] = $line."\n";
  25. }
  26.  
  27. $output .= implode('', $report_line);
  28.  
  29. return $output;
  30. }
  31. else
  32. {
  33. trigger_error('There is no data to generate a CSV report.', E_USER_ERROR);
  34. }
  35.  
  36. }
  37. }
  38.  
  39. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: rodrigo on August 9, 2008

Real world usage:

php

...

header('Content-type: text/csv'); header('Content-disposition: attachment; filename="report_'.$this->person->name.'.csv"');

$historic = $this->reports->gethistoric($person->id, Utils::getdate()); // Get historic resultset view from DB where date is provided by helper Utils method get_date();

print Reports::csv($historic);

Posted By: rodrigo on August 14, 2008

Actually you should replace if($title) echo $title."\n"; for something like if($title) $output = $title."\n"; and instead of return implode... $output .= implode... and then return all together.

You need to login to post a comment.