Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: PHP Change Table BG Color

  1. #11
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Well, the delay happens because the script waits for the page to finish loading (the document.ready state) before changing the color of the table.

    You could work around this by inserting the script on the next line that the table is made.

    So you would have something like this:
    Code:
    <table width='400' border='1' id='table_id'>
      <tr>
        <th scope='col'>ID #1: $id1</th>
      </tr>
    </table>
    <script type="text/javascript">document.getElementById('table_id').style.backgroundColor = '<?php echo $bg2; ?>';
    </script>
    And, while it wouldn't completely eliminate a delay, it would come very close to it.

  2. #12
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Why not just do this with css?
    Corrections to my coding/thoughts welcome.

  3. #13
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    You could, but calling CSS outside of the <head> tags is generally not a good idea (and causes problems in some browsers).

  4. #14
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    The css can be in the head, are the text files building the whole page, or just parts of it? I don't see any html in the example provided, seeing the whole page layout would be the easiest.

    for example

    PHP Code:
    <?php
    //php stuff here
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
    <head>
    <?php
    echo $define_stuff;
    ?>
    </head>
    <body>
    <h1>Welcome</h1>
    <?php
    echo $php_generated_content;
    ?>
    </body>
    </html>
    Corrections to my coding/thoughts welcome.

  5. #15
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    But he's already defining the bgcolor attribute when the table is being created, which would overwrite the CSS value.

  6. #16
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Yea, but he doesn't need to define it there he could do it conditionally in the css with the php.
    Last edited by bluewalrus; 08-01-2011 at 04:45 AM. Reason: flip flopped words, php would control the css
    Corrections to my coding/thoughts welcome.

  7. #17
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I agree with bluewalrus. This is why pre-processing a page then doing the output is best. If that's not possible, it's at least best to do any setup like this (that is-- checking what kind of format you will need) and to be able to output some special (PHP-generated, dynamic, conditional) CSS at the top.

    It may take redesigning the page some but it's much better in the end. Your choice.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #18
    Join Date
    Jul 2011
    Posts
    56
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default

    Thanks guys for your help!

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
  •