如何使用php循环获取html表中的固定表列宽度

时间:2022-11-29 09:12:20

This is my code to get data from a database to a html table. but column size expand with the size of the details. i need to get a fixed size columns. please help me to get fixed size columns.

这是我将数据从数据库获取到html表的代码。但是列大小会随着细节的大小而扩大。我需要一个固定大小的列。请帮助我获得固定大小的列。

<?php 
ini_set ('display_errors',0);
?>
<!DOCTYPE html>
<?php
    require_once 'issue_tracker_connection.php';
    mysql_select_db('issue_tracker');
    $sql = "SELECT * FROM issue_tracker";
    $records = mysql_query($sql);
?>
<html>

<head>
<style>

table th, td{
    table-layout:fixed;
    border: 1px solid black;
    border-collapse: collapse;
    align:center;
}
</style>
</head>

<body>

<?php
echo '<table >';
$columns = array();
$resultset = array();
while ($row = mysql_fetch_assoc($records)) {
    if (empty($columns)) {
        $columns = array_keys($row);
        echo '<tr><th>'.implode('</th><th>', $columns).'</th></tr>';
    }
    $resultset[] = $row;
    echo '<tr><td >'.implode('</td><td>', $row).'</td></tr>';
}
echo '</table>';

?> 
</body>
</html>

3 个解决方案

#1


0  

Try this:

尝试这个:

<?php
echo '<table width="100%" cellspacing="0" cellpadding="2" border="0">';
$columns = array();
$resultset = array();
while ($row = mysql_fetch_assoc($records)) {
    if (empty($columns)) {
        $columns = array_keys($row);
        echo '<tr><th>'.implode('</th><th>', $columns).'</th></tr>';
    }
    $resultset[] = $row;
    echo '<tr><td >'.implode('</td><td>', $row).'</td></tr>';
}
echo '</table>';

?> 

#2


0  

You can also set size of tag... Try it now...

您还可以设置标签大小...立即尝试...

<tr>
<td width="50%">Your Code Here</td>
<td width="50%">Your Code Here</td>
</tr>

#3


0  

Try this..

尝试这个..

<table class="fixed">
    <col width="20px" />
    <col width="30px" />
    <col width="40px" />
    <tr>
        <td>text</td>
        <td>text</td>
        <td>text</td>
    </tr>
    </table>

and CSS:

和CSS:

table.fixed { table-layout:fixed; }
table.fixed td { overflow: hidden; }

Check original answer here.

在这里查看原始答案。

#1


0  

Try this:

尝试这个:

<?php
echo '<table width="100%" cellspacing="0" cellpadding="2" border="0">';
$columns = array();
$resultset = array();
while ($row = mysql_fetch_assoc($records)) {
    if (empty($columns)) {
        $columns = array_keys($row);
        echo '<tr><th>'.implode('</th><th>', $columns).'</th></tr>';
    }
    $resultset[] = $row;
    echo '<tr><td >'.implode('</td><td>', $row).'</td></tr>';
}
echo '</table>';

?> 

#2


0  

You can also set size of tag... Try it now...

您还可以设置标签大小...立即尝试...

<tr>
<td width="50%">Your Code Here</td>
<td width="50%">Your Code Here</td>
</tr>

#3


0  

Try this..

尝试这个..

<table class="fixed">
    <col width="20px" />
    <col width="30px" />
    <col width="40px" />
    <tr>
        <td>text</td>
        <td>text</td>
        <td>text</td>
    </tr>
    </table>

and CSS:

和CSS:

table.fixed { table-layout:fixed; }
table.fixed td { overflow: hidden; }

Check original answer here.

在这里查看原始答案。