Reputation: 6858
It is fairly easy to use PHP to generate HTML code to display a table (which is an array of arrays of equal length), for instance the code below. I was wondering however: is there a PHP function that does this for you? I searched Google on print table in php
and searched the PHP manual on table
, but could not find such a function.
Code from http://davidwalsh.name/html-mysql-php to print a table:
$result2 = mysql_query('SHOW COLUMNS FROM '.$table) or die('cannot show columns from '.$table);
if(mysql_num_rows($result2)) {
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
echo '<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default<th>Extra</th></tr>';
while($row2 = mysql_fetch_row($result2)) {
echo '<tr>';
foreach($row2 as $key=>$value) {
echo '<td>',$value,'</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
Upvotes: 1
Views: 26606
Reputation: 8773
Answer to your question: No, there isn't.
However, I did find the idea interesting so I've written a function that does just that. However, your example uses MySQL() which is deprecated and insecure. So I'll be using a PDO class instead.
Please keep in mind that this function is only usefull for printing out a database table. It's NOT secure against injection and therefor should NEVER be used with user input inside the query!
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include "pdo.class.php";
//Database data
define("DB_HOST", "localhost");
define("DB_USER", "");
define("DB_PASS", "");
define("DB_NAME", "");
function printTable($tbl_name, $db_query){
//New PDO object
$pdo = new Database();
//Get column names
$pdo->query("DESCRIBE ". $tbl_name);
$col_names = $pdo->column();
//Get number of columns
$col_cnt = count($col_names);
//Setup table - user css class db-table for design
echo "<table class='db-table'>";
echo "<tr colspan='". $col_cnt ."'>". $tbl_name ."</tr>";
echo "<tr>";
//Give each table column same name is db column name
for($i=0;$i<$col_cnt;$i++){
echo "<td>". $col_names[$i] ."</td>";
}
echo "</tr>";
//Get db table data
$pdo->query($db_query);
$results = $pdo->resultset();
$res_cnt = count($results);
//Print out db table data
for($i=0;$i<$res_cnt;$i++){
echo "<tr>";
for($y=0;$y<$col_cnt;$y++){
echo "<td>". $results[$i][$col_names[$y]] ."</td>";
}
echo "</tr>";
}
}
//Query
$sqlq = "SELECT * FROM tablename";
//Useage: printTable("tablename","query");
printTable("tablename",$sqlq);
?>
The PDO class itself:
Class Database{
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $dbh;
private $error;
private $stmt;
public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function column(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_COLUMN);
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function rowCount(){
return $this->stmt->rowCount();
}
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
public function beginTransaction(){
return $this->dbh->beginTransaction();
}
public function endTransaction(){
return $this->dbh->commit();
}
public function cancelTransaction(){
return $this->dbh->rollBack();
}
public function debugDumpParams(){
return $this->stmt->debugDumpParams();
}
}
Upvotes: 1
Reputation: 160
There is no such built-in function. You created your own, that's how it works. There is always var_dump(), but that is not readable to your average visitor.
Upvotes: -1