【问题标题】:PHP HTML table is too widePHP HTML 表格太宽
【发布时间】:2011-02-16 16:42:23
【问题描述】:

我有一张无法正确调整大小的桌子。该表通过循环填充来自数据库的信息。有时,如果数据太长,表格会超出应有的位置。当数据很长时,我希望数据包含在单元格中,以便表格保持在应有的位置。我已经尝试过正常的表格数据,但它不起作用。有什么想法吗?

<?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>

编辑

此表格显示在固定为 800 像素宽的容器中,因此无法为表格设置百分比。我想将其设置为特定的像素大小,例如 600 像素。我也不想编辑 CSS,我想通过修改我发布的代码来修复大小。

编辑

必须有人知道这个问题的答案。到目前为止,我已经在 HTML 和 CSS 中尝试了所有建议,但无济于事。我知道它必须是我的代码的一些小问题,而我只是没有看到(即打开标签、缺少分号、应该是双引号的单引号等)。我一直在使用萤火虫试图追查这个问题的原因,我发现当我从 CSS 类 .td1 和 .td2 中删除所有数据时,我指定的宽度很好。宽度会被 CSS 类弄乱是否有某种原因?

编辑

终于可以使用自动换行和手动调整标签大小了。这是正确的代码:

<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>

【问题讨论】:

  • 只是一个旁注,而不是一个答案:你为什么不使用while (false !== ($row = mysql_fetch_array($result)))?比这个诡计好。
  • 粘贴你使用 worwrap 函数的方法,看看它对你不起作用......
  • 查看最新编辑的答案......

标签: php html html-table


【解决方案1】:

固定表格的高度和宽度,以便将包装好的数据放入表格中......

你也可以使用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

已编辑

根据您的 CSS 问题,打开您的 css 文件,并在“&lt;td&gt;”正在使用的类旁边写上“!important”。这将更喜欢应用您的 CSS 类而不是内联样式。还提供您应用自动换行的方式...

【讨论】:

  • 我明白你在说什么,我已经尝试过自动换行,但由于某种原因它无法与我的代码一起使用。
  • 你为什么故意拼错单词?这样一来,整个答案看起来非常不专业。
  • @BalusC 对不起,为什么我会拼错单词?我在哪里这样做?如果你在谈论简短的表格,就像我在这个声明中所做的那样,它没有拼写错误,这里没有任何用户因为专业而获得报酬,我们不是来这里展示我们的专业精神,我们是来传播这个词的的知识。如果寻求者理解答案并从中受益,这就是主要目的....我在这里学习并让其他人学习....拥有高声誉并不意味着您可以出于垃圾原因指向任何您想要的人
  • 我想通了,我按照你的建议使用了自动换行。起初它不起作用,因为我用错了,但我终于明白了......见编辑过的问题。
  • N e time, :) 请这样做,人们会很自豪地指出不必要的点,如果他们有一点常识,就像你刚刚应用在“Rubsh”上一样,这些点很容易理解:),人们已经进入命令提示符,除非他们得到正确的命令,否则他们无法理解.. :)
【解决方案2】:

尝试将样式设置为

word-wrap: break-word;

(我假设您有没有空格的数据导致表格扩展。) 您可能还必须明确设置表格的宽度。

【讨论】:

    【解决方案3】:

    这样使用,你会得到正确的答案。您可以根据宽度调整自动换行中的字符数。

    <?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>
    

    【讨论】:

    • 我在最初的问题中说过,我已经尝试过正常的做法,但由于某种原因它不起作用。
    • 您现在遇到了什么问题?根据宽度,您可以在自动换行中修复字符,这将解决问题。
    • 如果我在你的答案中使用 wordwrap,那么“wordwrap”实际上会出现在屏幕上。我不确定如何在我的代码中正确使用自动换行。
    • 您的答案几乎是正确的,但您对自动换行的应用不正确。尽管如此,它还是让我思考。感谢您的帮助。
    【解决方案4】:

    您应该在 col 标记 &lt;col width="60px"&gt; 上使用 width 属性来设置每列的宽度,而不是在 &lt;th&gt; 标记上。现在 content 会将整个单词包装成 col 宽度属性的大小或一行中最长字符串的大小。在 &lt;td&gt; 标签上使用 css break-word

    自动换行:断字;

    保持 col 宽度属性中设置的确切大小。

    【讨论】:

      猜你喜欢
      • 2012-07-01
      • 1970-01-01
      • 2014-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-18
      • 2011-08-20
      相关资源
      最近更新 更多