Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $table_name = rtrim($table_name);
- // get count of table rows
- $result = $conn->query("select count(*) from $table_name");
- $numrows = $result->fetch_array()[0] ?? '';
- mysqli_free_result($result);
- $numrows = htmlentities($numrows,ENT_QUOTES,'UTF-8');
- $headers = $conn->query("SHOW COLUMNS FROM $table_name");
- $tableArr = array();
- while($row1 = mysqli_fetch_assoc($headers)) {
- $tableArr[] = $row1['Field'];
- }
- // shift the array to remove the 'id' column
- array_shift($tableArr);
- // convert the array in a comma delimited list
- $table_data = implode('`,`', $tableArr);
- // place back ticks around query string (allows for spaces in field names)
- $table_data = "`{$table_data}`";
- // build the table
- build_header($conn,$numrows,$table_name,$table_data);
- function build_header($conn,$numrows,$table_name,$table_data) {
- if ($numrows >= "1") {
- $query_header = ("SELECT $table_data FROM $table_name LIMIT 1");
- $header=mysqli_query($conn,$query_header);
- // open table
- echo "<!-- begin table construct -->\n";
- echo "<table class=\"center\">\n";
- foreach( $header as $key => $value ) {
- if( is_array($value) ) {
- echo "<tr>\n";
- // populate the column headers
- foreach($value as $key => $column) {
- echo "<th>".$key."</th>\n";
- }
- echo "</tr>\n";
- } else {
- echo "<th>No header information available</th>\n";
- }
- }
- // table data
- $query_header = ("SELECT $table_data FROM $table_name");
- $header=mysqli_query($conn,$query_header);
- foreach( $header as $key => $value ) {
- if( is_array($value) ) {
- echo "<tr>\n";
- foreach($value as $key => $column) {
- echo "<td>{$column}</td>\n";
- }
- echo "</tr>\n";
- } else {
- echo "<td>{$value}</td>\n";
- }
- }
- // close table
- echo "</table>\n";
- echo "<!-- end table construct -->\n";
- mysqli_free_result($header);
- } else {
- echo "No entries found in table: $table_name\n";
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement