在PHP中,如何在表格中显示数组内容

发布于 2024-10-30 05:04:03 字数 797 浏览 1 评论 0原文

如果我在 MySQL 客户端中进行选择,我将得到如下所示的输出:

mysql> select * FROM `group` LIMIT 2;
+----------+---------------------+-----------------+-------------+
| group_id | group_supergroup_id | group_deletable | group_label |
+----------+---------------------+-----------------+-------------+
|        1 |                   4 |               0 | defaut      |
|        8 |                   1 |               1 | dbdfg       |
+----------+---------------------+-----------------+-------------+

How can I conversion a PDO fetch (or fetchAll) array in a table like that ?

这是一个代码使用示例:

$prep = $pdo->query('SELECT * FROM `group` LIMIT 2;');
$arr = $prep->fetchAll(PDO::FETCH_ASSOC);
echo renderMySQLTable($arr);

If I do a select in the MySQL client, I will have output that looks like this:

mysql> select * FROM `group` LIMIT 2;
+----------+---------------------+-----------------+-------------+
| group_id | group_supergroup_id | group_deletable | group_label |
+----------+---------------------+-----------------+-------------+
|        1 |                   4 |               0 | defaut      |
|        8 |                   1 |               1 | dbdfg       |
+----------+---------------------+-----------------+-------------+

How can I convert a PDO fetch (or fetchAll) array in a table like that?

Here's a code usage example:

$prep = $pdo->query('SELECT * FROM `group` LIMIT 2;');
$arr = $prep->fetchAll(PDO::FETCH_ASSOC);
echo renderMySQLTable($arr);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱冒险 2024-11-06 05:04:03

类似于 mysql 客户端 输出的内容:

$data = array(
    array(
        'group_id'            => '1',
        'group_supergroup_id' => '4',
        'group_deletable'     => '0',
        'group_label'         => 'default',
    ),
    array(
        'group_id'            => '8',
        'group_supergroup_id' => '1',
        'group_deletable'     => '1',
        'group_label'         => 'dbdfg',
    ),
);

if ( empty($data) ) {
    echo "Empty set";
} else {
    // determine widths of titles
    $colWidths = array();
    foreach ( $data[0] as $title => $value ) {
        $colWidths[$title] = strlen($title);
    }
    // determine widths of columns
    foreach ( $data as $row ) {
        foreach ( $row as $title => $value ) {
            if ( is_null($value) ) {
                $value = 'NULL';
            }
            if ( $colWidths[$title] < strlen($value) ) {
                $colWidths[$title] = strlen($value);
            }
        }
    }
    // generate horizontal border
    $horizontalBorder = '+';
    foreach ( $colWidths as $title => $width ) {
        $horizontalBorder .= str_repeat('-', $width + 2) . "+";
    }
    $horizontalBorder .= "\n";
    // print titles
    echo $horizontalBorder;
    echo '|';
    foreach ( $data[0] as $title => $value ) {
        printf(" %-{$colWidths[$title]}s |", $title);
    }
    echo "\n";
    echo $horizontalBorder;
    // print contents
    foreach ( $data as $row ) {
        echo "|";
        foreach ( $row as $title => $value ) {
            if ( is_null($value) ) {
                $value = 'NULL';
            }
            printf(" %-{$colWidths[$title]}s |", $value);
        }
        echo "\n";
    }
    echo $horizontalBorder;
}

Something close to what mysql client outputs:

$data = array(
    array(
        'group_id'            => '1',
        'group_supergroup_id' => '4',
        'group_deletable'     => '0',
        'group_label'         => 'default',
    ),
    array(
        'group_id'            => '8',
        'group_supergroup_id' => '1',
        'group_deletable'     => '1',
        'group_label'         => 'dbdfg',
    ),
);

if ( empty($data) ) {
    echo "Empty set";
} else {
    // determine widths of titles
    $colWidths = array();
    foreach ( $data[0] as $title => $value ) {
        $colWidths[$title] = strlen($title);
    }
    // determine widths of columns
    foreach ( $data as $row ) {
        foreach ( $row as $title => $value ) {
            if ( is_null($value) ) {
                $value = 'NULL';
            }
            if ( $colWidths[$title] < strlen($value) ) {
                $colWidths[$title] = strlen($value);
            }
        }
    }
    // generate horizontal border
    $horizontalBorder = '+';
    foreach ( $colWidths as $title => $width ) {
        $horizontalBorder .= str_repeat('-', $width + 2) . "+";
    }
    $horizontalBorder .= "\n";
    // print titles
    echo $horizontalBorder;
    echo '|';
    foreach ( $data[0] as $title => $value ) {
        printf(" %-{$colWidths[$title]}s |", $title);
    }
    echo "\n";
    echo $horizontalBorder;
    // print contents
    foreach ( $data as $row ) {
        echo "|";
        foreach ( $row as $title => $value ) {
            if ( is_null($value) ) {
                $value = 'NULL';
            }
            printf(" %-{$colWidths[$title]}s |", $value);
        }
        echo "\n";
    }
    echo $horizontalBorder;
}
年华零落成诗 2024-11-06 05:04:03

不过,非 pdo 方法:)

function renderMySQLTable($query) {
  echo `mysql -H -e"$query"`;
}

对于数组来说,这并不难。几个循环,你就得到了你需要的。
从运行 print_r($arr) 开始查看它的结构

non-pdo aproach though :)

function renderMySQLTable($query) {
  echo `mysql -H -e"$query"`;
}

for the array it's not that hard. a few loops and you've got you need.
start from running print_r($arr) to see it's structure

抱着落日 2024-11-06 05:04:03

使用Table,并在css中提供一些样式。

echo "<table class='datasheet'>";
foreach($arr as $a)
{
    echo "<tr>";
        foreach($a as $v)
        {
                echo "<td>$v</td>";
        }
    echo "</tr>";
}
echo "</table>";

use Table, and give a bit of style in css.

echo "<table class='datasheet'>";
foreach($arr as $a)
{
    echo "<tr>";
        foreach($a as $v)
        {
                echo "<td>$v</td>";
        }
    echo "</tr>";
}
echo "</table>";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文