0

this is my code for a multiplication table. By combining PHP and HTML/CSS I need to make every alternating rows background color white or grey.

I've thought of using a row and using %2==1 and %2==0 to figure out the odd and even rows but how would i target those rows to make a id or class to target on my css file? Where would I insert this code at?

<?php
    echo '<table>';
    echo '<tr><th></th>';
    for ($x = 1; $x <= 9; $x++)
        echo '<th>'.$x.'</th>';
    echo '</tr>';
    for ($y = 1; $y <= 9; $y++)
    {   
        echo '<tr><th>'.$y.'</th>';
        for ($z = 1; $z <= 9; $z++)
        {   
            echo '<td>'.($y * $z).'</td>';
        }
        echo '</tr>';
    }
    echo '</table>';
?>
5
  • 3
    td:nth-child(even) { background: #f5f5f5 } td:nth-child(odd) { background: #ddd } Commented Jan 28, 2014 at 19:39
  • @mdesdev You again! You should make this an answer, it's the best way to go about it. Commented Jan 28, 2014 at 19:41
  • @DrydenLong hahaha I know, I'm doing something right now, busy, just checking from time to time new questions and making comments. Take it and post it if you wish, elaborate a little ;) Commented Jan 28, 2014 at 19:46
  • @mdesdev SO should make team accounts haha Commented Jan 28, 2014 at 19:46
  • @DrydenLong hahahahahaha true ;) Commented Jan 28, 2014 at 19:47

3 Answers 3

2

No need to assign classes with PHP when CSS can style alternate elements just fine. Using nth-child you can style alternative rows pretty easily.

tr:nth-child(even) { 
    background: #f5f5f5;
} 
tr:nth-child(odd) { 
    background: #ddd;
}

Here is a fiddle showing it off: Fiddle

And here is more info on the nth-child selector: https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child

Sign up to request clarification or add additional context in comments.

Comments

0

As Dryden Long comments, best is CSS, but if you do need do it with php, you could do:

<?php
echo '<table>';
echo '<tr><th></th>';
for ($x = 1; $x <= 9; $x++)
    echo '<th>'.$x.'</th>';
echo '</tr>';
for ($y = 1; $y <= 9; $y++)
{   
    echo '<tr class="'. ( ($y%2 == 0 ) ? 'even' : 'odd' ).'"><th>'.$y.'</th>';
    for ($z = 1; $z <= 9; $z++)
    {   
        echo '<td>'.($y * $z).'</td>';
    }
    echo '</tr>';
}
echo '</table>';

and then you only have to add the CSS classes, and all will be working.

table tr.even { background-color: blue }
table tr.odd  { background-color: red }

1 Comment

The whole point was to practice html/css manipulation with php. This was very helpful, and I also learned the use of ternary conditionals from it. Thanks!
0
echo '<tr class ="' . ( $y % 2 == 0 ? 'even' : 'odd' ) . '"><th>'.$y.'</th>';

You can use the ternary operator to insert a class

condition ? if true : if false;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.