简体   繁体   English

更改PHP表的背景颜色

[英]Changing background color of a php table

I have the following code: 我有以下代码:

while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
    echo "<td id='col1'>" . $row['inputId'] . "</td> <td id='col2'>" . $row['inputReqDate'] . "</td> <td id='col3'>" . $row['inputOrderDate'] . "</td> <td id='col4'>" . $row['inputPriority'] . "</td><td id='col5'>" . $row['inputDescription'] . "</td>" ;
  echo "</tr>";
}

I need to make it so when col4 contains the text "priority" it changes the background color for the whole row. 我需要这样做,以便当col4包含文本“ priority”时,它将更改整个行的背景颜色。 How would I go about doing this? 我将如何去做呢?

ps. PS。 Sorry I have not attempted any javascript, I am new to the language! 抱歉,我没有尝试过任何JavaScript,我是该语言的新手!

Why not just change your php to add the class to the row for you? 为什么不只更改您的php来为您的行添加类?

while($row = mysqli_fetch_array($result)) {
  echo "<tr" . ($row['inputPriority'] == 'Priority' ? ' class="priority"': '') . ">";

  echo "<td id='col1'>" . $row['inputId'] . "</td> <td id='col2'>" . $row['inputReqDate'] . "</td> <td id='col3'>" . $row['inputOrderDate'] . "</td> <td id='col4'>" . $row['inputPriority'] . "</td><td id='col5'>" . $row['inputDescription'] . "</td>" ;
  echo "</tr>";
}

Then you can just add the css for the row: 然后,您可以为该行添加CSS:

tr.priority {
  background-color: red;
}

You could check the value, and given a match, add a css class to the row that could be styled. 您可以检查该值,并指定一个匹配项,将CSS类添加到可以设置样式的行中。

while($row = mysqli_fetch_array($result)) {
  //New portion
  if($row['inputPriority'] == "priority"){
    echo "<tr class='priority'>";
  } else {
    echo "<tr>";
  }
  echo "<td id='col1'>" . $row['inputId'] . "</td> <td id='col2'>" . $row['inputReqDate'] . "</td> <td id='col3'>" . $row['inputOrderDate'] . "</td>";
  echo "<td id='col4'>" . $row['inputPriority'] . "</td><td id='col5'>" . $row['inputDescription'] . "</td>" ;
  echo "</tr>";
}

Also, I'm guessing that you will have multiple columns labeled with id col1 , col2 , etc. Id's are meant to be unique on the page, you should refactor to make these attributes as classes. 另外,我猜想您将有多个列,它们的标签为id col1col2等。id在页面上是唯一的,您应该重构以使这些属性成为类。

For example: 例如:

<td class='col1'>

You can just add it to the TR. 您可以将其添加到TR。

css: CSS:

.priority { background: red; }

PHP: PHP:

while($row = mysqli_fetch_array($result)) {
  echo "<tr  class='" . $row['inputPriority'] . "' >";
    echo "<td id='col1'>" . $row['inputId'] . "</td> <td id='col2'>" . $row['inputReqDate'] . "</td> <td id='col3'>" . $row['inputOrderDate'] . "</td> <td id='col4'>" . $row['inputPriority'] . "</td><td id='col5'>" . $row['inputDescription'] . "</td>" ;
  echo "</tr>";
}

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

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