CSS for a PHP table

Good day,

I have a website made with HTML and a CSS styles file.
Within this HTML website, I have a .php page interacting with a MySQL database and getting data from there to make a table.

Someone told me is it possible to give style with a CSS file to a PHP table, something like this:
print(“<TABLE id=\“myTable\”>”);

I have two questions:

  • which CSS instructions are usable here to format a PHO table?
  • how can I give currency format to one field pof the table?

Thanks a lot!!!

The PHP is simply ‘rendering’ standard HTML output. The CSS styling information should remain in your CSS files.
If I understand your question correctly you can very easily modify the resultant HTML (generated from the PHP) with any CSS tags you need.
There are a few ways to do it, but the simplest is to modify the HTML that is embedded in the PHP file with the style attributes you need (like class= or id=).

If you can post a sample of the PHP file (in ‘code’ tags) it would be easier to guide you.

Hi ParkinT,

Thanks a lot for your answer.
the PHP code is a table generator from MySQL data, creating rows and columns depending on the information on the MySQL table:

    $table_format = "border='2'";
    
    print ("&lt;TABLE $table_format&gt;\

");

// table header:
print(“<TR>
“);
for($column_num = 0; $column_num < $column_count; $column_num++) {
$field_name = mysql_field_name($result, $column_num);
print(”<TH>$field_name</TH>”);
}
print("</TR>
");

//table body:
while($row = mysql_fetch_row($result)) {
print(“<TR ALIGN=CENTER VALIGN=TOP>”);
for($column_num = 0; $column_num < $column_count; $column_num++) {
print("<TD>$row[$column_num]</TD>
“);
}
print(”</TR>
“);
}
print(”</TABLE>
");

What I want to do is to replace $table_format for CSS style like id= or class=, and also give style to <tr> <th> and <td> tags in the same way.

Thanks!!!

So in every PHP “print” statement you should recognize the standard HTML tags that are being rendered.
Simply add your style attributes as you would on any HTML page.
For example:


$table_format = "id='primarytable'";  //setup a CSS style rule for #primarytable {}

print ("<TABLE $table_format>\
");

// table header:
print("<TR class='tableheaderrow'>\
");     //of course, you will use class names that make sense to you!!!
for($column_num = 0; $column_num < $column_count; $column_num++) {
    $field_name = mysql_field_name($result, $column_num);
    print("<TH class='tableheader'>$field_name</TH>");
}
print("</TR>\
");

//table body:
while($row = mysql_fetch_row($result)) {
    print("<TR class='everyrow' ALIGN=CENTER VALIGN=TOP>");
    for($column_num = 0; $column_num < $column_count; $column_num++) {
        print("<TD class='tabledata'>$row[$column_num]</TD>\
");
    }
    print("</TR>\
");
}
print("</TABLE>\
");


Hi ParkinT,

I will be testing.

Thanks a lot!!!