I am working on a code which has to change the background-color of the page or later a div red if a value is negative or green if a value is positive.
I have a code which is calculating right and a css which get some manipulations from a PHP-file.
But my file doesn't change the background-color. And I don't understand why. Maybe anyone could help, eventually explain what went wrong?
First: table quotation last two rows of price are 32 and 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>
In fact page should be red because 32 > 30 and so (32 - 30) > 0, but it is green. But what went wrong?
Anyone who has an idea? Thanks to everyone who tries to help.
Not entirely sure, but try using integers for 0
instead of strings:
if ($result>0){
$background = "red";
$color = "white";
} elseif ($result<0) {
$background = "green";
$color = "white";
}
else {
$background = "white";
$color = "black";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.