Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. <?php
  2. /* Database connection start */
  3. $servername = "localhost";
  4. $username = "root";
  5. $password = "";
  6. $dbname = "db_name";
  7.  
  8. $conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
  9.  
  10. /* check connection */
  11. if (mysqli_connect_errno()) {
  12. printf("Connect failed: %sn", mysqli_connect_error());
  13. exit();
  14. }
  15.  
  16. ?>
  17.  
  18. <?php
  19. //include connection file
  20. include_once("connection.php");
  21.  
  22. // initilize all variable
  23. $params = $columns = $totalRecords = $data = array();
  24.  
  25. $params = $_REQUEST;
  26.  
  27. //define index of column
  28. $columns = array(
  29. 0 =>'pid',
  30. 1 =>'product_name',
  31. 2 => 'product_price',
  32. 3 => 'added_date'
  33. );
  34.  
  35. $where = $sqlTot = $sqlRec = "";
  36.  
  37. // getting total number records without any search
  38. $sql = "SELECT pid,product_name,product_price,added_datek FROM `products` ";
  39. $sqlTot .= $sql;
  40. $sqlRec .= $sql;
  41.  
  42.  
  43. $sqlRec .= " ORDER BY added_date";
  44.  
  45. $queryTot = mysqli_query($conn, $sqlTot) or die("database error:". mysqli_error($conn));
  46.  
  47.  
  48. $totalRecords = mysqli_num_rows($queryTot);
  49.  
  50. $queryRecords = mysqli_query($conn, $sqlRec) or die("error to fetch employees data");
  51.  
  52. //iterate on results row and create new index array of data
  53. while( $row = mysqli_fetch_row($queryRecords) ) {
  54. $data[] = $row;
  55. }
  56.  
  57. $json_data = array(
  58. "draw" => 1,
  59. "recordsTotal" => intval( $totalRecords ),
  60. "recordsFiltered" => intval($totalRecords),
  61. "data" => $data // total data array
  62. );
  63.  
  64. echo json_encode($json_data); // send data as json format
  65. ?>
  66.  
  67. <html>
  68. <head>
  69.  
  70. <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/r/dt/jq-2.1.4,jszip-2.5.0,pdfmake-0.1.18,dt-1.10.9,af-2.0.0,b-1.0.3,b-colvis-1.0.3,b-html5-1.0.3,b-print-1.0.3,se-1.0.1/datatables.min.css"/>
  71.  
  72. <script type="text/javascript" src="https://cdn.datatables.net/r/dt/jq-2.1.4,jszip-2.5.0,pdfmake-0.1.18,dt-1.10.9,af-2.0.0,b-1.0.3,b-colvis-1.0.3,b-html5-1.0.3,b-print-1.0.3,se-1.0.1/datatables.min.js"></script>
  73.  
  74.  
  75. <div class="container" style="padding:20px;20px;">
  76. <div class="">
  77. <h1>Data Table with Export features Using PHP server-side</h1>
  78. <div class="">
  79. <table id="employee_grid" class="display" width="100%" cellspacing="0">
  80. <thead>
  81. <tr>
  82. <th>PID</th>
  83. <th>Product Name</th>
  84. <th>Price</th>
  85. <th>Purchase Date</th>
  86. </tr>
  87. </thead>
  88.  
  89. <tfoot>
  90. <tr>
  91. <th>PID</th>
  92. <th>Product Name</th>
  93. <th>Price</th>
  94. <th>Purchase Date</th>
  95.  
  96. </tr>
  97. </tfoot>
  98. </table>
  99. </div>
  100. </div>
  101.  
  102. </div>
  103.  
  104. <script type="text/javascript">
  105. $( document ).ready(function() {
  106. $('#employee_grid').DataTable({
  107. "processing": true,
  108. "sAjaxSource":"response.php",
  109. "dom": 'lBfrtip',
  110. "buttons": [
  111. {
  112. extend: 'collection',
  113. text: 'Export',
  114. buttons: [
  115. 'copy',
  116. 'excel',
  117. 'csv',
  118. 'pdf',
  119. 'print'
  120. ]
  121. }
  122. ]
  123. });
  124. });
  125. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement