【问题标题】:displaying data from mysql where if value is in database show true if its not show false显示来自 mysql 的数据,如果值在数据库中,则显示 true,如果不显示 false
【发布时间】:2018-07-17 10:15:57
【问题描述】:

我的总体目标是如果有一个价值拉入 ID,然后更改 ID 与数字匹配的单元格

例如以下数据已被拉入更改单元格 1,120,17,40,61,69,70,81,82,9 绿色,其余为红色。

如果有什么我需要更改 mysql 端然后让我知道我确实尝试过它以便它从节点 red 输入到数据库中来说明 mqtt 提要中是否存在 1-100 之间的数字添加值存在如果它不在提要中,则存在列添加值 false,但我无法让它工作。

数据

[{"id":"1","present":"present","date":"2018-07-17","time":"10:53:20"},
{"id":"120","present":"present","date":"2018-07-17","time":"10:54:24"},
{"id":"17","present":"present","date":"2018-07-17","time":"10:53:40"},
{"id":"40","present":"present","date":"2018-07-17","time":"10:53:27"},
{"id":"61","present":"present","date":"2018-07-17","time":"10:53:14"},
{"id":"69","present":"present","date":"2018-07-17","time":"11:02:01"},
{"id":"70","present":"present","date":"2018-07-17","time":"10:53:17"},
{"id":"81","present":"present","date":"2018-07-17","time":"10:56:50"},
{"id":"82","present":"present","date":"2018-07-17","time":"10:59:28"},
{"id":"9","present":"present","date":"2018-07-17","time":"10:53:15"}]

PHP

<?php
header('Content-type: application/json');
$servername = "localhost:3306";
$username = "user";
$password = "password!";
$dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
#   header('Content-Type: applicaton/json');

$sql = 'SELECT 
* 
FROM test.test where time > NOW() - interval 30 minute group by id';
$result = $conn->query($sql);


$result = mysqli_query($conn ,  $sql);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
  $rows[] = $r;
}

echo json_encode($rows);
$conn->close();
?>

HTML 方面

  <script>    
$(document).ready(function() {
    for (var i = 0; i < 12; i++) {
        var row = $('<tr>').appendTo("#zoning tbody");
        for (var j = 1; j < 11; j++) {
            $(`<td class='${i * 10 + j}'>${i * 10 + j}</td>`).appendTo(row);
        }
    }

    $.get('php/test.php', function(response) {
        console.log(response);
        var row;
        response.forEach(function(item, index) {
            console.log(item);
    if (item.id == "notNULL") {
         return $('td.coloured').css('background-color','green').toggleClass('coloured');
        } else {
          return $('td.coloured').css('background-color','red').toggleClass('coloured');
        }            });
    });


        function updateTable() {
        //console.log('function called');
        $('td.coloured').css('background-color','#8F8F8F').toggleClass('coloured');
        $.get('php/test.php', function(response) {
            response.forEach(function(item, index) {
                console.log(item.beacon);
                //$('td.coloured').css('background-color','#8F8F8F').toggleClass('coloured');
    if (item.id == "notNULL") {
         return $('td.coloured').css('background-color','green').toggleClass('coloured');
        } else {
          return $('td.coloured').css('background-color','red').toggleClass('coloured');
        }                
    });
        });

    }
    var updateTableInterval = setInterval(updateTable, 40000);
});
</script>
</head>
<body>

<table id='zoning'>
    <tbody></tbody>
</table>

</body>
</html>

【问题讨论】:

  • 有两个地方可以做if (item.id = present) 。您真的要将present 分配给item.id 吗?应该是if (item.id == present) 不是吗?
  • 所以我的目标是如果 ID 在过去 5 分钟内存在于数据库中,则将单元格更改为绿色,如果没有则将其更改为红色
  • 你是在分配而不是比较。
  • JSON 中的所有项目都有数字 ID - 但在 JavaScript 中,您正在寻找等于字符串 notNULL 的 ID。您生成的表格单元格没有 CSS 类 coloured - 但您在 forEach 中的 jQuery 选择器正在寻找这个 CSS 类。

标签: javascript php mysql


【解决方案1】:

您应该在将 item.id 设置为 present 的第一个中将 if (item.id = present) 更改为 if(item.id == present),这是错误的,您需要测试。这只是一个语法错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多