SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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!!!

  2. #2
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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...

    PHP Code:
    print("<TABLE id=\"myTable\">"); 
    I'm the web overlord for Graphic Business Systems

  3. #3
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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!!

  4. #4
    SitePoint Member Serj's Avatar
    Join Date
    Nov 2011
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Code:
    <table id="mytable"> </table>
    will create a table named mytable then in your CSS you could style it with
    Code:
    #mytable { width: 400px; }
    or you could apply styling to all of the tables on your site with
    Code:
    table { width:400px; }
    to style sub elements such as td, etc you could use
    Code:
    #mytable td { color: blue; }
    or apply that to all td element using that style sheet with
    Code:
    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:
    Code:
    <?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:

    Code:
    <?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:
    Code:
    print($x);
    echo $x;

  5. #5
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you want print table with php...........

    <?php
    echo '<table id="tabletest">';
    echo '<tr><td></td> <td></td> <td></td> </tr>';
    echo '<table>';

    ?>

  6. #6
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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 ...
    I will be changing it.

    Thanks again!!!

  7. #7
    SitePoint Addict
    Join Date
    May 2011
    Posts
    242
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi Serj,

    I was testing ...

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

    Thanks a lot!!!

  8. #8
    SitePoint Member
    Join Date
    May 2011
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    PHP Code:
    $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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •