|
1 | 1 | <?php |
2 | 2 | /** |
3 | 3 | * @author Gustavo Novaro |
4 | | - * @version 1.0.4 |
| 4 | + * @version 1.0.5 |
5 | 5 | */ |
6 | 6 | define('APP_NAME','SQL Server Admin'); |
7 | 7 | require('config.php'); |
8 | 8 | $connection = odbc_connect("Driver={SQL Server};Server=$server;Database=$database;", $user, $password); |
9 | 9 |
|
10 | 10 | $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 | +} |
11 | 45 | ?> |
12 | 46 | <!doctype html> |
13 | 47 | <html> |
|
47 | 81 | <form id="frm-query" method="post"> |
48 | 82 | <div class="form-group"> |
49 | 83 | <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>--> |
51 | 84 | <input type="hidden" name="query" id="query"> |
52 | 85 | </div> |
53 | 86 | <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> |
55 | 93 | </div> |
| 94 | + <input type="hidden" name="action" id="action" value="query"> |
56 | 95 | </form> |
57 | 96 | <?php |
58 | 97 | if(!empty($query)) |
|
63 | 102 | <?php |
64 | 103 | // Get Data From Result |
65 | 104 | if(!empty($result)): |
66 | | - $rows_count = odbc_num_rows ($result); |
| 105 | + $rows_count = odbc_num_rows($result); |
| 106 | + $columns = odbc_num_fields($result); |
67 | 107 | ?> |
68 | 108 | <div> |
69 | 109 | <strong>Rows count:</strong> <?php echo $rows_count;?> |
70 | 110 | </div> |
71 | 111 | <div class="table-responsive"> |
72 | 112 | <table class="table table-bordered table-striped"> |
| 113 | + <thead> |
| 114 | + <tr> |
73 | 115 | <?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 ); |
76 | 118 | ?> |
77 | | - |
| 119 | + <th><?php echo $key;?></th> |
78 | 120 | <?php |
79 | | - //Encabezado |
80 | | - if($i == 0): |
| 121 | + endfor; |
81 | 122 | ?> |
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> |
94 | 126 | <?php |
95 | | - endif |
| 127 | + while($row = odbc_fetch_array($result)): |
96 | 128 | ?> |
97 | | - |
98 | 129 | <tr> |
99 | 130 | <?php |
100 | | - foreach($data[$i] as $key => $val): |
| 131 | + foreach($row as $key => $val): |
101 | 132 | ?> |
102 | 133 | <td><?php echo $val;?></td> |
103 | 134 | <?php |
104 | 135 | endforeach; |
105 | 136 | ?> |
106 | 137 | </tr> |
107 | 138 | <?php |
108 | | - $i++; |
109 | 139 | endwhile; |
110 | 140 | ?> |
111 | 141 | </tbody> |
|
151 | 181 | } |
152 | 182 | }); |
153 | 183 | </script> |
| 184 | +<script src="asset/SQLAdmin.js"></script> |
154 | 185 | </body> |
155 | 186 | </html> |
0 commit comments