table headers not aligning properly
I am outputing 4 columns from a mysql query, but using the code below doesnt align each column wiht the headers, I guess is due the fact this are static declared against the dynamic rows . Can someone advise a way to align the headers properly with each fetched column ,,,
$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 </t开发者_开发百科h><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>";
This line is wrong:
echo '<tr style=" align="center">';
I think you want:
echo '<tr style="text-align:center;">';
Are you using Joomla? The loadRowList()
suggests this. If you are, then use loadAssocList()
instead, which returns the field names as well as the field values. You can do then a separate loop to output your column headers and guarantee they match up with the data fields.
docs for the function here: http://help.joomla.org/content/view/509/60/
you'd do something like:
$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>';
}
精彩评论