tags:

views:

42

answers:

2

Hi guys,

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 = mysql_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>
A: 

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

Shaun Hare
Shaun ... I'm not too familiar with using CSS, but I do know about it. Wouldn't CSS set the column widths the same for all the columns? I only want to change it for a few specific columns.I think it would be really hard to isolate them ... since they really are use individual data cells placed due to <td> ...
c0d3rs
A: 

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

Drewdin
Hi Drewdin, I did change my table to reflect the right HTML, I was just being blind ... but the coloumn widths remain the same ...
c0d3rs
Hi Drew - Thanks for the quick reply! The width: auto does take care of a few of my coloums. But I have some other tables on my page (using them for menus) ... and they get completely messed up visually due to the auto. I tried to force it by saying width=100%, but it doesn't seem to work, the CSS takes priority ... any suggestions??
c0d3rs
Nevermind, I got it to work! instead of changing the CSS, I just changed the table style attribute ... thanks again!
c0d3rs
The style attribute of the HTML? that's not recommended anymore and might ack different in the different browsers, if you can use CSS it is the preferred way. Good luck!
Drewdin