【发布时间】:2015-10-25 12:42:15
【问题描述】:
我有一个二维电影数组,其中包含通过 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 循环,循环遍历 2D 数组并将结果作为表格返回:
$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;
从我在网页上看到的结果来看,它是这样显示的:
蝙蝠侠开始 2005 动作,冒险 训练后 ...
如何将键显示为列标题?我尝试在主循环中创建另一个 foreach 循环,但返回的标题如下:titleyrgenreplottitleyrgenreplot 等。
我怎样才能在表格中正确格式化它,以便出现标题?
也只是一个小而快速的问题:不是将数据库从 phpMyAdmin 导出为 PHP 数组,而是在 MySQL 数据库表中进行更改时如何更新 PHP/网页?
【问题讨论】:
标签: php html arrays html-table