server_side.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  5. <link rel="shortcut icon" type="image/ico" href="http://www.datatables.net/media/images/favicon.ico" />
  6. <title>DataTables example</title>
  7. <style type="text/css" title="currentStyle">
  8. @import "../../media/css/demo_page.css";
  9. @import "../../media/css/demo_table.css";
  10. </style>
  11. <script type="text/javascript" language="javascript" src="../../media/js/jquery.js"></script>
  12. <script type="text/javascript" language="javascript" src="../../media/js/jquery.dataTables.js"></script>
  13. <script type="text/javascript" charset="utf-8">
  14. $(document).ready(function() {
  15. $('#example').dataTable( {
  16. "bProcessing": true,
  17. "bServerSide": true,
  18. "sAjaxSource": "scripts/server_processing.php"
  19. } );
  20. } );
  21. </script>
  22. </head>
  23. <body id="dt_example">
  24. <div id="container">
  25. <div class="full_width big">
  26. DataTables server-side processing example
  27. </div>
  28. <h1>Preamble</h1>
  29. <p>There are many ways to get your data into DataTables, and if you are working with seriously large databases, you might want to consider using the server-side options that DataTables provides. Basically all of the paging, filtering, sorting etc that DataTables does can be handed off to a server (or any other data source - Google Gears or Adobe Air for example!) and DataTables is just an events and display module.</p>
  30. <p>The example here shows a very simple display of the CSS data (used in all my other examples), but in this instance coming from the server on each draw. Filtering, multi-column sorting etc all work as you would expect.</p>
  31. <h1>Live example</h1>
  32. <div id="dynamic">
  33. <table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
  34. <thead>
  35. <tr>
  36. <th width="20%">Rendering engine</th>
  37. <th width="25%">Browser</th>
  38. <th width="25%">Platform(s)</th>
  39. <th width="15%">Engine version</th>
  40. <th width="15%">CSS grade</th>
  41. </tr>
  42. </thead>
  43. <tbody>
  44. <tr>
  45. <td colspan="5" class="dataTables_empty">Loading data from server</td>
  46. </tr>
  47. </tbody>
  48. <tfoot>
  49. <tr>
  50. <th>Rendering engine</th>
  51. <th>Browser</th>
  52. <th>Platform(s)</th>
  53. <th>Engine version</th>
  54. <th>CSS grade</th>
  55. </tr>
  56. </tfoot>
  57. </table>
  58. </div>
  59. <div class="spacer"></div>
  60. <h1>Initialisation code</h1>
  61. <pre class="brush: js;">$(document).ready(function() {
  62. $('#example').dataTable( {
  63. "bProcessing": true,
  64. "bServerSide": true,
  65. "sAjaxSource": "scripts/server_processing.php"
  66. } );
  67. } );</pre>
  68. <style type="text/css">
  69. @import "../examples_support/syntax/css/shCore.css";
  70. </style>
  71. <script type="text/javascript" language="javascript" src="../examples_support/syntax/js/shCore.js"></script>
  72. <h1>Server response</h1>
  73. <p>The code below shows the latest JSON data that has been returned from the server in response to the Ajax request made by DataTables. This will update as further requests are made.</p>
  74. <pre id="latest_xhr" class="brush: js;"></pre>
  75. <h1>Server side (PHP) code</h1>
  76. <pre>&lt;?php
  77. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  78. * Easy set variables
  79. */
  80. /* Array of database columns which should be read and sent back to DataTables. Use a space where
  81. * you want to insert a non-database field (for example a counter or static image)
  82. */
  83. $aColumns = array( 'engine', 'browser', 'platform', 'version', 'grade' );
  84. /* Indexed column (used for fast and accurate table cardinality) */
  85. $sIndexColumn = "id";
  86. /* DB table to use */
  87. $sTable = "ajax";
  88. /* Database connection information */
  89. $gaSql['user'] = "";
  90. $gaSql['password'] = "";
  91. $gaSql['db'] = "";
  92. $gaSql['server'] = "localhost";
  93. /* REMOVE THIS LINE (it just includes my SQL connection user/pass) */
  94. include( $_SERVER['DOCUMENT_ROOT']."/datatables/mysql.php" );
  95. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  96. * If you just want to use the basic configuration for DataTables with PHP server-side, there is
  97. * no need to edit below this line
  98. */
  99. /*
  100. * Local functions
  101. */
  102. function fatal_error ( $sErrorMessage = '' )
  103. {
  104. header( $_SERVER['SERVER_PROTOCOL'] .' 500 Internal Server Error' );
  105. die( $sErrorMessage );
  106. }
  107. /*
  108. * MySQL connection
  109. */
  110. if ( ! $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) )
  111. {
  112. fatal_error( 'Could not open connection to server' );
  113. }
  114. if ( ! mysql_select_db( $gaSql['db'], $gaSql['link'] ) )
  115. {
  116. fatal_error( 'Could not select database ' );
  117. }
  118. /*
  119. * Paging
  120. */
  121. $sLimit = "";
  122. if ( isset( $_GET['iDisplayStart'] ) &amp;&amp; $_GET['iDisplayLength'] != '-1' )
  123. {
  124. $sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
  125. mysql_real_escape_string( $_GET['iDisplayLength'] );
  126. }
  127. /*
  128. * Ordering
  129. */
  130. $sOrder = "";
  131. if ( isset( $_GET['iSortCol_0'] ) )
  132. {
  133. $sOrder = "ORDER BY ";
  134. for ( $i=0 ; $i&lt;intval( $_GET['iSortingCols'] ) ; $i++ )
  135. {
  136. if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
  137. {
  138. $sOrder .= "`".$aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."` ".
  139. mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
  140. }
  141. }
  142. $sOrder = substr_replace( $sOrder, "", -2 );
  143. if ( $sOrder == "ORDER BY" )
  144. {
  145. $sOrder = "";
  146. }
  147. }
  148. /*
  149. * Filtering
  150. * NOTE this does not match the built-in DataTables filtering which does it
  151. * word by word on any field. It's possible to do here, but concerned about efficiency
  152. * on very large tables, and MySQL's regex functionality is very limited
  153. */
  154. $sWhere = "";
  155. if ( isset($_GET['sSearch']) &amp;&amp; $_GET['sSearch'] != "" )
  156. {
  157. $sWhere = "WHERE (";
  158. for ( $i=0 ; $i&lt;count($aColumns) ; $i++ )
  159. {
  160. $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
  161. }
  162. $sWhere = substr_replace( $sWhere, "", -3 );
  163. $sWhere .= ')';
  164. }
  165. /* Individual column filtering */
  166. for ( $i=0 ; $i&lt;count($aColumns) ; $i++ )
  167. {
  168. if ( isset($_GET['bSearchable_'.$i]) &amp;&amp; $_GET['bSearchable_'.$i] == "true" &amp;&amp; $_GET['sSearch_'.$i] != '' )
  169. {
  170. if ( $sWhere == "" )
  171. {
  172. $sWhere = "WHERE ";
  173. }
  174. else
  175. {
  176. $sWhere .= " AND ";
  177. }
  178. $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
  179. }
  180. }
  181. /*
  182. * SQL queries
  183. * Get data to display
  184. */
  185. $sQuery = "
  186. SELECT SQL_CALC_FOUND_ROWS `".str_replace(" , ", " ", implode("`, `", $aColumns))."`
  187. FROM $sTable
  188. $sWhere
  189. $sOrder
  190. $sLimit
  191. ";
  192. $rResult = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
  193. /* Data set length after filtering */
  194. $sQuery = "
  195. SELECT FOUND_ROWS()
  196. ";
  197. $rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
  198. $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
  199. $iFilteredTotal = $aResultFilterTotal[0];
  200. /* Total data set length */
  201. $sQuery = "
  202. SELECT COUNT(`".$sIndexColumn."`)
  203. FROM $sTable
  204. ";
  205. $rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
  206. $aResultTotal = mysql_fetch_array($rResultTotal);
  207. $iTotal = $aResultTotal[0];
  208. /*
  209. * Output
  210. */
  211. $output = array(
  212. "sEcho" =&gt; intval($_GET['sEcho']),
  213. "iTotalRecords" =&gt; $iTotal,
  214. "iTotalDisplayRecords" =&gt; $iFilteredTotal,
  215. "aaData" =&gt; array()
  216. );
  217. while ( $aRow = mysql_fetch_array( $rResult ) )
  218. {
  219. $row = array();
  220. for ( $i=0 ; $i&lt;count($aColumns) ; $i++ )
  221. {
  222. if ( $aColumns[$i] == "version" )
  223. {
  224. /* Special output formatting for 'version' column */
  225. $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
  226. }
  227. else if ( $aColumns[$i] != ' ' )
  228. {
  229. /* General output */
  230. $row[] = $aRow[ $aColumns[$i] ];
  231. }
  232. }
  233. $output['aaData'][] = $row;
  234. }
  235. echo json_encode( $output );
  236. ?&gt;</pre>
  237. <h1>Other examples</h1>
  238. <div class="demo_links">
  239. <h2>Basic initialisation</h2>
  240. <ul>
  241. <li><a href="../basic_init/zero_config.html">Zero configuration</a></li>
  242. <li><a href="../basic_init/filter_only.html">Feature enablement</a></li>
  243. <li><a href="../basic_init/table_sorting.html">Sorting data</a></li>
  244. <li><a href="../basic_init/multi_col_sort.html">Multi-column sorting</a></li>
  245. <li><a href="../basic_init/multiple_tables.html">Multiple tables</a></li>
  246. <li><a href="../basic_init/hidden_columns.html">Hidden columns</a></li>
  247. <li><a href="../basic_init/complex_header.html">Complex headers - grouping with colspan</a></li>
  248. <li><a href="../basic_init/dom.html">DOM positioning</a></li>
  249. <li><a href="../basic_init/flexible_width.html">Flexible table width</a></li>
  250. <li><a href="../basic_init/state_save.html">State saving</a></li>
  251. <li><a href="../basic_init/alt_pagination.html">Alternative pagination styles</a></li>
  252. <li>Scrolling: <br>
  253. <a href="../basic_init/scroll_x.html">Horizontal</a> /
  254. <a href="../basic_init/scroll_y.html">Vertical</a> /
  255. <a href="../basic_init/scroll_xy.html">Both</a> /
  256. <a href="../basic_init/scroll_y_theme.html">Themed</a> /
  257. <a href="../basic_init/scroll_y_infinite.html">Infinite</a>
  258. </li>
  259. <li><a href="../basic_init/language.html">Change language information (internationalisation)</a></li>
  260. <li><a href="../basic_init/themes.html">ThemeRoller themes (Smoothness)</a></li>
  261. </ul>
  262. <h2>Advanced initialisation</h2>
  263. <ul>
  264. <li>Events: <br>
  265. <a href="../advanced_init/events_live.html">Live events</a> /
  266. <a href="../advanced_init/events_pre_init.html">Pre-init</a> /
  267. <a href="../advanced_init/events_post_init.html">Post-init</a>
  268. </li>
  269. <li><a href="../advanced_init/column_render.html">Column rendering</a></li>
  270. <li><a href="../advanced_init/html_sort.html">Sorting without HTML tags</a></li>
  271. <li><a href="../advanced_init/dom_multiple_elements.html">Multiple table controls (sDom)</a></li>
  272. <li><a href="../advanced_init/length_menu.html">Defining length menu options</a></li>
  273. <li><a href="../advanced_init/complex_header.html">Complex headers and hidden columns</a></li>
  274. <li><a href="../advanced_init/dom_toolbar.html">Custom toolbar (element) around table</a></li>
  275. <li><a href="../advanced_init/highlight.html">Row highlighting with CSS</a></li>
  276. <li><a href="../advanced_init/row_grouping.html">Row grouping</a></li>
  277. <li><a href="../advanced_init/row_callback.html">Row callback</a></li>
  278. <li><a href="../advanced_init/footer_callback.html">Footer callback</a></li>
  279. <li><a href="../advanced_init/sorting_control.html">Control sorting direction of columns</a></li>
  280. <li><a href="../advanced_init/language_file.html">Change language information from a file (internationalisation)</a></li>
  281. <li><a href="../advanced_init/defaults.html">Setting defaults</a></li>
  282. <li><a href="../advanced_init/localstorage.html">State saving with localStorage</a></li>
  283. <li><a href="../advanced_init/dt_events.html">Custom events</a></li>
  284. </ul>
  285. <h2>API</h2>
  286. <ul>
  287. <li><a href="../api/add_row.html">Dynamically add a new row</a></li>
  288. <li><a href="../api/multi_filter.html">Individual column filtering (using "input" elements)</a></li>
  289. <li><a href="../api/multi_filter_select.html">Individual column filtering (using "select" elements)</a></li>
  290. <li><a href="../api/highlight.html">Highlight rows and columns</a></li>
  291. <li><a href="../api/row_details.html">Show and hide details about a particular record</a></li>
  292. <li><a href="../api/select_row.html">User selectable rows (multiple rows)</a></li>
  293. <li><a href="../api/select_single_row.html">User selectable rows (single row) and delete rows</a></li>
  294. <li><a href="../api/editable.html">Editable rows (with jEditable)</a></li>
  295. <li><a href="../api/form.html">Submit form with elements in table</a></li>
  296. <li><a href="../api/counter_column.html">Index column (static number column)</a></li>
  297. <li><a href="../api/show_hide.html">Show and hide columns dynamically</a></li>
  298. <li><a href="../api/api_in_init.html">API function use in initialisation object (callback)</a></li>
  299. <li><a href="../api/tabs_and_scrolling.html">DataTables scrolling and tabs</a></li>
  300. <li><a href="../api/regex.html">Regular expression filtering</a></li>
  301. </ul>
  302. </div>
  303. <div class="demo_links">
  304. <h2>Data sources</h2>
  305. <ul>
  306. <li><a href="../data_sources/dom.html">DOM</a></li>
  307. <li><a href="../data_sources/js_array.html">Javascript array</a></li>
  308. <li><a href="../data_sources/ajax.html">Ajax source</a></li>
  309. <li><a href="../data_sources/server_side.html">Server side processing</a></li>
  310. </ul>
  311. <h2>Server-side processing</h2>
  312. <ul>
  313. <li><a href="../server_side/server_side.html">Obtain server-side data</a></li>
  314. <li><a href="../server_side/custom_vars.html">Add extra HTTP variables</a></li>
  315. <li><a href="../server_side/post.html">Use HTTP POST</a></li>
  316. <li><a href="../server_side/ids.html">Automatic addition of IDs and classes to rows</a></li>
  317. <li><a href="../server_side/object_data.html">Reading table data from objects</a></li>
  318. <li><a href="../server_side/row_details.html">Show and hide details about a particular record</a></li>
  319. <li><a href="../server_side/select_rows.html">User selectable rows (multiple rows)</a></li>
  320. <li><a href="../server_side/jsonp.html">JSONP for a cross domain data source</a></li>
  321. <li><a href="../server_side/editable.html">jEditable integration with DataTables</a></li>
  322. <li><a href="../server_side/defer_loading.html">Deferred loading of Ajax data</a></li>
  323. <li><a href="../server_side/pipeline.html">Pipelining data (reduce Ajax calls for paging)</a></li>
  324. </ul>
  325. <h2>Ajax data source</h2>
  326. <ul>
  327. <li><a href="../ajax/ajax.html">Ajax sourced data (array of arrays)</a></li>
  328. <li><a href="../ajax/objects.html">Ajax sourced data (array of objects)</a></li>
  329. <li><a href="../ajax/defer_render.html">Deferred DOM creation for extra speed</a></li>
  330. <li><a href="../ajax/null_data_source.html">Empty data source columns</a></li>
  331. <li><a href="../ajax/custom_data_property.html">Use a data source other than aaData (the default)</a></li>
  332. <li><a href="../ajax/objects_subarrays.html">Read column data from sub-arrays</a></li>
  333. <li><a href="../ajax/deep.html">Read column data from deeply nested properties</a></li>
  334. </ul>
  335. <h2>Plug-ins</h2>
  336. <ul>
  337. <li><a href="../plug-ins/plugin_api.html">Add custom API functions</a></li>
  338. <li><a href="../plug-ins/sorting_plugin.html">Sorting and automatic type detection</a></li>
  339. <li><a href="../plug-ins/sorting_sType.html">Sorting without automatic type detection</a></li>
  340. <li><a href="../plug-ins/paging_plugin.html">Custom pagination controls</a></li>
  341. <li><a href="../plug-ins/range_filtering.html">Range filtering / custom filtering</a></li>
  342. <li><a href="../plug-ins/dom_sort.html">Live DOM sorting</a></li>
  343. <li><a href="../plug-ins/html_sort.html">Automatic HTML type detection</a></li>
  344. </ul>
  345. </div>
  346. <div id="footer" class="clear" style="text-align:center;">
  347. <p>
  348. Please refer to the <a href="http://www.datatables.net/usage">DataTables documentation</a> for full information about its API properties and methods.<br>
  349. Additionally, there are a wide range of <a href="http://www.datatables.net/extras">extras</a> and <a href="http://www.datatables.net/plug-ins">plug-ins</a> which extend the capabilities of DataTables.
  350. </p>
  351. <span style="font-size:10px;">
  352. DataTables designed and created by <a href="http://www.sprymedia.co.uk">Allan Jardine</a> &copy; 2007-2011<br>
  353. DataTables is dual licensed under the <a href="http://www.datatables.net/license_gpl2">GPL v2 license</a> or a <a href="http://www.datatables.net/license_bsd">BSD (3-point) license</a>.
  354. </span>
  355. </div>
  356. </div>
  357. </body>
  358. </html>