所以我正在探索PHP并开发Web应用程序。我从我的数据库中显示一个表。但是,我希望每行显示不同的颜色。 例如:row1为绿色;第2行为蓝色; row3 in grey ......等等!
这是我的代码:
<?php
$username = 'root';
$pwd = '';
$db_name = 'fileattente';
$db = new mysqli('localhost', $username, $pwd, $db_name) or die("Unable to connect.");
?>
<table>
<tr>
<th><span class="Style1">SERVICE</span> </th>
<th><span class="Style2">NUMERO</span></th>
<th><span class="Style3">GUICHET</span></th>
<th><span class="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);
while ( $row = mysqli_fetch_array($stid1))
{
?>
<!--While true, do the following-->
<tr>
<td class="Style14"><span>
<?php echo $row['LIBESERV'];?>
</span></td>
<td><?php echo $row['CODESERV'];?> </td>
<td><blink><?php echo $row['CODEGUIC'];?></blink></td>
<td><?php echo $row['NOMBATTE'];?></td>
<?php
} //End of while loop
?>
这里已经看过其他一些类似的问题,但是他们在行之间交替使用了两种颜色,这不是我想要的。
提前致谢!
答案 0 :(得分:4)
以下是一些简单的代码,每次迭代都会显示不同的$cur_color
:
$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++;
}
只需将$cur_color
添加到tr/td
。
答案 1 :(得分:0)
尝试像这样的回声
<?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++;
}
?>
答案 2 :(得分:0)
您可以尝试此代码
^(?!CD|\d\d)
答案 3 :(得分:0)
这个PHP代码将生成一个彩虹色的表:
<?php
function hue2rgb($t)
{
if ($t < 0) $t += 1;
if ($t > 1) $t -= 1;
if ($t < 1/6) return 6 * $t;
if ($t < 1/2) return 1;
if ($t < 2/3) return (2/3 - $t) * 6;
return 0;
}
function color_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;
?>
<table style="width:100%">
<?php for ($i = 0; $i < $rows; $i++):?>
<tr style="background:<?=color_hex($i / $rows)?>">
<td>Row <?=$i + 1?></td>
</tr>
<?php endfor?>
</table>
答案 4 :(得分:-1)
技术#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)];
?>
然后在任何需要的地方回显$ color值。例如:
<body style="background: <?php echo $color; ?>;">
技术#2
<?php printf( "#%06X\n", mt_rand( 0, 0xFFFFFF )); ?>
我最喜欢的是:
<?php $color = sprintf("%02x%02x%02x", mt_rand(0x22, 0xaa), mt_rand(0x22, 0xaa), mt_rand(0x22, 0xaa)); ?>