SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict beebs93's Avatar
    Join Date
    Jul 2010
    Location
    Vancouver, Canada
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Alternating background colors: JS v PHP

    Out of pure curiosity:

    Assuming tabulated data in HTML is being generated by a PHP script, would it be better to alternate each row's background color with PHP or rely on JS to do it?

    One is run client-side and the other server-side so my guess you have to take the whole web app as a whole and see which side should be spared the additional workload. Is it situational or is there an absolute rule?

    In a vacuum, I guess you could argue PHP would be better since it cannot be turned off in the way JS can (unless plugins like NoScript can block PHP as well), but I don't have enough experience with either to be sure.

    Thanks
    "To be truly dedicated to something
    you must be willing to betray it.
    " -SW

  2. #2
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    with tabulated html data generated by php I use css to alternate each rows background color by using php to give each row alternate class names and then setting the css to whatever.

    eg - alternate the class for each row between something like class='oddRow' and class='evenRow'

  3. #3
    SitePoint Addict beebs93's Avatar
    Join Date
    Jul 2010
    Location
    Vancouver, Canada
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    eg - alternate the class for each row between something like class='oddRow' and class='evenRow'
    Do you actually use two class names for each or have a default row color and use a single additional class to define the alternating color?

    I'm probably splitting hairs, but if you have a large table of data then wouldn't the extra bytes from typing class='evenRow' make it a bit bloated?
    "To be truly dedicated to something
    you must be willing to betray it.
    " -SW

  4. #4
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in my loop that generates each row I would have something similar to this

    Code:
     
    $rowClass = ($rowClass == 'oddRow')? 'evenRow' : 'oddRow';    //this toggles the class name for each row
     
    echo '<tr class="'.$rowClass.'">';
    and then set the styles for each class name.

  5. #5
    SitePoint Addict beebs93's Avatar
    Join Date
    Jul 2010
    Location
    Vancouver, Canada
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    in my loop that generates each row I would have something similar to this

    Code:
     
    $rowClass = ($rowClass == 'oddRow')? 'evenRow' : 'oddRow';    //this toggles the class name for each row
     
    echo '<tr class="'.$rowClass.'">';
    and then set the styles for each class name.
    Understood. Ha, I de-railed my own thread

    So you would advocate using PHP in all circumstances and leaving JS alone?
    "To be truly dedicated to something
    you must be willing to betray it.
    " -SW

  6. #6
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beebs93 View Post
    Understood. Ha, I de-railed my own thread

    So you would advocate using PHP in all circumstances and leaving JS alone?
    For this type of scenario - yes.

    You can do it with JS but then if it's critical to have alternate row colours then you will need a Plan B for those, albeit very small number, who have JS turned off for whatever reason.

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,184
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    You only need a odd or even class, but not both. Both is not really necessary considering the default can be the even than you create a more specific selector for the odd, targeting the odd class. Nothing really to split hairs over, just something to consider for the next project. Also, using something like even or odd is a little nicer than evenRow or oddRow. Both oddRow and evenRow only restate the obvious holding no actual purpose. I always use odd, myself.

  8. #8
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need both where both the oddRow and evenRow styles are different to the default style.

    Whether you use oddRow/evenRow or just odd/even is a personal choice. I prefer to use the more descriptive class name so I can easily distinguish it from say odd and even list items, for example, in the stylesheet.


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
  •