PHP 函数以 HTML 格式打印表格


PHP function to print table in HTML

使用 PHP 生成 HTML 代码来显示表(这是一个长度相等的数组数组)相当容易,例如下面的代码。但是我想知道:是否有一个PHP函数可以为您执行此操作?我在print table in php上搜索了谷歌,在table上搜索了PHP手册,但找不到这样的功能。

从 http://davidwalsh.name/html-mysql-php 代码打印表:

$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 />';
}

回答您的问题:不,没有.

但是,我确实发现这个想法很有趣,所以我编写了一个函数来做到这一点。但是,您的示例使用 MySQL(),它已弃用且不安全。所以我将使用PDO类.

请记住,此功能仅用于打印数据库表。它对注入不安全,因此永远不应该与查询中的用户输入一起使用!

<?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);
?>

PDO类本身:

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();
    }
}

没有这样的内置函数。你创建了自己的,这就是它的工作原理。总是有 var_dump(),但普通访问者无法读取。