tags:

views:

94

answers:

3

Hi,

I have created a table in PHP but the cells size is not fixed it expends when the input is long. Here is the Code.

echo "<div style=\"overflow-y: scroll; white-space: nowrap; height: 190px;\">\n";
        echo "<table cellspacing=\"1\" cellpadding=\"1\" align=\"center\" width=\"100%\" id=\"clients\">\n";
        echo "<tr bgcolor=\"2b2d5d\">\n";
        echo "<th align=\"left\" style=\"color: FFFFFF; font-size: 12px; font-weight: normal; font-family: Arial; padding: 5\">&nbsp;</th>\n";
        echo "<th align=\"left\" style=\"color: FFFFFF; font-size: 12px; font-weight: normal; font-family: Arial; padding: 5\">Name</th>\n";
        echo "<th align=\"left\" style=\"color: FFFFFF; font-size: 12px; font-weight: normal; font-family: Arial; padding: 5\">Address</th>\n";
        echo "<th align=\"left\" style=\"color: FFFFFF; font-size: 12px; font-weight: normal; font-family: Arial; padding: 5\">Phone</th>\n";
        echo "<th align=\"left\" style=\"color: FFFFFF; font-size: 12px; font-weight: normal; font-family: Arial; padding: 5\">Fax</th>\n";

I want to fixed the cells width please need help. Thanks

+4  A: 

Just set % widths on the th tags .

<th style="width:20%;">

or you could just set a fixed width on certain columns if you preffered that design.

I would also advice you to move the inline css into a class to save you repeating the same code, and place it in a seperate css stylesheet. Incase your unfamiliar (or anyone who is reading) attach a stylesheet by placing the below in the head of the page:

<link rel="stylesheet" href="default.css">

example class:

.myTableColumn{color: FFFFFF; font-size: 12px; font-weight: normal; font-family: Arial; padding: 5;width:20%;}

Call it from the column:

<th class="myTableColumn">
Andi
This is the best solution. Even using the `wordwrap()` function (http://php.net/wordwrap) will not help as HTML will collapse whitespace and will treat it the same (of course you could wrap it in <pre> tags but then I'd just be getting pedantic).
dcousineau
Just a bit of an extension to this is that if you use a % width, the text will expand on larger resolutions. 20% of 768 pixels is different than 20% of 1050 pixels. Using a pixel width would be more proper IF you need it to be an exact width permanently. Also, the code is missing the semicolon after 20% :)
Jesse O'Brien
you don't need a semi colon for a single style definition, but i'll put it for you :)
Andi
You could also try nl2br(wordwrap())
Keeper
A: 

Still not working I have put the style code in my CSS

.myTableColumntd{
 color: 000000;
 font-size: 12px; 
 font-weight: normal; 
 font-family: Arial; 
 padding: 2;
 width:20%;
 }

and the PHP is now

echo "<th align=\"left\" class=\"myTableColumnth\">&nbsp;</th>\n";
    echo "<th align=\"left\" class=\"myTableColumnth\">Name</th>\n";
    echo "<th align=\"left\" class=\"myTableColumnth\">Address</th>\n";
    echo "<th align=\"left\" class=\"myTableColumnth\">Phone</th>\n";
    echo "<th align=\"left\" class=\"myTableColumnth\">Fax</th>\n";

But still the Input Display is the same the cells are still expending.

Talha Bin Shakir
your class is declared as .myTableColumntd, but your html references myTableColumnth
dnagirl
A: 
<th width="50"></th>

should do. If it doesn't

<th style="max-width: 50px;"></th>
Ivan Vučica