PHP - 二维数组 table 格式化
PHP - 2D Array table formatting
我有一个带有密钥的二维电影数组,这些密钥是通过 MySQL Db 通过 phpMyAdmin 生成的。
(剧情从复制粘贴中截断)
$films = array(
array('title' => 'Batman Begins','yr' => '2005','genre' => 'Action, Adventure','plot' => 'After training with his mentor, Batman begins his w...')
array('title' => 'Ted 2','yr' => '2015','genre' => 'Comedy','plot' => 'Newlywed couple Ted and Tami-Lynn want to have a baby, but in order to...')
array('title' => 'Ted','yr' => '2012','genre' => 'Comedy, Fantasy','plot' => 'As the result of a childhood wish, John Bennett\'s teddy bear, ...')
array('title' => 'Interstellar','yr' => '2014','genre' => 'Adventure, Sci-Fi','plot' => 'A team of explorers travel through a wormhole in spa...')
);
我还有一个 foreach 循环,用于遍历二维数组,return 结果为 table:
$out = "";
$out .= "<table>";
foreach($films as $key => $element){
$out .= "<tr>";
foreach($element as $subk => $subel){
$out .= "<td>$subel</td>";
}
$out .= "</tr>";
}
$out .="<table>";
echo $out;
从我在网页上看到的结果来看,它是这样显示的:
Batman Begins 2005 动作、冒险 训练后 ...
如何才能将键显示为列 header?我试过在主循环中制作另一个 foreach 循环,但是 returned header 是这样的:titleyrgenreplottitleyrgenreplot
等
我如何才能将其正确格式化为 table,从而出现 header?
还有一个小而快速的问题:不是将数据库从 phpMyAdmin 导出为 PHP 数组,而是如何在 PHP/webpage 中进行更改时更新MySQL 数据库 table?
以下是您获得 headers 的方法:
$headers="<thead><tr>";
foreach($films as $key => $element){
$headers.= "<th>$key</th>";
}
$headers.= "</tr></thead>";
所以您的代码现在应该如下所示:
$out = "";
$out .= "<tbody>";
$headers="<table><thead><tr>";
foreach($films as $key => $element){
$headers.= "<th>$key</th>";
$out .= "<tr>";
foreach($element as $subk => $subel){
$out .= "<td>$subel</td>";
}
$out .= "</tr>";
}
$out .="</tbody><table>";
$headers.= "</tr></thead>";
echo $headers;
echo $out;
为了将字段显示为 header,我们只需要遍历第一个子数组的键并添加另一行 (<tr>
) table header(<th>
) before 循环遍历整个结果。一般我们只显示一次headers。
$out = "";
$out .= "<table>";
$out .= "<tr>";
foreach($films[0] as $key => $value)
{
$out .= "<th>".$key."</th>";
}
$out .= "</tr>";
foreach($films as $key => $element){
$out .= "<tr>";
foreach($element as $subk => $subel){
$out .= "<td>$subel</td>";
}
$out .= "</tr>";
}
$out .="<table>";
echo $out;
我有一个带有密钥的二维电影数组,这些密钥是通过 MySQL Db 通过 phpMyAdmin 生成的。
(剧情从复制粘贴中截断)
$films = array(
array('title' => 'Batman Begins','yr' => '2005','genre' => 'Action, Adventure','plot' => 'After training with his mentor, Batman begins his w...')
array('title' => 'Ted 2','yr' => '2015','genre' => 'Comedy','plot' => 'Newlywed couple Ted and Tami-Lynn want to have a baby, but in order to...')
array('title' => 'Ted','yr' => '2012','genre' => 'Comedy, Fantasy','plot' => 'As the result of a childhood wish, John Bennett\'s teddy bear, ...')
array('title' => 'Interstellar','yr' => '2014','genre' => 'Adventure, Sci-Fi','plot' => 'A team of explorers travel through a wormhole in spa...')
);
我还有一个 foreach 循环,用于遍历二维数组,return 结果为 table:
$out = "";
$out .= "<table>";
foreach($films as $key => $element){
$out .= "<tr>";
foreach($element as $subk => $subel){
$out .= "<td>$subel</td>";
}
$out .= "</tr>";
}
$out .="<table>";
echo $out;
从我在网页上看到的结果来看,它是这样显示的:
Batman Begins 2005 动作、冒险 训练后 ...
如何才能将键显示为列 header?我试过在主循环中制作另一个 foreach 循环,但是 returned header 是这样的:titleyrgenreplottitleyrgenreplot
等
我如何才能将其正确格式化为 table,从而出现 header?
还有一个小而快速的问题:不是将数据库从 phpMyAdmin 导出为 PHP 数组,而是如何在 PHP/webpage 中进行更改时更新MySQL 数据库 table?
以下是您获得 headers 的方法:
$headers="<thead><tr>";
foreach($films as $key => $element){
$headers.= "<th>$key</th>";
}
$headers.= "</tr></thead>";
所以您的代码现在应该如下所示:
$out = "";
$out .= "<tbody>";
$headers="<table><thead><tr>";
foreach($films as $key => $element){
$headers.= "<th>$key</th>";
$out .= "<tr>";
foreach($element as $subk => $subel){
$out .= "<td>$subel</td>";
}
$out .= "</tr>";
}
$out .="</tbody><table>";
$headers.= "</tr></thead>";
echo $headers;
echo $out;
为了将字段显示为 header,我们只需要遍历第一个子数组的键并添加另一行 (<tr>
) table header(<th>
) before 循环遍历整个结果。一般我们只显示一次headers。
$out = "";
$out .= "<table>";
$out .= "<tr>";
foreach($films[0] as $key => $value)
{
$out .= "<th>".$key."</th>";
}
$out .= "</tr>";
foreach($films as $key => $element){
$out .= "<tr>";
foreach($element as $subk => $subel){
$out .= "<td>$subel</td>";
}
$out .= "</tr>";
}
$out .="<table>";
echo $out;