开发者

Setting col widths of a mysql resultset in Php

I'm querying a MySQL table to get some data that I display on a php page. The problem is that the col widths are small for a few columns. Is there any-way I can specifically change those (not the rest)?

Currently, I'm doing my printing via a loop, so it seems strange to me as to how to pin-point which corresponds to the coloumn. Do I maybe parse the contents and see make a decision on ?

Here's the code I am using:

    $result = mysql_query("SELECT * FROM {$table}"); 
    if (!$result) {    die("Query to show fields from table failed"); }

    $fields_num = mysql_num_fields($result);

    echo "<table><tr>"; // Table headers 
    for($i=0; $i<$fields_num; $i++) {    
        $field = 开发者_如何学Gomysql_fetch_field($result);    
        echo "<td><b>{$field->name}</td></b>"; 
    } 
    echo "</tr></th>\n"; // Table rows 
    while($row = mysql_fetch_row($result)) {    
        echo "<tr>";

       foreach($row as $cell)
           echo "<td>$cell</td>";

       echo "</tr>\n"; 
    } 
    mysql_free_result($result); 
?>

    </td> </tr> </table>


your HTML tables are not correct, the tables should be listed as follows, i copied it from the w3schools webpage:

http://www.w3schools.com/html/html_tables.asp

<table>
    <tr>
        <th>Header 1</th>
        <th>Header 2</th>
    </tr>
    <tr>
        <td>row 1, cell 1</td>
        <td>row 1, cell 2</td>
    </tr>
    <tr>
        <td>row 2, cell 1</td>
        <td>row 2, cell 2</td>
    </tr>
</table> 

If your HTML is all set then you can add some CSS to adjust the widths of the cells

table, td, th {
border:                         2px solid gray;
border-collapse:                collapse;
text-align:                     left;
width:                          auto;
padding:                        3px;
}

I added some other features of the table here but i think all you need is the width = auto. Let me know how it works out

You can set different table widths by setting each table to have an individual id, then set that id in the css to the specific widths you need.

<table id="formtable">

then in the css you can use

#formtable th td{
width:                          auto;
}

See how that works and let me know


I would suggest this can be achieved with css Set some styles show the columns are appropriate widths

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜