Formatting PHP tables

Good day,

I have a HTML/CSS website.
Now it has been needed to add MySQL/PHP functionality.

To display data from the database, I am using tables:
print (“<TABLE>”); …

And then print (“<TR>”), print (“<TH>”), print (“<TD>”) …

Could you please help me to improve the table format?
How can I add styles/format to them?

Thanks a lot!!!

You would style it the same as any other section of your website: With CSS. You can print out ids and classes as well, if you need to…


print("<TABLE id=\\"myTable\\">");

Great!

Is itr possible you can let me know a couple of instructions to create ids or classes in CSS for <table> <tr> <th> and <td> tags?

Thanks!!

The nice thing about CSS is that you can apply almost any style to almost any element, I’m adding almost, but I’m quite certain that any style can be applied to any element… you can name these elements or you can apply the styles to all matching elements, for example you can name your table

<table id="mytable"> </table>

will create a table named mytable then in your CSS you could style it with

#mytable { width: 400px; }

or you could apply styling to all of the tables on your site with

table { width:400px; }

to style sub elements such as td, etc you could use

#mytable td { color: blue; }

or apply that to all td element using that style sheet with

td { color: blue; }

It is beneficial to get some education in CSS so that you can really understand it’s power, w3schools is a good place for free education about that and other similar topics.

Also a comment about PHP sometimes it is easier to type your code and even read it later to close the PHP tag for the HTML so instead of typing:

<?php
$x = "This is my table";
if ($x) {
    print("<table id=\\"mytable\\">");
    print("<tr><td>");
    print($x);
    print("</td></tr>");
    print("</table>");
}
?>

It might be easier to type, read and fix later:

<?php
$x = "This is my table";
if ($x) { ?>
    <table id="mytable">
        <tr>
             <td>
                 <?php
                     print($x);
                 ?>
             </td>
        </tr>
    </table>
<?php
}
?>

On a final note, is there a reason why you use print instead of echo? Same results but less typing:

print($x);
echo $x;

if you want print table with php…

<?php
echo ‘<table id=“tabletest”>’;
echo ‘<tr><td></td> <td></td> <td></td> </tr>’;
echo ‘<table>’;

?>

Hi Serj,

Thanks a lot for your great answer!!

About why print instead of echo … just because the code I used as starting point to build the table was like this … :slight_smile:
I will be changing it.

Thanks again!!!

Hi Serj,

I was testing …

Could you please let me know how should I re-write these sentences using echo?
print (“<TABLE $table_format>
“);
print(”<TH>$field_name</TH>”);

Thanks a lot!!!

Print is always slower than echo. Use echo whenever you need to. I also think it’s much faster if you did everything in one echo like so:


$tableView = "<table $table_format><th>$field_name</th>";
echo $tableView;

That porting of the string seems to be incomplete. You would want to finish defining that string before you echo it once.