Skip to content

Instantly share code, notes, and snippets.

@e1himself
Created April 27, 2015 09:24
Show Gist options
  • Save e1himself/16128b0afc2795dba711 to your computer and use it in GitHub Desktop.
Save e1himself/16128b0afc2795dba711 to your computer and use it in GitHub Desktop.
Download CSV Table
<?php
/**
* Convert PHP array to CSV.
* First line keys are used as column names.
*
* [
* ['ID' => 1, 'Name' => 'John'],
* ['ID' => 2, 'Name' => 'Jane'],
* ]
*
* will give
*
* ID,Name
* 1,John
* 2,Jane
*
* @param array $input_array
* @param string $delimiter
*
* @return string
*/
function array_to_csv($input_array, $delimiter)
{
/** open raw memory as file, no need for temp files */
$temp_memory = fopen('php://memory', 'w');
$header = reset($input_array);
fputcsv($temp_memory, array_keys($header), $delimiter);
/** loop through array */
foreach ($input_array as $line) {
/** default php csv handler **/
fputcsv($temp_memory, $line, $delimiter);
}
/** rewrind the "file" with the csv lines **/
fseek($temp_memory, 0);
$csv = stream_get_contents($temp_memory);
fclose($temp_memory);
return $csv;
}
/**
* Send csv content to browser for downloading
*
* @param string|array $csv
* @param string $output_file_name
*/
function send_csv($csv, $output_file_name)
{
if (is_array($csv)) {
$csv = array_to_csv($csv, ',');
}
/** modify header to be downloadable csv file **/
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
/** Send file to browser for download */
echo $csv;
}
<?php
/**
* @param array $input_array
*/
function array_to_html($input_array)
{
if (count($input_array) > 0): ?>
<table>
<thead>
<tr>
<?php foreach (array_keys(reset($input_array)) as $column): ?>
<th><?php echo $column ?></th>
<?php endforeach ?>
</tr>
</thead>
<tbody>
<?php foreach ($input_array as $row): ?>
<tr>
<?php foreach ($row as $cell): ?>
<td><?php echo htmlspecialchars($cell) ?></td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</table>
<?php endif;
}
<?php
require_once __DIR__.'/array_to_csv.php';
require_once __DIR__.'/array_to_html.php';
$data = array(
array('id'=>100,'Name'=>'Zelenia Moody','Phone'=>'186-5379'),
array('id'=>101,'Name'=>'Britanni Peck','Phone'=>'1-751-173-1357'),
array('id'=>102,'Name'=>'Elaine Whitfield','Phone'=>'1-815-638-3045'),
array('id'=>103,'Name'=>'Fritz Knight','Phone'=>'1-358-710-4177'),
array('id'=>104,'Name'=>'Shaine Contreras','Phone'=>'1-238-103-2662'),
array('id'=>105,'Name'=>'Alan Steele','Phone'=>'1-682-308-4650'),
array('id'=>106,'Name'=>'Oscar Knapp','Phone'=>'1-512-611-3695'),
array('id'=>107,'Name'=>'Curran Sears','Phone'=>'808-6003'),
array('id'=>108,'Name'=>'Phelan Bryan','Phone'=>'939-6383'),
array('id'=>109,'Name'=>'Gage Murray','Phone'=>'287-4423'),
array('id'=>110,'Name'=>'Aaron Sherman','Phone'=>'827-1617'),
array('id'=>111,'Name'=>'Galvin Hobbs','Phone'=>'1-132-231-2085'),
array('id'=>112,'Name'=>'Zena Howell','Phone'=>'565-3132'),
array('id'=>113,'Name'=>'Charde Ferguson','Phone'=>'856-2917'),
array('id'=>114,'Name'=>'Nathan Clayton','Phone'=>'1-112-467-4910'),
array('id'=>115,'Name'=>'Cameran Daniel','Phone'=>'1-302-406-9845'),
array('id'=>116,'Name'=>'Paki Juarez','Phone'=>'457-6986'),
array('id'=>117,'Name'=>'Stephen Rhodes','Phone'=>'767-0674'),
array('id'=>118,'Name'=>'Ivy Moran','Phone'=>'191-4339'),
array('id'=>119,'Name'=>'Brady Mccarthy','Phone'=>'1-339-499-5281'),
array('id'=>120,'Name'=>'Justina Vasquez','Phone'=>'128-2822'),
array('id'=>121,'Name'=>'Hu Wise','Phone'=>'381-5468'),
array('id'=>122,'Name'=>'Norman Sears','Phone'=>'1-475-918-0226'),
array('id'=>123,'Name'=>'Maggie Hansen','Phone'=>'1-141-996-8680'),
array('id'=>124,'Name'=>'Mariko Stein','Phone'=>'1-641-837-9531'),
array('id'=>125,'Name'=>'Mia Wolfe','Phone'=>'184-9772'),
array('id'=>126,'Name'=>'Cherokee Fisher','Phone'=>'836-7026'),
array('id'=>127,'Name'=>'Brianna Wilkinson','Phone'=>'643-9536'),
array('id'=>128,'Name'=>'Lisandra Fernandez','Phone'=>'1-125-995-4706'),
array('id'=>129,'Name'=>'Faith Eaton','Phone'=>'1-184-201-8104'),
array('id'=>130,'Name'=>'Caesar Rodgers','Phone'=>'228-0287'),
array('id'=>131,'Name'=>'Akeem Dudley','Phone'=>'787-1600'),
array('id'=>132,'Name'=>'Pearl Ashley','Phone'=>'119-9042'),
array('id'=>133,'Name'=>'David Madden','Phone'=>'289-8800'),
array('id'=>134,'Name'=>'Rogan Benjamin','Phone'=>'1-711-653-2310'),
array('id'=>135,'Name'=>'Kenneth Osborne','Phone'=>'1-986-469-7693'),
array('id'=>136,'Name'=>'Evan Duncan','Phone'=>'823-1234'),
array('id'=>137,'Name'=>'Irene Hendricks','Phone'=>'259-1417'),
array('id'=>138,'Name'=>'Lillith Molina','Phone'=>'336-6167'),
array('id'=>139,'Name'=>'Karyn Hogan','Phone'=>'1-227-447-3403'),
array('id'=>140,'Name'=>'Marshall Randall','Phone'=>'1-303-536-5560'),
array('id'=>141,'Name'=>'Justin Fuentes','Phone'=>'1-210-588-7866'),
array('id'=>142,'Name'=>'Forrest Hudson','Phone'=>'1-716-501-6507'),
array('id'=>143,'Name'=>'Ross Herrera','Phone'=>'1-325-457-7413'),
array('id'=>144,'Name'=>'Aphrodite Harrington','Phone'=>'1-898-884-3107'),
array('id'=>145,'Name'=>'Prescott Buchanan','Phone'=>'1-659-647-8476'),
array('id'=>146,'Name'=>'Merritt Mack','Phone'=>'1-746-971-7085'),
array('id'=>147,'Name'=>'Eleanor Blair','Phone'=>'981-6112'),
array('id'=>148,'Name'=>'Mara Frazier','Phone'=>'130-3714'),
array('id'=>149,'Name'=>'Prescott Hines','Phone'=>'1-699-505-4482'),
array('id'=>150,'Name'=>'Timon Burns','Phone'=>'1-332-816-3938'),
array('id'=>151,'Name'=>'Buckminster Kerr','Phone'=>'884-1700'),
array('id'=>152,'Name'=>'Elaine Tillman','Phone'=>'321-1672'),
array('id'=>153,'Name'=>'Priscilla Villarreal','Phone'=>'339-5614'),
array('id'=>154,'Name'=>'Aladdin Terrell','Phone'=>'1-637-672-0563'),
array('id'=>155,'Name'=>'Rachel Bray','Phone'=>'1-214-213-7156'),
array('id'=>156,'Name'=>'Audra Cooper','Phone'=>'683-5251'),
array('id'=>157,'Name'=>'Nero Romero','Phone'=>'582-2804'),
array('id'=>158,'Name'=>'Cruz Hogan','Phone'=>'1-246-885-6643'),
array('id'=>159,'Name'=>'Belle Jimenez','Phone'=>'985-3106'),
array('id'=>160,'Name'=>'Hannah Ross','Phone'=>'1-291-540-8481'),
array('id'=>161,'Name'=>'Vaughan Hughes','Phone'=>'1-526-630-9848'),
array('id'=>162,'Name'=>'Denise Gonzales','Phone'=>'1-330-821-6258'),
array('id'=>163,'Name'=>'Abraham Farrell','Phone'=>'258-1163'),
array('id'=>164,'Name'=>'Christine Lester','Phone'=>'207-5184'),
array('id'=>165,'Name'=>'Lydia Olsen','Phone'=>'806-7781'),
array('id'=>166,'Name'=>'Cally Hebert','Phone'=>'1-223-582-7671'),
array('id'=>167,'Name'=>'Alexandra Flynn','Phone'=>'1-714-360-0962'),
array('id'=>168,'Name'=>'Nerea Mccray','Phone'=>'701-1589'),
array('id'=>169,'Name'=>'Melanie Vasquez','Phone'=>'1-795-193-0428'),
array('id'=>170,'Name'=>'Savannah Mcpherson','Phone'=>'1-227-154-3890'),
array('id'=>171,'Name'=>'Holmes Austin','Phone'=>'220-6517'),
array('id'=>172,'Name'=>'Illiana Browning','Phone'=>'1-283-958-6024'),
array('id'=>173,'Name'=>'Jael Berry','Phone'=>'1-811-568-7288'),
array('id'=>174,'Name'=>'Clark Buckner','Phone'=>'1-718-672-4583'),
array('id'=>175,'Name'=>'Vincent Mills','Phone'=>'294-3013'),
array('id'=>176,'Name'=>'Myles Gibson','Phone'=>'227-6775'),
array('id'=>177,'Name'=>'Quentin Douglas','Phone'=>'338-2142'),
array('id'=>178,'Name'=>'Cheyenne Cunningham','Phone'=>'1-325-421-4656'),
array('id'=>179,'Name'=>'Jermaine Fisher','Phone'=>'1-770-203-1486'),
array('id'=>180,'Name'=>'Ori Leblanc','Phone'=>'1-458-693-3612'),
array('id'=>181,'Name'=>'Colorado Padilla','Phone'=>'444-0908'),
array('id'=>182,'Name'=>'Lamar David','Phone'=>'1-894-752-7660'),
array('id'=>183,'Name'=>'Liberty Burton','Phone'=>'170-1339'),
array('id'=>184,'Name'=>'Holly Mcfarland','Phone'=>'1-244-619-6389'),
array('id'=>185,'Name'=>'Nola Faulkner','Phone'=>'1-121-617-0812'),
array('id'=>186,'Name'=>'Isaac Merritt','Phone'=>'700-8828'),
array('id'=>187,'Name'=>'Macaulay Bowman','Phone'=>'571-2433'),
array('id'=>188,'Name'=>'Camille Rojas','Phone'=>'527-5066'),
array('id'=>189,'Name'=>'Olga Malone','Phone'=>'672-5141'),
array('id'=>190,'Name'=>'Aimee Farmer','Phone'=>'177-1319'),
array('id'=>191,'Name'=>'Nell Oneal','Phone'=>'656-7532'),
array('id'=>192,'Name'=>'Orlando Spence','Phone'=>'1-320-630-5065'),
array('id'=>193,'Name'=>'Lynn Terrell','Phone'=>'1-464-637-1646'),
array('id'=>194,'Name'=>'Noel Patrick','Phone'=>'1-808-461-0801'),
array('id'=>195,'Name'=>'Trevor Mcdonald','Phone'=>'1-615-516-7657'),
array('id'=>196,'Name'=>'Thane Slater','Phone'=>'767-6917'),
array('id'=>197,'Name'=>'Moses Farmer','Phone'=>'596-6432'),
array('id'=>198,'Name'=>'Olivia Goodman','Phone'=>'1-376-151-3670'),
array('id'=>199,'Name'=>'Lillith Hopkins','Phone'=>'1-326-905-0412')
);
if (isset($_GET['csv']) && $_GET['csv']) {
send_csv(array_to_csv($data, ','), 'names_and_phones.csv');
} else {
$download_url = '/index.php?csv=1';
require_once __DIR__.'/show.php';
}
<?php
/**
* @var array $data
* @var string $download_url
*/
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Names and Phones</title>
</head>
<body>
<h1>Names and Phones</h1>
<a href="<?php echo $download_url ?>">Download table</a>
<hr/>
<?php array_to_html($data) ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment