gpt4 book ai didi

php - 使用 cake php 动态显示表字段名称

转载 作者:行者123 更新时间:2023-11-29 03:04:50 26 4
gpt4 key购买 nike

我正在尝试显示表 people 中所有条目的列表。我有正确生成的条目列表作为 html 表,但我想在表的顶部添加一行,其中包含表字段名称。我可以手动执行此操作,但我认为必须有一种动态执行此操作的方法,以防我稍后添加或删除字段。

这里是 Controller 函数

public function peopleDisplay() {
$this->set('people', $this->people->find('all'));

}

字段名称是否已经在生成的数组中?如果是这样,我该如何引用它们?如果不是,我如何让它们变成这样?

这是目前的观点

    <table>

<tr>

***were I want the row of field names to go***

</tr>


<?php foreach($people as $people): ?>
<tr>
<td>
<?php echo $people['people']['id'] ?>
</td>
<td>
<?php echo $people['people']['firstName'] ?>
</td>
<td>
<?php echo $people['people']['secondName'] ?>
</td>
</tr>

<?php endforeach; ?>

</table>

最佳答案

列名是数组中的键。例如,'id'、'firstName' 和 'secondName' 是列名。

另一种方法是将列名称作为 Controller 中的单独数组获取,然后将它们输出到 View 中。

$people = $this->People->find('all');
$colNames = array_keys($this->People->getColumnTypes());
$this->set(compact('colNames', 'people'));

那么在你看来:

foreach($colNames as $col){ //Output column names }
foreach($people as $person){ //Output people }

关于php - 使用 cake php 动态显示表字段名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17391466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com