表标题没有正确对齐


table headers not aligning properly

我从mysql查询输出4列,但使用下面的代码不对齐每列的标题,我猜是由于这是静态声明对动态行。有人能建议一种方法来正确对齐标题与每个获取的列…

$tableStyle = "padding: 5px;border:1px"; 
    $tdStyle = "padding:5px "; 
    $thStyle = "padding:5px; align:center ";

    echo '<table style="' . $tableStyle . '" cellpadding="7" cellspacing="7">'; 
    echo "<tr> <th>Quiz Title </th><th> Score </th><th>Maximum Score </th><th>Finished On </th></tr>"; 
    $row = $database->loadRowList();
    foreach($row as $valuearray)
    {
    echo '<tr style=" align="center">';
    foreach($valuearray as $field)
    {
    echo "<td>$field</td>";
    }
    echo "</tr>";
    }
    echo "</table>";

这一行不对:

echo '<tr style=" align="center">';

我想你要:

echo '<tr style="text-align:center;">';

您正在使用Joomla吗?loadRowList()表明了这一点。如果是,则使用loadAssocList(),它将返回字段名和字段值。然后你可以做一个单独的循环来输出你的列标题,并保证它们与数据字段匹配。


这里函数的文档:http://help.joomla.org/content/view/509/60/

你可以这样做:

$rows = $database->loadAssocList();
echo '<tr>';
foreach(array_keys($rows[0]) as $header) {
    echo "<th>$header</th>";
}
echo '</tr>';
foreach ($rows as $row) {
    echo '<tr>';
    foreach($row as $value) {
        echo "<td>$value</td>";
    }
    echo '</tr>';
}