I have a table that collects data from web inputs and stores them on an html page... the question how do i style this table... I am not sure how to give it borders, style columns (column 1 is red, 2 is blue, 3 is red, so on), or color the headers... i think i am just lost in my code, but can someone help me?

My code:

<?php
echo "<html>\n<body>\n\t<table style=''>\n\n";
$f = fopen("MYCSVFILE.csv", "r"); //Safe
$trcount = 0; //start the row count as 0 //start from the column heading
while (($line = fgetcsv($f)) !== false) {
        $trclass = ''; if ($trcount%2==0) { $trclass=' class="dark"'; } //default to nothing, but if it's even apply a class
        echo "\t\t<tr".$trclass.">\n"; //same as before, but now this also has the variable $class to setup a class if needed
        $tdcount = 0; //reset to 0 for each inner loop
        foreach ($line as $cell) {
                $tdclass = ''; if ($tdcount%2==0) { $tdclass=' class="dark"'; } //default to nothing, but if it's even apply a class
                echo "\t\t\t<td ".$tdclass."style='padding:.4em;'>" . htmlspecialchars($cell) . "</td>"; //same as before, but now this also has the variable $class to setup a class if needed
                $tdcount++; //go up one each loop
        }
        echo "\r</tr>\n";
        $trcount++; //go up one each loop
}
fclose($f);
echo "\n\t</table>\n</body>\n</html>";
?>

Recommended Answers

All 9 Replies

So, i'm not a PHP developer, but I am wondering why are you including the HTML structure elements within your echo...

instead why not do this...then style the table either by applying the style within the head element, or referring to an external style sheet.

<DOCTYPE! html>
<html>
<head>
<title>My title</title>
<style>
table {border:1px solid black;border-collapse:collapse;}
</style>
</head>
<body>
<?php

Your PHP code

?>
</body>
</html>

To apply a style to a column, you could create column groups and apply a class to each column. here is an exmaple..(without the PHP, but you should be able to adapt.)

<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<style>
table{border-collapse:collapse}
.col1 {background:red}
.col2 {background:blue}
.col3 {background:green}
td,th{border:1px solid #000;padding:5px}
</style>
</head>
<body>
<table>
    <colgroup>
    <col class="col1" />
    <col class="col2" />
    <col class="col3" />
    </colgroup>
    <tr>
        <th>red</th>
        <th>blue</th>
        <th>green</th>
    </tr>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
</table>
</body>
</html>

I'd recommend that you do not try to echo all of your HTML. Only what you really need to.

Oh wow thanks for you advice and resource... I have a quick question... when I run this... how come an extra line appears? Do you know what caused it? Maybe an extra "\n" was added?

And also... this maybe a dumb question... but how do you adjust the widths of each individual columns... can't figure it out...

Member Avatar for LastMitch

@<MICHAEL>

I am not sure how to give it borders, style columns (column 1 is red, 2 is blue, 3 is red, so on), or color the headers... i think i am just lost in my code, but can someone help me?

That's is not the way to echo out the table with color.

Here is a link on how to echo out a table with color in csv correct way:

http://stackoverflow.com/questions/5277099/producing-a-colorful-table-from-a-csv-file-with-php

Member Avatar for LastMitch

@JorgeM

So, i'm not a PHP developer, but I am wondering why are you including the HTML structure elements within your echo...

Yes, you can echo out html in certain circumstance. There's no issue with that.

He's echoing out an excel sheet into a table in php into a website

Ok, while its OK to echo all HTML, it just seems a bit messy when you are "echoing" certain elements like <html>, <head>, <body>, etc.. What if you needed to apply an internal stylesheet. Seems like a lot of extra unorganized code to include that within an echo statement. In any case, I was just wondering. I plan on picking up PHP in the near future.

width for the columns? I would just apply the width to the td element...

td {width:##px;}

Member Avatar for LastMitch

@JorgeM

Seems like a lot of extra unorganized code to include that within an echo statement. In any case, I was just wondering. I plan on picking up PHP in the near future.

<MICHAEL> example is bit unorganized. He's still a kid in high school. So he is still learning.

What if you needed to apply an internal stylesheet.

Yes you can put internal stylesheet.

Regarding about this td {width:##px;}

Here is how you put a internal stylesheet:

<?php
echo '<html><head><style type="text/css">td {width:50px; color: blue; }</style></head><body>';
echo '<table border=1 cellpadding=2 cellspacing=2>';
echo '<tr>';
echo '<td><p>This&nbsp;time</p></td>';
echo '<td><p>No&nbsp;time</p></td>';
echo '</tr>';
echo '</table>';
echo '</body></html>';
?>

Usually I only echo out a table using that. I agree with you that it's bit messy to actaully echo out the whole website. I only see people echoing out tables or forms nothing more than that.

Okay, I applied some styling... but unfortunately I can't see some of them... for example.... I wanted to make the header an orange-yellow color... it doesn't work.... Can you guys tell me why some of my stylings do not work/do anything...?

Here is my code:

<style> table{width:100%; border-collapse:collapse; border:1px solid grey; background: #fafafa; font-size: 12px;} td {font-size: 12px; border: 1px solid grey;border-collapse:collapse; text-align: left; width: 150px; padding-top: 5px; padding-left: 5px;} th {background: #ffb300; text-align: left; padding-top: 5px; padding-left: 5px; font-size: 12px;} tr:hover {color: red; font-size: 12px;} .col1 {background: #fafafa;} .col2  {background: none;} .dark {background: #fafafa;} .light {background: #fff;}</style>
<body>

<?php
echo "<html>\n<body>\n\t<table style=''>\n\n";
$f = fopen("CSVFILE.csv", "r"); //Safe
$trcount = 0; //start the row count as 0 //start from the column heading
while (($line = fgetcsv($f)) !== false) {
        $trclass = ''; if ($trcount%2==0) { $trclass=' class="dark"'; } //default to nothing, but if it's even apply a class
        echo "\t\t<tr".$trclass.">\n"; //same as before, but now this also has the variable $class to setup a class if needed
        $tdcount = 0; //reset to 0 for each inner loop
        foreach ($line as $cell) {
                $tdclass = ''; if ($tdcount%2==0) { $tdclass=' class="light"'; } //default to nothing, but if it's even apply a class
                echo "\t\t\t<td ".$tdclass."style='padding:.4em;'>" . htmlspecialchars($cell) . "</td>"; //same as before, but now this also has the variable $class to setup a class if needed
                $tdcount++; //go up one each loop
        }
        echo "\r</tr>\n";
        $trcount++; //go up one each loop
}
fclose($f);
echo "\n\t</table>\n</body>\n</html>";
?>
Member Avatar for LastMitch

@<MICHAEL>

I wanted to make the header an orange-yellow color... it doesn't work.... Can you guys tell me why some of my stylings do not work/do anything...?

Read my previous post. It will show you how to echo the table.

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.