为什么它不会像它应该那样改变背景颜色?

时间:2014-02-24 23:17:30

标签: php html css

我正在处理一个代码,该代码必须更改页面的背景颜色,如果值为负数则稍后为红色,如果值为正则为绿色。

我有一个正在计算的代码和一个从PHP文件中获取一些操作的css。

但我的文件不会改变背景颜色。我不明白为什么。也许任何人都可以提供帮助,最终解释出了什么问题?

第一: 表格报价最后两行的价格是32和30.

style.css.php

<?php
$con=mysqli_connect("localhost","root","","boerse");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"  SELECT SUM(CASE WHEN rnum = 2 THEN -1 * price ELSE price END) diff_value,
       ROUND(SUM(CASE WHEN rnum = 2 THEN -1 * price ELSE price END) /
             SUM(CASE WHEN rnum = 1 THEN 0 ELSE price END) * 100) diff_percent
  FROM
(
  SELECT id, stock_id, price, @n := @n + 1 rnum
    FROM quotations CROSS JOIN (SELECT @n := 0) i
   ORDER BY date DESC
   LIMIT 2
) q");

//$chnge = mysqli_fetch_array($result))


  header('Content-type: text/css');

  if ($result>"0"){
    $background = "red";
    $color = "white";
  } elseif ($result<"0") {
    $background = "green";
    $color = "white";
  }
    else {
    $background = "white";
    $color = "black";
  }

mysqli_close($con);
?>

body {
  background-color: <?=$background?>;
  color: <?=$color?>;
}

tiat2.php

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css.php">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
    $("#responsecontainer").load("getdata.php");
    var refreshId = setInterval(function() {
        $("#responsecontainer").load('getdata.php?randval=2');
    }, 5000);
});
</script>
</head>
<body>

<div id="responsecontainer">
</div>

</body>
</html>

实际上页面应该是红色的,因为32&gt; 30等等(32-30)> 0,但它是绿色的。但出了什么问题呢?

任何有想法的人?感谢所有试图提供帮助的人。

1 个答案:

答案 0 :(得分:0)

不完全确定,但尝试使用0的整数而不是字符串:

  if ($result>0){
    $background = "red";
    $color = "white";
  } elseif ($result<0) {
    $background = "green";
    $color = "white";
  }
    else {
    $background = "white";
    $color = "black";
  }