Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: MJ VC
Added: Jan 30, 2019 1:47 AM
Modified: Jan 30, 2019 1:54 AM
Views: 2
  1. //returns datatable from database result set with params.:database query, table id, css class, and dabase class
  2.   function render_datatable($data_query, $id, $class, $database) {
  3.     $pdo = $database::connect();
  4.     $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  5.     $query = $pdo->prepare($data_query);
  6.     $query->execute();
  7.     $tableheader = false;
  8.    
  9.     echo '<table id="'.$id.'" class="'.$class.'" style = "margin: 0 !important; width:100% !important" >';
  10.     foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) {
  11.       if($tableheader === false) {
  12.         render_tablehead($row);  
  13.        // render_tablefoot($row);
  14.         $tableheader = true;
  15.       }
  16.       render_tablebody($row);
  17.     }  
  18.     echo "</tbody></table>";  
  19.    
  20.     $database::disconnect();
  21.   }
  22.   //returns table header
  23.   function render_tablehead($row){
  24.     echo '<thead><tr>';  
  25.     foreach($row as $key => $val) {
  26.       echo "<th>$key</th>";  
  27.     }
  28.     echo '</tr></thead>';
  29.   }
  30.   //returns table body
  31.   function render_tablebody($row){
  32.     echo "<tr>";    
  33.     foreach($row as $value) {  echo "<td>$value</td>"; }
  34.     echo "</tr>";
  35.   }
  36.   //returns table foot
  37.   function render_tablefoot($row){
  38.     echo '<tfoot ><tr>';
  39.     foreach($row as $key => $val) {
  40.       echo "<th>$key</th>";  
  41.     }
  42.     echo '</tr></tfoot>';
  43.     echo '<tbody>';
  44.   }