如何向表中添加自动换行符和最大列宽

时间:2014-07-08 11:34:48

标签: php html html-table width

如何为表格列添加不同的最大宽度?当我在列中写入内容时,我也希望有一个自动换行符。这是我的代码:

echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Kategorie</th> <th>Titel</th> <th>Betroffene Seiten</th> <th>Beschreibung</th><th>prioritaet</th><th>Status</th><th>Eingereicht von</th><th>Umsetzung mit Release</th><th>Dev. Kommentar</th><th>Entwickler</th><th></th> <th></th></tr>";

// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {

    // echo out the contents of each row into a table
    echo "<tr>" ;
    echo '<td>' . $row['id'] . '</td>';
    echo '<td>' . $row['kategorie'] . '</td>';
    echo '<td>' . $row['titel'] . '</td>';
    echo '<td>' . $row['betroffen'] . '</td>';
    echo '<td>' . $row['beschreibung'] . '</td>';
    echo '<td>' . $row['prioritaet'] . '</td>';
    echo '<td>' . $row['status'] . '</td>';
    echo '<td>' . $row['eingereicht'] . '</td>';
    echo '<td>' . $row['umsetzung'] . '</td>';
    echo '<td>' . $row['kommentar'] . '</td>';
    echo '<td>' . $row['entwickler'] . '</td>';
    echo '<td><a href="edit.php?id=' . $row['id'] . '">bearbeiten</a></td>';
    echo '<td><a href="delete.php?id=' . $row['id'] . '">löschen</a></td>';
    echo "</tr>"; 
} 

// close table>
echo "</table>";

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作来断开列中的行并保持最大宽度

echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Kategorie</th> <th>Titel</th> <th>Betroffene Seiten</th> <th>Beschreibung</th><th>prioritaet</th><th>Status</th><th>Eingereicht von</th><th>Umsetzung mit Release</th><th>Dev. Kommentar</th><th>Entwickler</th><th></th> <th></th></tr>";

// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {

    // echo out the contents of each row into a table
    echo "<tr>" ;
    echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['id'], 20, "<br />\n") . '</td>';
    echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['kategorie'], 20, "<br />\n") . '</td>';
    echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['titel'], 20, "<br />\n") . '</td>';
    echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['betroffen'], 20, "<br />\n") . '</td>';
    echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['beschreibung'], 20, "<br />\n") . '</td>';
    echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['prioritaet'], 20, "<br />\n") . '</td>';
    echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['status'], 20, "<br />\n") . '</td>';
    echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['eingereicht'], 20, "<br />\n") . '</td>';
    echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['umsetzung'], 20, "<br />\n") . '</td>';
    echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['kommentar'], 20, "<br />\n") . '</td>';
    echo '<td width="'.strlen($row['id'])+5.'">' . wordwrap($row['entwickler'], 20, "<br />\n") . '</td>';
    echo '<td><a href="edit.php?id=' . $row['id'] . '">bearbeiten</a></td>';
    echo '<td><a href="delete.php?id=' . $row['id'] . '">löschen</a></td>';
    echo "</tr>"; 
} 

// close table>
echo "</table>";

使用 strlen 函数将max width设置为td并使用 wordwrap 将列值拆分为新行

答案 1 :(得分:0)

您可以设置宽度为td(以像素为单位)或%。在一个td中设置将为列设置它。

echo '<td style="width:40px">' . $row['id'] . '</td>';

我没有理解你的强化:

  

&#34;当我在列中写下内容时。&#34;

您可以使用\n在PHP代码中获取新行。只需将其添加到您的代码中。

you can see here

您还可以在HTML中使用<br />来获得相同的换行符。