简体   繁体   English

突出显示或着色使用PHP和Mysql获取的表中特定行的值

[英]Highlight or color the values of a specific row in table fetched using PHP and Mysql

I am a beginner to PHP. 我是PHP的初学者。 I am looking to highlight or color the values of first and last row in a table fetched using PHP and Mysql. 我想突出显示或着色使用PHP和Mysql获取的表中第一行和最后一行的值。 预期产量

Below is the code. 下面是代码。

 <?php
  $result_trend = mysql_query("select Week, New_Tickets, Closed_Tickets, 
  Backlogged, Resolution_Rate, Transactions from table");
  while($row = mysql_fetch_array($result_trend))
  {
  echo "<tr>";
  echo "<td>" . $row['Week'] . "</td>";
  echo "<td>" . $row['New_Tickets'] . "</td>";
  echo "<td>" . $row['Closed_Tickets'] . "</td>";
  echo "<td>" . $row['Backlogged'] . "</td>";
  echo "<td>" . $row['Resolution_Rate'] . "</td>";
  echo "<td>" . $row['Transactions'] . "</td>";
  }
  echo "</tr>";
  ?>

I am not really sure how to proceed further. 我不确定如何进一步进行。 Any suggestion is much appreciated. 任何建议深表感谢。 Thanks in advance. 提前致谢。

You can define a CSS style for the first and last rows as below: 您可以为第一行和最后一行定义CSS样式,如下所示:

 .my-table tbody tr:first-child { background-color: yellow; } 
 <table class="my-table"> <thead> <tr> <td>Col 1</td> <td>Col 2</td> </tr> </thead> <tbody> <tr> <td>First</td> <td>Row</td> </tr> <tr> <td>Second</td> <td>row</td> </tr> <tr> <td>Third</td> <td>row</td> </tr> </tbody> </table> 

You can simply obtain number of results and then count iterations. 您可以简单地获取结果数量,然后计算迭代次数。 And if this is 1. row or last, add the style you need or css class that you'll style in your css file. 如果这是1.行或最后一行,则在css文件中添加所需样式或css类。 Like this: 像这样:

 <?php
  $result_trend = mysql_query("select Week, New_Tickets, Closed_Tickets, 
  Backlogged, Resolution_Rate, Transactions from table");
  $rows_count = mysql_num_rows($result_trend);
  $i = 0;
  while($row = mysql_fetch_array($result_trend))
  {
  $i++;
  $style = "";
  if($i == 1 || $i == $rows_count) {
  //  $style .= "bgcolor=\"#FF0000\""   //for inline style, bad practise
      $style .= "class=\"colorful\""
  }
  echo "<tr " . $style . ">";
  echo "<td>" . $row['Week'] . "</td>";
  echo "<td>" . $row['New_Tickets'] . "</td>";
  echo "<td>" . $row['Closed_Tickets'] . "</td>";
  echo "<td>" . $row['Backlogged'] . "</td>";
  echo "<td>" . $row['Resolution_Rate'] . "</td>";
  echo "<td>" . $row['Transactions'] . "</td>";
  }
  echo "</tr>";
  ?>

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

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