53468

Alternating table row background colors on a dynamic table

Question:

I have a table generated by a foreach loop.

Before the loop, I create $iteration = 0. At the beginning of the loop it increments $iteration.

Then I do this:

if($iteration % 2 == 0) { $greyRow = 'css for a grey row'; }

I've only three rows, at max, to test this with, but it seems that it greys the 2nd and 3rd row under the current rule, rather than only the second.

Answer1:

Perhaps you also need to have an else statement to set back to the other color on the odd rows. Not quite sure without seeing more of your implementation.

if ($iteration % 2 == 0) { $css = 'css for a grey row'; } else { $css = 'css for a white row'; }

Answer2:

This following will add a grey class for even rows.

echo '<tr', ($iteration & 1 ? ' class="grey"': ''), '>'; <h3>Note</h3>

This uses a <a href="http://php.net/manual/en/language.operators.bitwise.php" rel="nofollow">bitwise operator</a>. It's a micro-optimization but far more optimal than the modulus operator (although notoriously used in these cases).

Also, I would encourage you to utilize CSS classes for even and odd rows versus inline styles or just toggling a single class (i.e. grey).

echo '<tr class="', ($iteration & 1 ? 'even': 'odd'), '">';

Answer3:

I like to do this kind of thing..

<style type="text/css"> .odd td { background: #eee; } </style> <tr class="<?= ++$iteration % 2? 'odd' : 'even'; ?>">....</tr>

Recommend

  • Check all commands exit code within a bash script
  • How do I prepend to a stream in Bash?
  • manupulating a string to create directories in unix
  • Zeromq with python hangs if connecting to invalid socket
  • Getting syntax error in mysql-php. You have an error in your SQL syntax;
  • removing last 3 characters on a file (file extension)
  • F#: In which memory area is the continuation stored: stack or heap?
  • .NET video play library which allows to change the playback rate?
  • How to 'create temp table as select' in Slick?
  • Installing PHP 7 on digitalocean
  • Get specific string
  • RxJava debounce by arbitrary value
  • Apache RewriteRule redirection with url encoded
  • How does this usort cmp function actually work?
  • D3 get axis values on zoom event
  • MySQL Order by column = x, column asc?
  • Stop Bash Script if Hive Fails
  • C: Incompatible pointer type initializing
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • JSON with duplicate key names losing information when parsed
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Weird JavaScript statement, what does it mean?
  • Jquery - Jquery Wysiwyg return html as a string
  • How to get next/previous record number?
  • Invalid access key error using credentials redeemed from an amazon open id token
  • PHP: When would you need the self:: keyword?
  • Acquiring multiple attributes from .xml file in c#
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Android Google Maps API OnLocationChanged only called once
  • Qt: Run a script BEFORE make
  • reshape alternating columns in less time and using less memory
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?