【问题标题】:Print each row with a different color PHP用不同的颜色打印每一行 PHP
【发布时间】:2016-10-15 02:05:32
【问题描述】:

所以我正在探索 PHP 并开发一个 Web 应用程序。我从我的数据库中显示一个表。但是,我希望每一行都以不同的颜色显示。 例如:row1 为绿色;第 2 行为蓝色;灰色的第 3 行...等等!

这是我的代码:

<?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
?>

已经在这里查看了其他一些类似的问题,但它们在行之间交替使用 2 种颜色,这不是我想要的。

提前致谢!

【问题讨论】:

  • 所以在任意数量的颜色之间交替。创建颜色数组并使用它。
  • @u_mulder 请问我该怎么做?我还在学习PHP,非常感谢!
  • 为什么不使用带有rand(0,255);的随机颜色生成器
  • @Baklap4 我想显示的颜色不是随机的!每行都有颜色编码!
  • @jungkookie 好吧没有从你的问题中得到。那么 u_mulder 给你的答案就可以了!

标签: php html colors


【解决方案1】:

这里有一些简单的代码,每次迭代都会显示不同的$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 的样式中即可。

【讨论】:

  • 应该可以,但不是...我添加了以下内容: 是否正确?
  • 如前所述style='color:red'style='background-color:red'
【解决方案2】:

你可以试试这个代码

<?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);
  $records = mysqli_fetch_array($stid1)
  $colors = array('red', 'green', 'blue', 'yellow', 'black');
  for($i=0; $i<$count($records); $i++){
  ?>
    <tr style="background-color: <?php echo $colors[$i]; ?>">
      <td><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>
    </tr>
  <?php
  }
  ?>

【讨论】:

  • 非常感谢!我终于得到了我想要的结果!!此代码有效!
  • 有一天我会把我的房间粉刷得漂亮明亮Undefined offset
【解决方案3】:

试试这样的回声

<?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++;
}


?>

【讨论】:

  • 不同的颜色在哪里?
  • @Dest - 重复问题的标题:“用不同颜色的 PHP 打印每一行”...
  • @Dest ,是的!这就是我想要的!
  • @Dest 是的,您的回答现在符合我的问题,谢谢! ^^
【解决方案4】:

此 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>

【讨论】:

    【解决方案5】:

    技术#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)); ?>
    

    来源:https://css-tricks.com/snippets/php/random-hex-color/

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签