$group_id)); $session_members = $session_members_query->fetchAll(); //generate the row data foreach ($session_members as $member) { if (! isset($data[$member->booking_session_id])) { $data[$member->booking_session_id] = array(); } // @todo // lookup the actual name and put this instead of just the node id $data[$member->booking_session_id][] = $member->booking_node_id; } watchdog('booking_debug', "
Study Group CSV Report\n@info
", array('@info' => print_r( $data_array, true))); //calculate the CSV layout $header_array = array_keys($data); $maximums = array(); foreach ($header_array as $column) { $maximums[] = count($data[$column]); } //add the column headings to the CSV $header = implode( $delimiter, $header_array ); $csv .= $header . "\n"; //generate each row for the CSV for ($i = 0; $i < max($maximums); $i++) { $output = array(); foreach ($header_array as $column) { $field = isset($data[$column][$i]) ? $data[$column][$i] : ''; //enclose $field if necessary if ( $encloseAll || preg_match( "/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field ) ) { $output[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure; } else { $output[] = $field; } } //loop through columns $row = implode($delimiter, $output) . "\n"; $csv .= $row; } //output the CSV to the browser drupal_add_http_header("Content-type", "application/octet-stream; charset=utf-8"); drupal_add_http_header("Content-Disposition", "attachment; filename=" . $name . ".csv"); print $csv; exit(0); } /** * Function taken from http://php.net/manual/en/function.fputcsv.php#87120 */ function fputcsv2 ($fh, array $fields, $delimiter = ',', $enclosure = '"', $mysql_null = false) { $delimiter_esc = preg_quote($delimiter, '/'); $enclosure_esc = preg_quote($enclosure, '/'); $output = array(); foreach ($fields as $field) { if ($field === null && $mysql_null) { $output[] = 'NULL'; continue; } $output[] = preg_match("/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field) ? ( $enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure ) : $field; } fwrite($fh, join($delimiter, $output) . "\n"); }