I have a table that uses some php to populate it, and I am having trouble adjust each individual column width (based on their headers)... Can someone tell me how to do that?
(i.e) Name column is 30px width, last name column is 50px width, comments is 100px width

<style> table {width:100%; border-collapse:collapse; border:3px solid red; font-size: 10px;} 
        td {font-size: 10px; border: 1px solid blue;border-collapse:collapse; text-align: left; width: 50px; padding-top: 1px; padding-left: 1px;} 
        th {background: #ffb300; text-align: left; padding-top: 1px; padding-left: 1px; font-size: 10px;}  
        tr:hover {color: red; font-size: 10px;} 
        .dark {background: #ffb300;} 
        .light {background: #ffb300;} 
</style>

<body>

<?php
echo "<html><body><table style=''>"; 
$f = fopen("csv.csv", "r"); 
$trcount = 0; 

while (($line = fgetcsv($f)) !== false) { 
        $trclass = '';  
        if ($trcount%2==0) 
                          { $trclass=' class="dark"'; } 
            echo "<tr".$trclass.">\n";

    $tdcount = 1; 
        foreach ($line as $cell) {
                $tdclass = ''; 
                if ($tdcount%2==0) 

                       { $tdclass='class="light"'; } 


                echo "<td ".$tdclass."style='padding:.4em;'>" . htmlspecialchars($cell) . "</td>"; 
                $tdcount++;
        }
        $trcount++; 
}
fclose($f);
echo "</table></body></html>";
?>

Recommended Answers

All 3 Replies

Member Avatar for LastMitch

@<MICHAEL>

I am having trouble adjust each individual column width (based on their headers)... Can someone tell me how to do that?
(i.e) Name column is 30px width, last name column is 50px width, comments is 100px width

May I ask you a question?

Why do you have a <table style=''> and also a <style> ?

In order to echo out the column in different width you have put a style in the table kinda what you have now: <table style=''>

You have nothing in it?

wait... are you saying I have to put all of lines 1-6 in side of the <table style=''>... But anyways... I thought that since this is the web page that gets displayed, I didn't have to put it there... but your solution makes sense!

But do you have a solution for my original question on how to adjust the columns width?
Can I take a php variable such as $name and make it a css class like this: name {width: ##;}?

Thanks for your quick response! :)

I assume you have only 3 columns as u said name, last and comments, so you can try following

if($tdcount==1)
    $width="30px";
else if ($tdcount==2)
    $width="50px";
else if ($tdcount==3)
    $width="100px";
else
    $width="50px";



echo "<td ".$tdclass."style='padding:.4em';width:'{$width}' >" . htmlspecialchars($cell) . "</td>"; 
            $tdcount++;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.