PHP Table Column name



>我有一个脚本,用于从用PHP编写的数据库中获取表。

我正在尝试为第一行中的每一列添加名称:

该准则的一部分是:

$rows = [];
foreach (range(1, 4) as $row) {
$rows[$row] = "";
}
$rows["Name"] = $order->user->aFirstName;
$rows["Last Name"] = $alumn->aLastName;
$rows["Age"] = $alumn->aAge;
$rows["Gender"] = $alumn->aGender;
$string = "";
foreach ($rows as $r) {
$string .= $r . "t";
}

我想要得到的是

1 | Name | Last Name | Age | Gender
2 | John | Des       | 45  | Male.

我现在得到的是第一行中的数据。

1 | John | Des       | 45  | Male.

有什么建议吗?谢谢

您可以使用https://www.php.net/manual/de/function.array-unshift.php 在$rows中创建新的第一个元素

$labels = ["Name" => "NameLabel", "Last Name" => "Last NameLabel" ...];
array_unshift($rows, $labels);

因此,$rows数组的第一个元素是标签。现在,当生成表格时,将在顶部显示标签。

您加载数组不正确。

我假设您不想从可用的元数据中获取列名,并且很乐意手动添加列名,如果不让我知道

$rows = [];
// add labels
$rows[] = ["Name", "Last Name", "Age", "Gender"];
#$rows[] = [$order->user->aFirstName, $alumn->aLastName, $alumn->aAge, $alumn->aGender];
// I dont have your data, so this is to simulate the above line
$rows[] = ['John', 'Des', 45, 'Male'];
$string = '';
foreach ($rows as $i => $row) {
$n = $i+1;
$string .= "$nt";
foreach ($row as $col){
$string .= $col . "t";
}
$string .= '<br>'. PHP_EOL;
}

print_r($string);

结果,如您所见,选项卡实际上不足以正确设置表格格式

1   Name    Last Name   Age Gender  <br>
2   John    Des 45  Male    <br>

最新更新