function csv_file($data, $cols, $filename = '') { csv_header($filename); echo csv_row(csv_cols($cols)); foreach ($data as $row) { $data = array(); foreach ($cols as $i => $name) { $data[] = !is_int($i) && is_callable($name) ? $name($row) : $row->{$name}; } echo csv_row($data); } if ($filename) { exit; } }
function export_csv($query, $separator) { ob_cleanup(); set_time_limit(0); if (!is_select($query)) { trigger_error('export_csv() failed: not a SELECT query: ' . $query, E_USER_ERROR); } $table = table_from_query($query); if (!$table) { $table = 'unknown'; } header("Cache-control: private"); header("Content-type: application/octet-stream"); header('Content-Disposition: attachment; filename=' . $table . '_' . date('Ymd') . '.csv'); $rs = db_query($query); $first = true; while ($row = db_row($rs)) { if ($first) { echo csv_row(array_keys($row), $separator); $first = false; } echo csv_row($row, $separator); flush(); } exit; }
</body> </html> <?php } else { header('Content-Type: text/csv'); header("Content-Disposition: attachment; filename=export.csv"); $emails = array(); echo "First Name,Last Name,Company,Email\r\n"; if ($_REQUEST['export_customers'] && $_REQUEST['export_customers_group']) { $user_query = mysql_query("SELECT c.customers_id, c.customers_firstname, c.customers_lastname, a.customers_id, c.customers_email_address,a.entry_company \n\t\t\t\t\t\t\t\tFROM " . TABLE_CUSTOMERS . " c, address_book a \n\t\t\t\t\t\t\t\tWHERE c.customers_group_id IN ('" . join("','", $_REQUEST['export_customers_group']) . "')\n\t\t\t\t\t\t\t\tAND c.customers_id = a.customers_id\n\t\t\t\t\t\t\t\t"); while ($row = mysql_fetch_array($user_query)) { if (isset($emails[$row['customers_email_address']])) { continue; } $emails[$row['customers_email_address']] = true; echo csv_row(array($row['customers_firstname'], $row['customers_lastname'], $row['entry_company'], $row['customers_email_address'])); } } if ($_REQUEST['export_subscribers']) { $user_query = mysql_query('select * from subscribers'); while ($row = mysql_fetch_array($user_query)) { if (isset($emails[$row['subscribers_email_address']])) { continue; } $emails[$row['subscribers_email_address']] = true; echo csv_row(array($row['subscribers_firstname'], $row['subscribers_lastname'], $row['subscribers_email_address'])); } } } require DIR_WS_INCLUDES . 'application_bottom.php';