Generate PHP Code to Display MySQL Table

This chunk of PHP code automatically generates the PHP code to display a MySQL table. All you have to do is change the table name and enter your database settings. This comes in handy so that you can format each column appropriately depending on the data type (dates, emails, etc.) rather than just having to display the data exactly as it's stored in the database.

Here's the code that generates the code. Confused? Just take a look (at both sets of code), I think you'll see what we mean…

1.   <?php
2.   
3.   $tablename = "animals" ;
4.   
5.   echo "<pre>\n" ;
6.   echo htmlspecialchars("echo \"<table>\\n\" ;\n") ;
7.   echo htmlspecialchars("echo \"  <thead>\\n\" ;\n") ;
8.   echo htmlspecialchars("echo \"  <tr>\\n\" ;\n") ;
9.   $query = "SHOW COLUMNS FROM `".$tablename."`" ;
10.  $result = dbQuery($query) ;
11.  while ($row = dbGetRow($result,null,false,false)) {
12.  	echo htmlspecialchars("echo \"	<td>".$row['0']."</td>\\n\" ;\n") ;
13.  }
14.  echo htmlspecialchars("echo \"  </tr>\\n\" ;\n") ;
15.  echo htmlspecialchars("echo \"  </thead>\\n\" ;\n") ;
16.  echo htmlspecialchars("echo \"  <tbody>\\n\" ;\n") ;
17.  echo "\$query = \"SELECT * FROM `".$tablename."`\" ;\n" ;
18.  echo "\$result = dbQuery(\$query,\$link) ;\n" ;
19.  echo "while (\$row = dbGetRow(\$result)) {\n" ;
20.  echo htmlspecialchars("	echo \"  <tr>\\n\" ;\n") ;
21.  $result = dbQuery($query) ;
22.  while ($row = dbGetRow($result,null,false,false)) {
23.  	echo htmlspecialchars("	echo \"	<td>\".\$row['".$row['0']."'].\"</td>\\n\" ;\n") ;
24.  }
25.  echo htmlspecialchars("	echo \"  </tr>\\n\" ;\n") ;
26.  echo "	}\n" ;
27.  echo htmlspecialchars("echo \"  </tbody>\\n\" ;\n") ;
28.  echo htmlspecialchars("echo \"</table>\\n\" ;\n") ;
29.  echo "</pre>\n\n" ;
30.  
31.  
32.  ?>

Now, if you were to take that code and run it, this is what you'd see:

1.   
2.   echo "<table>\n" ;
3.   echo "  <thead>\n" ;
4.   echo "  <tr>\n" ;
5.   echo "	<td>id</td>\n" ;
6.   echo "	<td>name</td>\n" ;
7.   echo "	<td>animal_type</td>\n" ;
8.   echo "	<td>color</td>\n" ;
9.   echo "  </tr>\n" ;
10.  echo "  </thead>\n" ;
11.  echo "  <tbody>\n" ;
12.  $query = "SELECT * FROM `animals`" ;
13.  $result = dbQuery($query,$link) ;
14.  while ($row = dbGetRow($result)) {
15.  	echo "  <tr>\n" ;
16.  	echo "	<td>".$row['id']."</td>\n" ;
17.  	echo "	<td>".$row['name']."</td>\n" ;
18.  	echo "	<td>".$row['animal_type']."</td>\n" ;
19.  	echo "	<td>".$row['color']."</td>\n" ;
20.  	echo "  </tr>\n" ;
21.  	}
22.  echo "  </tbody>\n" ;
23.  echo "</table>\n" ;
24.  

Plugging that second set of code into a PHP file would display a nice, neat little table for you.

Note that the above code (both sets) uses some custom database functions that you'll need to define before using it.


Comments

Loading…

This post was first published on October 10th, 2009 and last updated on June 28th, 2014 by Robert James Reese in the following categories: MySQL and PHP. Before using any of the code or other content in this post, you must read and agree to our terms of use.