ADODB5 (PHP):动态构建表列

发布于 2024-11-01 08:19:02 字数 836 浏览 0 评论 0原文

我想知道如何在adodb5中动态生成表列。

这是我当前的代码:

<?php
$sql = "SELECT id FROM customers";
$query = $db->Execute($sql);
$rows = $query->GetRows();
$fields = $query->fields;

foreach($rows as $row) {
    print_r($row). '<br />';
}
?>

我收到的输出是:

Array
(
    [id] => 280
)
Array
(
    [id] => 1024
)
Array
(
    [id] => 474
)
Array
(
    [id] => 476
)
Array
(
    [id] => 564
)
Array
(
    [id] => 569
)
Array
(
    [id] => 594
)
Array
(
    [id] => 385
)
Array
(
    [id] => 304
)
Array
(
    [id] => 700
)
Array
(
    [id] => 285
)
Array
(
    [id] => 205
)
Array
(
    [id] => 536
)
Array
(
    [id] => 140
)

我实际上只是希望它获取查询中的所有列并构建表头。所以基本上,我将有一个表,其中一列下包含所有“ID”。我实际上想要标记列,所以万一我有多个列,例如 ID、名称、日期、评论;它会动态地知道如何为每列制作标题。这是可以帮助我的人吗?

I'm wanting to know how to dynamically generate the table columns in adodb5.

Here's my current code:

<?php
$sql = "SELECT id FROM customers";
$query = $db->Execute($sql);
$rows = $query->GetRows();
$fields = $query->fields;

foreach($rows as $row) {
    print_r($row). '<br />';
}
?>

The output I recieve is:

Array
(
    [id] => 280
)
Array
(
    [id] => 1024
)
Array
(
    [id] => 474
)
Array
(
    [id] => 476
)
Array
(
    [id] => 564
)
Array
(
    [id] => 569
)
Array
(
    [id] => 594
)
Array
(
    [id] => 385
)
Array
(
    [id] => 304
)
Array
(
    [id] => 700
)
Array
(
    [id] => 285
)
Array
(
    [id] => 205
)
Array
(
    [id] => 536
)
Array
(
    [id] => 140
)

I'm literally just wanting it to grab all the columns in the query and build the table headers. So basically, I will have a table that has all the "ID"'s under one columns. I actually want the columns labeled, so in case I have more than one columns, for example, ID, Name, Date, Comment; it would dynamically know how to make the headers for each columns. Is this someone that can help me with?

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

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

发布评论

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

评论(1

玩套路吗 2024-11-08 08:19:02

您首先需要修改获取数据行的查询:

select * from customers

或者如果您只想要某些列:

select id, name from customers

然后您可以迭代行并看到您有一个对象或关联数组(我忘记了哪个),它允许您布局您的桌子。对于标头,您已经拥有 $fields,因此只需迭代即可形成标头:

foreach ($fields as $name) {
    echo $name;
}

希望这会有所帮助。

You first need to modify your query that gets the data rows:

select * from customers

or if you only want certain columns:

select id, name from customers

you can then iterate through the rows and see you have an object or an associative array (I forget which) that allows you to lay out your table. For the headers you already have the $fields so just iterate to form the header:

foreach ($fields as $name) {
    echo $name;
}

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文