简体   繁体   English

用php + mysql填充morris.js

[英]Populate morris.js with php + mysql

Oh Hello! 哦你好! I can't create charts with mysql using morris.js... I have a table in my db which is divided into: 我无法使用morris.js用mysql创建图表...我的数据库中有一个表,该表分为:

+----+-------+-----------+---------+
| id | name  | connected | blocked |
+----+-------+-----------+---------+

where in connected and blocked is stored respectively true or false and true or false . 在connected和blocked中分别存储true或falsetrue或false的位置

Example: 例:

+----+-------+-----------+---------+
| id | name  | connected | blocked |
+----+-------+-----------+---------+
|  1 | cesar | true      | false   |
+----+-------+-----------+---------+

And I want to show the number of connected clients, disconnected clients, blocked and unblocked clients in morris.js charts. 我想在morris.js图表​​中显示已连接的客户端,断开的客户端,已阻止和已阻止客户端的数量。

I also think my query is wrong... There it is: 我还认为我的查询是错误的...它是:

$link = mysqli_connect("127.0.0.1", "root", "mysql", "charts");
$query = mysqli_query($link, "SELECT * from data");

$array = array();
while ( $row = mysqli_fetch_assoc( $query ) ) 
{
 array_push(
    $array,
    array(
         'x' => $row['blocked'],
         'y' => $row['connected']
    )
 );
}
echo json_encode($array);

$.ajax({
    'async': true,
    'global': false,
    'url': 'cobaJson.php',
    'dataType': "json",
    'success': function (data) {
        new Morris.Line({
            element: 'myfirstchart',
            data: json,
            xkey: 'x',
            ykeys: ['y'],
            labels: ['Value']
        });   
    }
});

First, check your response, you should see it in the network tab of chrome dev tools. 首先,检查您的响应,您应该在chrome dev工具的“网络”标签中看到它。

If it correct chrome can show you your structured data 如果正确,Chrome可以向您显示结构化数据

在此处输入图片说明

If it is not correct you can better manage your query like this: 如果不正确,则可以像这样更好地管理您的查询:

$link = mysqli_connect("127.0.0.1", "root", "mysql", "charts");
$query = mysqli_query($link, "SELECT * from data");
if(!$query) {
  die('Mysql error:'. mysqli_error($link));
}

and if there's an error in your query you should see the error in chrome dev tools when the call is performed. 并且如果您的查询中有错误,则在执行调用时,您应该在chrome dev工具中看到该错误。

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

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