Print Each Row With A Different Color Php
So i'm exploring PHP and working on a web app. I display a table from my database. However, i want each row to be displayed in a different color. For example: row1 in green; row2 i
Solution 1:
Here's some simple code that will show different $cur_color
on each iteration:
$colors = array('red', 'green', 'blue', 'yellow', 'black');
$i = 0;
while ($row = mysqli_fetch_array($stid1)) {
$cur_color = $colors[$i % count($colors)];
echo'$cur_color is ' . $cur_color;
$i++;
}
Just add $cur_color
to the style of your tr/td
.
Solution 2:
You can try this code
<?php$username = 'root';
$pwd = '';
$db_name = 'fileattente';
$db = new mysqli('localhost', $username, $pwd, $db_name) ordie("Unable to connect.");
?><table><tr><th><spanclass="Style1">SERVICE</span></th><th><spanclass="Style2">NUMERO</span></th><th><spanclass="Style3">GUICHET</span></th><th><spanclass="Style4">EN ATTENTE</span></th></tr><?php$req_service_1="SELECT `LIBESERV`, `CODESERV`, `CODEGUIC` , `NOMBATTE` FROM `v_attente_service`";
$stid1= mysqli_query($db, $req_service_1);
$records = mysqli_fetch_array($stid1)
$colors = array('red', 'green', 'blue', 'yellow', 'black');
for($i=0; $i<$count($records); $i++){
?><trstyle="background-color: <?phpecho$colors[$i]; ?>"><td><span><?phpecho$row['LIBESERV'];?></span></td><td><?phpecho$row['CODESERV'];?></td><td><blink><?phpecho$row['CODEGUIC'];?></blink></td><td><?phpecho$row['NOMBATTE'];?></td></tr><?php
}
?>
Solution 3:
Try echo like this
<?php$colors = Array("bfbfbf","cccc","fbfbff","00ffff","bffbff");
$x = 0;
while ( $row = mysqli_fetch_array($stid1))
{
if(!isset($colors[$x])){
$x = 0; //if color will less then records it will start from 0 again
}
echo"<tr>
<td style='color:#".$colors[$x]."'><span>".$row['LIBESERV']."</span></td>
<td style='color:#".$colors[$x]."'>".$row['CODESERV']."</td>
<td style='color:#".$colors[$x]."'><blink>".$row['CODEGUIC']."</blink></td>
<td style='color:#".$colors[$x]."'>."$row['NOMBATTE']."</td>
</tr>";
$x++;
}
?>
Solution 4:
This PHP code will generate a table with rainbow colors:
<?phpfunctionhue2rgb($t)
{
if ($t < 0) $t += 1;
if ($t > 1) $t -= 1;
if ($t < 1/6) return6 * $t;
if ($t < 1/2) return1;
if ($t < 2/3) return (2/3 - $t) * 6;
return0;
}
functioncolor_hex($hue)
{
return sprintf('#%02x%02x%02x',
round(255 * hue2rgb($hue + 1/3)),
round(255 * hue2rgb($hue)),
round(255 * hue2rgb($hue - 1/3))
);
}
$rows = 100;
?><tablestyle="width:100%"><?phpfor ($i = 0; $i < $rows; $i++):?><trstyle="background:<?=color_hex($i / $rows)?>"><td>Row <?=$i + 1?></td></tr><?phpendfor?></table>
Solution 5:
Technique #1
<?php$rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
$color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
?>
Then echo out the $color value anywhere you need it. For example:
<bodystyle="background: <?phpecho$color; ?>;">
Technique #2
<?php printf( "#%06X\n", mt_rand( 0, 0xFFFFFF )); ?>
My favorite one is:
<?php$color = sprintf("%02x%02x%02x", mt_rand(0x22, 0xaa), mt_rand(0x22, 0xaa), mt_rand(0x22, 0xaa)); ?>
Source: https://css-tricks.com/snippets/php/random-hex-color/
Post a Comment for "Print Each Row With A Different Color Php"