嗨,我认为这只是一个语法问题,但我可能正在做一些PHP不是为了做.
我正在尝试绘制一个HTML表,使用数组来填充表.例如标题.
其中$ headers是我尝试过的标头数组:
$html_table = '
<table border="1" cellspacing="0" cellpadding="2">
<tr>
foreach($headers as $header)
{
echo "<th> $header </th>";
}
</tr>
';
Run Code Online (Sandbox Code Playgroud)
想要生成:
<table border="1" cellspacing="0" cellpadding="2">
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
<th>Heading 3</th>
...
<th>Heading 99</th>
</tr>
Run Code Online (Sandbox Code Playgroud)
只需稍后询问$ html_table
我只想说当前我得到一个带有"$ header"的单头栏,因为循环不在变量等式中运行.
我将HTML存储为这样的变量的原因是因为我想连接(使用?)它与其他生成的html,即
$html_table .= '</table>';
Run Code Online (Sandbox Code Playgroud)
稍后(当然中间的实际位更复杂,与从数据库中检索数据以填充表格有关.
我哪里错了?谢谢
把foreach从引号中拿出来
$html_table = '
<table border="1" cellspacing="0" cellpadding="2">
<tr>';
foreach($headers as $header){
$html_table .= "<th> $header </th>";
}
$html_table .='
</tr>
';
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3829 次 |
| 最近记录: |