我有一张桌子,我无法正确调整尺寸。该表通过循环填充来自数据库的信息。有时,如果数据太长,表格会延伸到应有的位置。当数据很长时,我希望数据包装在单元格中,这样表就可以保留在应有的位置。我已经尝试了普通的表数据,但它无法正常工作。有什么想法吗?
<?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>
修改
此表显示在固定为800px宽的容器中,因此设置表的百分比将不起作用。我想将其设置为特定的像素大小,如600px。我也不想编辑CSS,我想通过修改我发布的代码来修复大小。
修改
必须有人知道答案。到目前为止,我已经尝试了所有的建议,在HTML和CSS中,但无济于事。我知道我的代码中有一些小问题,我只是没有看到(即一个开放的标签,一个缺少的分号,一个单引号,它应该是一个双引号,等等)。我一直在使用firebug试图找出这个问题的原因,我发现当我从CSS类.td1和.td2中删除所有数据时,我指定的宽度都很好。是否有某种原因宽度会被CSS类弄乱?
修改
最后使用wordwrap并手动调整标签大小。这是正确的代码:
<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>
答案 0 :(得分:2)
修复表格的高度和宽度,以便将包裹的数据放入表格中......
您也可以使用wordwrap
功能
使用字符串中断字符将字符串包装到给定数量的字符。
语法:string wordwrap ( string $str [, int $width = 75 [, string $break = "\n" [, bool $cut = false ]]] )
str
The input string.
width
The column width.
break
The line is broken using the optional break parameter.
cut
If the cut is set to TRUE, the string is always wrapped at or before the specified width. So if you have a word that is larger than the given width, it is broken apart. (See second example).
Returns the given string wrapped at the specified column.
更新回复
根据您编辑的问题,您最好使用自动换行,如上所述..
例如:
<?php
$text = "Typoking wants to know about "Php html table is too wide".";
$newtext = wordwrap($text, 20, "<br />\n");
echo $newtext;
?>
The above example will output:
Typoking wants to kn<br />
ow about "Php html t<br />
able is too wide".
有关更多信息,请访问:PHP.net
<强> EDITED 强>
根据您的CSS问题,打开您的css文件,并在“!important
”正在使用的类旁边写上“<td>
”。这比使用内联样式更喜欢CSS类。还提供了应用自动换行的方式......
答案 1 :(得分:0)
尝试将样式设置为
word-wrap: break-word;
(我假设您的数据没有空格导致表格扩展。) 您可能还必须明确设置表格的宽度。
答案 2 :(得分:0)
像这样使用,你会得到正确答案。你可以根据宽度调整wordwrap中的字符。
<?php
echo "<table width='800'>
<tr>
<th width='50'>id</th>
<th width='100'>700-number</th>
<th width='150'>First name</th>
<th width='150'>Last name</th>
<th width='100'>Email</th>
<th width='150'>Response</th>
<th width='100'>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 . ">wordwrap($school_id, 8, '\n', 1); </td>";
echo "<td class=" . $class . ">wordwrap($fname, 15, '\n', 1);</td>";
echo "<td class=" . $class . ">wordwrap($lname, 20, '\n', 1);</td>";
echo "<td class=" . $class . ">wordwrap($email, 20, '\n', 1);</td>";
echo "<td class=" . $class . ">wordwrap($attending, 30, '\n', 1);</td>";
echo "<td class=" . $class . ">wordwrap($date, 10, '\n', 1);</td>";
echo "</tr>";
}
?>
</table>
答案 3 :(得分:0)
您应该在 col 标记<col width="60px">
上使用宽度属性来设置每列的宽度,而不是<th>
标记。现在,content将整个单词包装为col width属性的大小或一行中最长字符串的大小。在<td>
代码
自动换行:break-word;
保持col width属性中设置的确切大小。