Skip to content

Commit 8ddebae

Browse files
authored
Add export to csv function
1 parent c09720f commit 8ddebae

File tree

1 file changed

+56
-25
lines changed

1 file changed

+56
-25
lines changed

index.php

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,47 @@
11
<?php
22
/**
33
* @author Gustavo Novaro
4-
* @version 1.0.4
4+
* @version 1.0.5
55
*/
66
define('APP_NAME','SQL Server Admin');
77
require('config.php');
88
$connection = odbc_connect("Driver={SQL Server};Server=$server;Database=$database;", $user, $password);
99

1010
$query = !empty($_POST['query']) ? $_POST['query'] : null;
11+
12+
function exportCSV($data)
13+
{
14+
$filename = 'export_'.date('Ymd_His').'.csv';
15+
header( 'Content-Type: text/csv' );
16+
header( 'Content-Disposition: attachment;filename='.$filename);
17+
$out = fopen('php://output', 'w');
18+
foreach($data as $line) {
19+
if(is_array($line)){
20+
fputcsv($out, $line);
21+
}
22+
}
23+
fclose($out);
24+
}
25+
26+
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'export')
27+
{
28+
$i = 0;
29+
$result = @odbc_exec($connection,$query);
30+
if(!empty($result)) {
31+
$columns = odbc_num_fields($result);
32+
for ($j=1; $j<= $columns; $j++)
33+
{
34+
$header[] = odbc_field_name ($result, $j );
35+
}
36+
$data[] = $header;
37+
38+
while($row = odbc_fetch_array($result)){
39+
$data[] = $row;
40+
}
41+
exportCSV($data);
42+
}
43+
die;
44+
}
1145
?>
1246
<!doctype html>
1347
<html>
@@ -47,12 +81,17 @@
4781
<form id="frm-query" method="post">
4882
<div class="form-group">
4983
<div id="queryEditor"><?php echo $query;?></div>
50-
<!--<textarea name="query" id="query" required="required" cols="120" placeholder="Add your SQL query heare. Ex: SELECT * FROM table" class="form-control"><?php echo $query;?></textarea>-->
5184
<input type="hidden" name="query" id="query">
5285
</div>
5386
<div class="form-group">
54-
<button type="submit" class="btn btn-primary">Run <span class="glyphicon glyphicon-play"></span></button> (Run query with F9)
87+
<div class="col-md-6">
88+
<button type="button" class="btn btn-primary" onclick="setAction('query')">Run <span class="glyphicon glyphicon-play"></span></button> (Run query with F9)
89+
</div>
90+
<div class="col-md-6">
91+
<button type="button" class="btn btn-success" onclick="setAction('export')">Export csv <span class="fa fa-file-excel-o"></span></button>
92+
</div>
5593
</div>
94+
<input type="hidden" name="action" id="action" value="query">
5695
</form>
5796
<?php
5897
if(!empty($query))
@@ -63,49 +102,40 @@
63102
<?php
64103
// Get Data From Result
65104
if(!empty($result)):
66-
$rows_count = odbc_num_rows ($result);
105+
$rows_count = odbc_num_rows($result);
106+
$columns = odbc_num_fields($result);
67107
?>
68108
<div>
69109
<strong>Rows count:</strong> <?php echo $rows_count;?>
70110
</div>
71111
<div class="table-responsive">
72112
<table class="table table-bordered table-striped">
113+
<thead>
114+
<tr>
73115
<?php
74-
$i = 0;
75-
while($data[] = odbc_fetch_array($result)):
116+
for ($j=1; $j<= $columns; $j++):
117+
$key = odbc_field_name ($result, $j );
76118
?>
77-
119+
<th><?php echo $key;?></th>
78120
<?php
79-
//Encabezado
80-
if($i == 0):
121+
endfor;
81122
?>
82-
<thead>
83-
<tr>
84-
<?php
85-
foreach($data[$i] as $key => $val):
86-
?>
87-
<th><?php echo $key;?></th>
88-
<?php
89-
endforeach;
90-
?>
91-
</tr>
92-
</thead>
93-
<tbody>
123+
</tr>
124+
</thead>
125+
<tbody>
94126
<?php
95-
endif
127+
while($row = odbc_fetch_array($result)):
96128
?>
97-
98129
<tr>
99130
<?php
100-
foreach($data[$i] as $key => $val):
131+
foreach($row as $key => $val):
101132
?>
102133
<td><?php echo $val;?></td>
103134
<?php
104135
endforeach;
105136
?>
106137
</tr>
107138
<?php
108-
$i++;
109139
endwhile;
110140
?>
111141
</tbody>
@@ -151,5 +181,6 @@
151181
}
152182
});
153183
</script>
184+
<script src="asset/SQLAdmin.js"></script>
154185
</body>
155186
</html>

0 commit comments

Comments
 (0)