results in CSV

2 posts by 2 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: July 24, 2013   (RSS)

By gregThomas - July 24, 2013

Hi Tim,

If your generating your report using a getRecords function you could do something like this to create a CSV file of the data:


  // load records from 'blog'
  list($blogRecords, $blogMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'loadUploads' => false,
    'allowSearch' => false,
    'loadListDetails' => false,
    'loadPseudoFields' => false
  ));


  foreach($blogRecords as $key => $blog){
    $csv = ''; 
    if($key == '0'){
      $csv .= "'".implode("', '", array_keys($blog)) . "'\r\n";
    }
    $csv .= "'".implode("', '", $blog) . "'\r\n"; 
  }


  sendMessage(array( 
      'from' => "test@example.com", 
      'to' => "test@example.com", 
      'subject' => "Application Form", 
      'text' => "Text message content", 
      'attachments' => array( 
        'form.csv' => $csv 
    ) 
  ));

This is just example code, so you will have to make a few changes to get it working how you want with your page.

So the foreach loop cycles through the blog records and generates a line for the CSV file. If it is the first line of the foreach loop then the array keys are used for the title fields of the CSV file, they are generated using the array_keys function which returns an array of an arrays keys. 

You would also have to add a wrapper of code to detect when user has clicked a link to be e-mailed the CSV.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com