0

I am new to PHP and I want to display a table from my database with each row with a different color from other rows and I tried answers and solutions similar to my question but I failed to make it done on the rows of <td><?= $field ?></td>

This my script :

<?php mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = mysqli_connect('localhost','root','','class'); $mysqli->query("set names 'UTF8'"); $data = []; $res = $mysqli->query("SELECT math, physics, english FROM student order by math desc"); while ($row = $res->fetch_assoc()) { foreach(array_keys($row) as $key) { $data[$key][] = $row[$key]; } } ?> <div>student</div> <table border="1"> <?php foreach($data as $key => $val): ?> <tr> <td><?= $key ?></td> <?php foreach($val as $field): ?> <td><?= $field ?></td> <?php endforeach ?> </tr> <?php endforeach ?> </table> 
3
  • The duplicate I posted was more a case of alternate rows rather than a different color in each row. Commented Jun 7, 2020 at 18:00
  • After how many records should the colours repeat? If you had 16,700,001 records admittedly the universe might have ended before the browser rendered the recordset but ... ?? Commented Jun 7, 2020 at 18:25
  • @RamRaider I am just learning PHP I want for example first row of math ,physics ,english to be yellow and second row in a different color just to make the differance visible I want something like this answer but for my problem . Commented Jun 7, 2020 at 18:31

2 Answers 2

1

You can do this with the help of CSS

For even and odd selectors you follow following syntax

table tr:nth-child(even) td{ background: #f1f1f1; } table tr:nth-child(odd) td{ background: #fff; } 
Sign up to request clarification or add additional context in comments.

Comments

0

I'm just using the following function as an example which returns a random color. The function has been posted by @outis

function rand_color() { return '#' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT); } 

This will return a random RGB hex string which you can use in your foreach loop, like this (note the <tr> tag):

<div>student</div> <table border="1"> <?php foreach($data as $key => $val): ?> echo '<tr style="background-color:' . rand_color() . '">'; <td><?= $key ?></td> <?php foreach($val as $field): ?> <td><?= $field ?></td> <?php endforeach ?> </tr> <?php endforeach ?> </table> </div> 

If you just want to use specifiek colors, then i suggest to define those colors in an array, and then use those as you loop through your data.

1 Comment

Sir thanks for you replay , your solution did not work for me probably because I am just learning PHP and I could not figure out how to use it but how can I have specific colors using array I tried this answer but the result I get was just one color in all rows so How can I use the idea for this answer on my problem to get specific colors and thanks for your help .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.