简体   繁体   English

PHP While-每次都会改变颜色

[英]PHP While - Color change every time

I have a PHP while loop, which displays the content of a database, in a table. 我有一个PHP while循环,它在一个表中显示数据库的内容。 I would like to make all the rows in the table different colors. 我想将表中的所有行设置为不同的颜色。

But not just random. 但不仅仅是随机的。 I would like them have different shades of red, where the first row is most red, and then the rest fades into a lighter red. 我希望它们具有不同的红色阴影,第一行的颜色最红色,然后其余的颜色逐渐淡化为红色。

As an exemple, I would like the same effect as the iPhone App: Clear. 例如,我想要与iPhone App相同的效果:清除。 Photo example: http://www.realmacsoftware.com/_resources/clear/images/ss_pinch.png 照片示例: http : //www.realmacsoftware.com/_resources/clear/images/ss_pinch.png

I've tried to make the effect with -nth in CSS and some jQuery, but just couldn't figure it out. 我已经尝试在CSS和jQuery中使用-nth来实现效果,但无法弄清楚。 I hope you will give it a try. 希望您能尝试一下。

Here is the code: 这是代码:

<table>


<tr>
<td align="center"><strong>Navn:</strong></td>
<td align="center"><strong>Spørgsmål:</strong></td>
<td align="center"><strong>Tid:</strong></td>

</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['name']; ?></td>
<td><? echo $rows['ask']; ?></td>
<td><? echo $rows['time']; ?></td>


<?php
}
?>

</tr>
</table>

You have to apply css in table row. 您必须在表格行中应用CSS。

<div class="grad">
<table>
<tr>
<td align="center"><strong>Navn:</strong></td>
<td align="center"><strong>Spørgsmål:</strong></td>
<td align="center"><strong>Tid:</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['name']; ?></td>
<td><? echo $rows['ask']; ?></td>
<td><? echo $rows['time']; ?></td>
</div>
 <?php
 }
?>
</tr>
</table>


<script>

//css Part
.grad
{
   background: -webkit-gradient(linear, left top, left bottom, from(#ccc), to(#000));
}
</script>

If you need a different color for each element, then a static CSS file is not able to describe that. 如果每个元素需要不同的颜色,则静态CSS文件无法描述该颜色。 You could generate a CSS file on the fly, but I opt for inline styles instead. 您可以动态生成CSS文件,但是我选择了内联样式。 Don't use inline styles if you can use a static CSS file. 如果可以使用静态CSS文件,请不要使用内联样式。

For a simple linear interpolaction from red to white, adhering to your coding style. 从红色到白色的简单线性插值,遵循您的编码风格。 I've also removed an extra </div> that didn't belong there: 我还删除了一个不属于此的</div>

<?php
$num_rows=mysql_num_rows($result);
$cur_row=0;
while($rows=mysql_fetch_array($result)){
$color=intval(256*$cur_row/($num_rows-1));
$cur_row++;
?>
<tr style="background:rgb(256,<? echo $color.','.$color;>);">
<td><? echo $rows['name']; ?></td>
<td><? echo $rows['ask']; ?></td>
<td><? echo $rows['time']; ?></td>
<?php
}
?>

You can also play around with Javascript and HSV (which is a different 'color coordinate system'). 您还可以尝试使用Javascript和HSV(这是一个不同的“颜色坐标系”)。

Jsfiddle demo: http://jsfiddle.net/QXLRg/7/ Jsfiddle演示: http : //jsfiddle.net/QXLRg/7/
Another demo: http://jsfiddle.net/QXLRg/8/ 另一个演示: http : //jsfiddle.net/QXLRg/8/
and another: http://jsfiddle.net/QXLRg/10/ 另一个: http//jsfiddle.net/QXLRg/10/

(All I did, just change the 'h' value..) (我所做的只是更改'h'值。)

var h = 0.4;
var s = 0.9;
var v = 0.95;

So, if you want to programatically create different colors then it's much better to use HSV instead of raw RGB, cause you can fine control for example 'the color mildness' and much more things.. as you can see in the above example 因此,如果您要以编程方式创建不同的颜色,那么最好使用HSV而不是原始RGB,因为您可以精细控制例如“颜色柔和度”等更多内容,如上例所示。

<?php
$colors = array("yellow","red","green","silver");
$i=0;
while($rows=mysql_fetch_array($result)){
?>
<tr bgcolor="<?php echo $colors[$i]; ?>">
<td><? echo $rows['name']; ?></td>
<td><? echo $rows['ask']; ?></td>
<td><? echo $rows['time']; ?></td>


<?php
$i<3?$i++:$i=0;
}
?>

replace like this if you want to rgb color mode then change color name to rgb scale such as rgb(100,100,100) 如果要使用rgb颜色模式,请像这样替换,然后将颜色名称更改为rgb比例,例如rgb(100,100,100)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM