用不同颜色的PHP打印每一行 [英] Print each row with a different color PHP

查看:46
本文介绍了用不同颜色的PHP打印每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 in blue; row3 in grey... And so on!

这是我的代码:

<?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种颜色,这不是我想要的.

Already looked through some other similar questions here, but they alternate 2 colors between rows, and that is not what i want.

提前谢谢!

推荐答案

您可以尝试使用此代码

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

这篇关于用不同颜色的PHP打印每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆