1

I am trying to add colors on an 2 dimensional array to look like this: https://i.sstatic.net/gi79o.jpg

What I have created right now is this : https://i.sstatic.net/EzlQT.jpg

My code right now :

<html>
<head>
<title>Two-dimensional Arrays</title>
</head>

<body>
<h1>Two-Dimensional Arrays</h1>

<?php
echo "<table border =\"1\" style='border-collapse: collapse'>";
    for ($row=1; $row <= 10; $row++) { 
        echo "<tr> \n";
        for ($col=1; $col <= 10; $col++) { 
           $p = $col * $row;
           echo "<td>$p</td> \n";
            }
            echo "</tr>";
        }
        echo "</table>";
        
        
?>

</body>
</html>

5
  • 1
    where is the part that you try to add the color? Commented Dec 1, 2019 at 14:01
  • Thank you for the question, I tried it with CSS and adding ids to the tr, td, however it doesn't work as I get an error. This is it: imgur.com/a/PaIAp48 Commented Dec 1, 2019 at 14:24
  • Please embed images to be viewed directly by simply putting an exclamation-mark before the image-link. Commented Dec 1, 2019 at 17:40
  • Noted for the next post, thanks you Commented Dec 1, 2019 at 18:12
  • I notted I can't embed images because I have <15 reputation as now. Commented Dec 1, 2019 at 18:23

1 Answer 1

0

I'd recommend using CSS:

<style>
table tbody tr:nth-child(odd) {
    background-color: red;
}
table tbody tr:nth-child(even) {
    background-color: green;
}
</style>

But if you want to do it in PHP, you can use inline styling:

<table border="1" style="border-collapse: collapse;">
<?php
    for ($row = 1; $row <= 10; $row++) { 
        echo '<tr style="background-color: ' . ($row % 2 === 0 ? 'green' : 'red') . ';">';

        for ($col = 1; $col <= 10; $col++) { 
           $p = $col * $row;

           echo "<td>$p</td> \n";
        }

        echo '</tr>';
    }   
?>
</table>
Sign up to request clarification or add additional context in comments.

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.