Advertisement
TonyAR

dynamic tables using php

Mar 28th, 2023 (edited)
821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.54 KB | Source Code | 0 0
  1. <?php
  2. $table_name = rtrim($table_name);
  3. // get count of table rows
  4. $result = $conn->query("select count(*) from $table_name");
  5. $numrows = $result->fetch_array()[0] ?? '';
  6. mysqli_free_result($result);
  7. $numrows = htmlentities($numrows,ENT_QUOTES,'UTF-8');
  8. $headers = $conn->query("SHOW COLUMNS FROM $table_name");
  9. $tableArr = array();
  10. while($row1 = mysqli_fetch_assoc($headers)) {
  11.     $tableArr[] = $row1['Field'];
  12. }
  13. // shift the array to remove the 'id' column
  14. array_shift($tableArr);
  15. // convert the array in a comma delimited list
  16. $table_data = implode('`,`', $tableArr);
  17. // place back ticks around query string (allows for spaces in field names)
  18. $table_data = "`{$table_data}`";
  19. // build the table
  20. build_header($conn,$numrows,$table_name,$table_data);
  21.  
  22. function build_header($conn,$numrows,$table_name,$table_data) {
  23.         if ($numrows >= "1") {
  24.                 $query_header = ("SELECT $table_data FROM $table_name LIMIT 1");
  25.                 $header=mysqli_query($conn,$query_header);
  26.                 // open table
  27.                 echo "<!-- begin table construct -->\n";
  28.                 echo "<table class=\"center\">\n";
  29.                         foreach( $header as $key => $value ) {
  30.                         if( is_array($value) ) {
  31.                         echo "<tr>\n";
  32.                         // populate the column headers
  33.                 foreach($value as $key => $column) {
  34.                         echo "<th>".$key."</th>\n";
  35.                         }
  36.                         echo "</tr>\n";
  37.                         } else {
  38.                         echo "<th>No header information available</th>\n";
  39.                         }
  40.                 }
  41.  
  42.                 // table data
  43.                 $query_header = ("SELECT $table_data FROM $table_name");
  44.                 $header=mysqli_query($conn,$query_header);
  45.                 foreach( $header as $key => $value ) {
  46.                         if( is_array($value) ) {
  47.                         echo "<tr>\n";
  48.                 foreach($value as $key => $column) {
  49.                         echo "<td>{$column}</td>\n";
  50.                         }
  51.                         echo "</tr>\n";
  52.                         } else {
  53.                         echo "<td>{$value}</td>\n";
  54.                         }
  55.                 }
  56.                 // close table
  57.                 echo "</table>\n";
  58.                 echo "<!-- end table construct -->\n";
  59.                 mysqli_free_result($header);
  60.                 } else {
  61.                 echo "No entries found in table: $table_name\n";
  62.                 }
  63. }
  64. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement