1 |
<?php |
2 |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
3 |
* Easy set variables |
4 |
*/ |
5 |
|
6 |
/* Array of database columns which should be read and sent back to DataTables. Use a space where |
7 |
* you want to insert a non-database field (for example a counter or static image) |
8 |
*/ |
9 |
$aColumns = array( 'name', 'phone', 'email', 'city', 'zip' ); |
10 |
|
11 |
/* Indexed column (used for fast and accurate table cardinality) */ |
12 |
$sIndexColumn = "id"; |
13 |
|
14 |
/* DB table to use */ |
15 |
$sTable = "testData"; |
16 |
|
17 |
/* Database connection information */ |
18 |
$gaSql['user'] = ""; |
19 |
$gaSql['password'] = ""; |
20 |
$gaSql['db'] = ""; |
21 |
$gaSql['server'] = "localhost"; |
22 |
|
23 |
/* REMOVE THIS LINE (it just includes my SQL connection user/pass) */ |
24 |
include( $_SERVER['DOCUMENT_ROOT']."/datatables/mysql.php" ); |
25 |
|
26 |
|
27 |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
28 |
* If you just want to use the basic configuration for DataTables with PHP server-side, there is |
29 |
* no need to edit below this line |
30 |
*/ |
31 |
|
32 |
/* |
33 |
* MySQL connection |
34 |
*/ |
35 |
$gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or |
36 |
die( 'Could not open connection to server' ); |
37 |
|
38 |
mysql_select_db( $gaSql['db'], $gaSql['link'] ) or |
39 |
die( 'Could not select database '. $gaSql['db'] ); |
40 |
|
41 |
|
42 |
/* |
43 |
* Paging |
44 |
*/ |
45 |
$sLimit = ""; |
46 |
if ( isset( $_GET['iStart'] ) && isset( $_GET['iLength'] ) ) |
47 |
{ |
48 |
$sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iStart'] ).", ". |
49 |
mysql_real_escape_string( $_GET['iLength'] ); |
50 |
} |
51 |
else |
52 |
{ |
53 |
echo '{ "aaData": [] }'; |
54 |
exit(); |
55 |
} |
56 |
|
57 |
/* |
58 |
* SQL queries |
59 |
* Get data to display |
60 |
*/ |
61 |
$sQuery = " |
62 |
SELECT ".str_replace(" , ", " ", implode(", ", $aColumns))." |
63 |
FROM $sTable |
64 |
ORDER BY name ASC |
65 |
$sLimit |
66 |
"; |
67 |
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error()); |
68 |
|
69 |
/* |
70 |
* Output |
71 |
*/ |
72 |
$sOutput = '{'; |
73 |
$sOutput .= '"aaData": [ '; |
74 |
while ( $aRow = mysql_fetch_array( $rResult ) ) |
75 |
{ |
76 |
$sOutput .= "["; |
77 |
for ( $i=0 ; $i<count($aColumns) ; $i++ ) |
78 |
{ |
79 |
/* General output */ |
80 |
$sOutput .= '"'.str_replace('"', '\"', $aRow[ $aColumns[$i] ]).'",'; |
81 |
} |
82 |
|
83 |
/* |
84 |
* Optional Configuration: |
85 |
* If you need to add any extra columns (add/edit/delete etc) to the table, that aren't in the |
86 |
* database - you can do it here |
87 |
*/ |
88 |
|
89 |
|
90 |
$sOutput = substr_replace( $sOutput, "", -1 ); |
91 |
$sOutput .= "],"; |
92 |
} |
93 |
$sOutput = substr_replace( $sOutput, "", -1 ); |
94 |
$sOutput .= '] }'; |
95 |
|
96 |
echo $sOutput; |
97 |
?> |