summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Kinkade <nath@nkinka.de>2013-11-26 23:16:04 +0000
committerNathan Kinkade <nath@nkinka.de>2013-11-26 23:16:04 +0000
commit917f9f6602659fb5d0a859cb8035f808f7a0ecf5 (patch)
tree70abd61b59823ec5a66ac0c1faf95930f4f5aa05
parent2bfa063c9a7444c701d98af50839974b0634ffdb (diff)
Added the ability for a user to export search results to a CSV file.
-rw-r--r--export.php24
-rw-r--r--search.php7
-rw-r--r--style.css8
3 files changed, 39 insertions, 0 deletions
diff --git a/export.php b/export.php
new file mode 100644
index 0000000..2ceca89
--- /dev/null
+++ b/export.php
@@ -0,0 +1,24 @@
+<?php
+
+$dbh = new PDO('mysql:host=localhost;dbname=mdcc', 'mdcc', 'Mdcc.');
+
+$filename = '/var/www/mdcc/csv_exports/mdcc_export.csv';
+
+header('Content-Type: text/csv');
+header("Content-Disposition: attachment; filename={$filename}");
+header('Pragma: no-cache');
+
+$sql = $_REQUEST['sql'];
+$sql .= " INTO OUTFILE '{$filename}'";
+
+$st = $dbh->prepare($sql);
+if ( $st->execute() ) {
+ readfile($filename);
+} else {
+ $errors = $st->errorInfo();
+ print_r($errors);
+}
+
+unlink($filename);
+
+?>
diff --git a/search.php b/search.php
index e8b5b31..c2409c5 100644
--- a/search.php
+++ b/search.php
@@ -279,6 +279,10 @@ echo "<a id='search_again' href='/'>New Search</a>";
$st = $dbh->prepare($sql);
if ( $st->execute() ) {
$row_count = $st->rowCount();
+ if ( $row_count == '0' ) {
+ echo "<h3>No records found.</h3>";
+ exit;
+ }
if ( $row_count <= $results_per_page ) {
echo "<h3>{$row_count} records found.</h3>";
} elseif ( $_GET['page'] ) {
@@ -288,6 +292,9 @@ if ( $st->execute() ) {
} else {
echo "<h3>Viewing 1 to {$results_per_page} of {$row_count} records found.</h3>";
}
+ $url_sql = urlencode($sql);
+ // Give the user an export button
+ echo "<div id='export'><a href='export.php?sql=$url_sql'>Export</a></div>";
}
# Pagination
diff --git a/style.css b/style.css
index c762e50..5f2c712 100644
--- a/style.css
+++ b/style.css
@@ -74,3 +74,11 @@ label {
}
label { vertical-align: top; }
+
+#export {
+ margin-bottom: 1ex;
+}
+
+#export a {
+ color: blue;
+}