我有以下代码,但它不像我想要的那样工作。我只是想表头显示一次,但现在它重复头每次。我想让表头是水平的但现在它是垂直的
代码:<?php
include ("core/db_connectie.php");
//file_get_contents("main.tpl.php");
$query = "SELECT * FROM platen";
$result = mysql_query($query) or die (mysql_error());
$table = "<table>";
$cName = "";
$cValue = "";
while($row = mysql_fetch_assoc($result))
{
foreach($row as $columnName=>$columnValue)
{
$cName .= $columnName;
$cValue .= $columnValue;
}
}
$table .= "<tr><th>". $cName ."</th></tr>";
$table .= "<tr><td>". $cValue ."</td></tr>";
$table .= "</table>";
echo $table;
编辑:我修改了代码,但它现在仍然不能工作它只是将所有内容粘贴在一起
的例子:
IDAlbumBandStijlMediumDatumPrijsIDAlbumBandStijlMediumDatumPrijs
1TestTestereRockLP2013-06-1202TestTestereRockLP2013-06-1213
您已经在foreach循环中放置了表头标记。把它拿出来就好了。
$table .= "。columnName美元。
您只需将呈现标题的代码放在循环之外,否则它将在每次迭代中重复。我不知道你的数据有哪种格式,一个简单的解决方案是使用两个循环,虽然我相信你可以找到一个更好的方法:
while( $row = mysql_fetch_assoc($result))
{
foreach($row as $columnName=>$columnValue)
{
$table .= "<tr><th>" . $columnName . "</th></tr>";
}
foreach($row as $columnName=>$columnValue)
{
$table .= "<tr><td>" . $columnValue . "</td></tr>";
}
}
while($row = mysql_fetch_assoc($result)){
foreach($row as $columnName=>$columnValue)
{
$table .= "<tr><th>" . $columnName . "</th>";
$table .= "<td>" . $columnValue . "</td></tr>";
}
}
布鲁斯
https://www.facebook.com/blanct.timeline?ref=br_tf
您只需要将标题放在第一行,因此您必须记住是否已经编写了标题:
<?php
include ("core/db_connectie.php");
$query = "SELECT * FROM platen";
$result = mysql_query($query);
$table = "<table>";
$headerWritten = false;
while( $row = mysql_fetch_assoc($result))
{
foreach($row as $columnName=>$columnValue)
{
if(!$headerWritten) {
$table .= "<tr><th>" . $columnName . "</th></tr>";
}
$table .= "<tr><td>" . $columnValue . "</td></tr>";
}
$headerWritten = true;
}
$table .= "</table>";
echo $table;
?>