centre a php table:

Soldato
Joined
26 Nov 2005
Posts
3,839
Location
Doon the Bay (Newcastle)
how do i centre this table?



echo "<table border='1'>
<tr>
<th>Date</th>
<th>Event</th>
<th>Type</th>
<th>Information</th>
</tr>";




while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['i1'] . "</td>";
echo "<td>" . $row['i2'] . "</td>";
echo "<td>" . $row['i3'] . "</td>";
echo "</tr>";
}
echo "</table>";








Data is pulling through fine from the SQL (yey spent all day getting that right) but the table it displaying to the left while my whole website is centered.


Thanks!!!! :D
 
<div style="margin: 0 auto; width: 960px">


If you have to use inline styles then use that, otherwise try to use an external stylesheet.
That DIV will be 960px wide btw so you can change it to whatever you want, but to have a div horizontally aligned you must define a width and 960px is standard web page width these days.

edit: Talked dog muck, stick the width of the table in that div and it will centre it horizontally
 
Last edited:
so theres no way to actually centre a table relative to the web browser? What if I view it on a 1920 wide screen? Will it be centred in the centre of the left most 960px?

Also, I know the mantra is 'tables are bad mmkay?' but i thought this only applied in the context of page layout. (like columns, graphics, to make it more like 'print') that is if your data is in a tabular format, (e.g. a set of returned db rows) then a table is the ideal way to present that data to the user?
 
Tables are bad for non-tabular data, but I don't have the energy to tell yet another person :D I'll just help with what the OP wants and be done with it!
 
PHP:
<div style="margin: 0 auto; width: 960px">
<table border="1">
<tr>
<th>Date</th>
<th>Event</th>
<th>Type</th>
<th>Information</th>
</tr>

<?php
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['date'] . "</td>";
  echo "<td>" . $row['i1'] . "</td>";
  echo "<td>" . $row['i2'] . "</td>";
  echo "<td>" . $row['i3'] . "</td>";
  echo "</tr>";
  }
?>
</table>
</div>

That should work. Code shamelessly stolen from tsinc80697.

(you should really put it in the stylesheet file though instead)
 
Also, I know the mantra is 'tables are bad mmkay?' but i thought this only applied in the context of page layout. (like columns, graphics, to make it more like 'print') that is if your data is in a tabular format, (e.g. a set of returned db rows) then a table is the ideal way to present that data to the user?

You're absolutely right, don't listen to anyone who says otherwise. Tables are for tabular data, exactly like you're using them (and you even have THs too!).
 
afaik align="center" isnt xhtml/css valid.

Also wise to avoid using tables if you can.

But if you cant avoid that and your table is a set width you can always style it with CSS using "margin: 0 auto" to centre it.


Tables are fine for what he's using them for. Avoid using tables for layouts by all means, but for tabular data that's what they're meant for.
 
Back
Top Bottom