I have a table that I cannot get to size correctly. The table populates with information from a database via a loop. Sometimes if the data is too long the table extends past where it should. When the data is that long I want the data to wrap in the cells so the table stays where it should. I have tried the normal table data but it isn't working. Any ideas?
<?php
echo "<table>
<tr>
<th>id</th>
<th>700-number</th>
<th>First name</th>
<th>Last name</th>
<th>Email</th>
<th>Response</th>
<th>Created On</th>
</tr>";
$num = mysql_num_rows($result);
for ($i = 0; $i < $num; $i++)
{
$row = mysql_fetch_array($result);
$id = $row['id'];
$school_id = $row['school_id'];
$fname = $row['first_name'];
$lname = $row['last_name'];
$email = $row['email'];
$attending = ($row['attending'] == 0) ? 'No' : 'Yes';
$date = $row['created_on'];
$class = (($i % 2) == 0) ? "td2" : "td1";
echo "<tr>";
echo "<td class=" . $class . ">$id</td>";
echo "<td class=" . $class . ">$school_id</td>";
echo "<td class=" . $class . ">$fname</td>";
echo "<td class=" . $class . ">$lname</td>";
echo "<td class=" . $class . ">$email</td>";
echo "<td class=" . $class . ">$attending</td>";
echo "<td class=" . $class . ">$date</td>";
echo "</tr>";
}
?>
</table>
EDIT
This table is displayed in a container that is fixed to 800px wide, so setting a percentage for the table will not work. I want to set it to a specific pixel size, like 600px. I would also rather not edit the CSS, I want to fix the size by modifying the code I have posted.
EDIT
There has to be someone who knows the answer to this. I have tried all the suggestions so far, in HTML and CSS but to no avail. I know it has to be some small problem with my code that I am just not seeing (i.e. an open tag, a missing semi-colon, a single quote where it should be a double quote, ect.). I have been using firebug trying to track down the cause of this problem and I have found that when I remove all data from the CSS classes .td1 and .td2 that the width I have specified sticks just fine. Is there some reason the width would get messed up by a CSS class?
EDIT
Finally got this working right with wordwrap and sizing the tags manually. Here is the correct code:
<table>
<tr>
<th width="16px">ID</th>
<th width="81px">700<br />Number</th>
<th width="90px">First<br />Name</th>
<th width="90px">Last<br />Name</th>
<th width="181px">E-Mail</th>
<th width="74px">Attending</th>
<th width="82px">Created<br />On</th>
</tr>
<?php
$num = mysql_num_rows($result);
for ($i = 0; $i < $num; $i++)
{
$row = mysql_fetch_array($result);
$id = $row['id'];
$school_id = $row['school_id'];
$fname = $row['first_name'];
$lname = $row['last_name'];
$email = $row['email'];
$attending = ($row['attending'] == 0) ? 'No' : 'Yes';
$date = $row['created_on'];
$wrap_id = wordwrap($id, 4, "\n", TRUE);
$wrap_school_id = wordwrap($school_id, 9, "\n", TRUE);
$wrap_fname = wordwrap($fname, 10,"\n", TRUE);
$wrap_lname = wordwrap($lname, 10, "\n", TRUE);
$wrap_email = wordwrap($email, 20, "\n", TRUE);
$wrap_attending = wordwrap($attending, 3, "\n", TRUE);
$wrap_date = wordwrap($date, 10, "\n", TRUE);
$class = (($i % 2) == 0) ? "td2" : "td1";
echo "<tr>";
echo "<td class=" . $class . ">$wrap_id</td>";
echo "<td class=" . $class . ">$wrap_school_id</td>";
echo "<td class=" . $class . ">$wrap_fname</td>";
echo "<td class=" . $class . ">$wrap_lname</td>";
echo "<td class=" . $class . ">$wrap_email</td>";
echo "<td class=" . $class . ">$wrap_attending</td>";
echo "<td class=" . $class . ">$wrap_date</td>";
echo "</tr>";
}
?>
</table>