基于MySQL的值更改形状颜色

问题描述 投票:0回答:2

有没有一种方法基于MySQL中的值,以动画或更改形状的颜色?我希望我的外形是:绿色,如果如果房间被占用的房间是空的红

<div id="wb_Shape6"   style="position:absolute;left:471px;top:94px;width:204px;height:183px;filter:alpha(opacity=90);opacity:0.90;z-index:7;">
<img src="images/img0012.png" id="Shape6" alt="" style="width:204px;height:183px;"></div>
php html
2个回答
1
投票

欢迎来到SO。有很多方法可以做到这一点。你需要弄清楚你的SQL查询将会是什么样的。既然你没有张贴了大量的数据,我们很多人会猜测。如果你可以添加更多的实例或代码到你的问题,我们可以帮助更多。

所以,我建议这样的:

<style>
.myBox {
  position:absolute;
  left:471px;
  top:94px;
  width:204px;
  height:183px;
  filter:alpha(opacity=90);
  opacity:0.90;
  z-index:7;
}

.redBox {
  background: red;
}

.greenBox {
  background: green;
}
</style>
<?php
// perform MySQL Query (MySQLi or PDO)
// get results: $result, make a decision based on the condition
// will use MySQLi for example
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($mysqli->connect_errno) {
  printf("Connect failed: %s\n", $mysqli->connect_error);
  exit();
}
if ($result = $mysqli->query("SELECT * FROM someTable;")) {
   while($row = $result->fetch_assoc()){
     if($row['room'] == "empty"){
       $colorClass = "greenBox";
     } else {
       $colorClass = "redBox";
     }
   }
   $result->free();
}
$mysqli->close();
?>
<div id="wb_Shape6" class="myBox <?php echo $colorClass; ?>">
  <img src="images/img0012.png" id="Shape6" alt="" style="width:204px;height:183px;">
</div>

0
投票

这被认为是一个基本条件的情况。有许多方法来处理它。在这里,我将不涉及如何将数据从数据源到来。假设你有一个$status变量命名,此变量有两个值01之一。

在您的例子中,你正在使用内联样式,所以我们可以做出一个内嵌条件类似如下:

<div style="background-color:<?php echo ($status)? 'green':'red'; ?>;"></div>
© www.soinside.com 2019 - 2024. All rights reserved.