在莫里斯条形图标签字段中显示 Mysql 数据库列
Display Mysql Database Column in Morris Bar Graph labels field
我正在使用莫里斯条形图来显示用户登录系统的次数。我想要动态标签(悬停时)。我正在通过 PHP.
获取数据
<?php
$conn = mysqli_connect("localhost", "root", "", "database_name");
$sql = "SELECT account,
(SELECT fullname
from employees as d
WHERE r.emp_id = d.employee_code) as emp_id,
count(username) as total
From logins as r
group by account
ORDER BY emp_id ASC
LIMIT 10";
$most_link =mysqli_query($conn,$sql);
$chart_data = '';
$most_sold = '';
while($row_most= mysqli_fetch_array($most_link)){
$most_sold .= "{ accounnt:'".$row_most["account"]."', employee:'".$row_most["emp_id"]."', total:".$row_most["total"].", }, ";
}
$most_sold = substr($most_sold, 0, -2);
?>
<script>
new Morris.Bar({
element : 'most',
data:[<?php echo $most_sold; ?>],
xkey:'employee',
ykeys:['total'],
labels:'account',
hideHover:'auto',
barColors:['#F5761A'],
});
</script>
如果您注意到,标签被硬编码为 'account(the label text words)',但是,我希望它是动态的(PHP 中的 'account' 属性)。知道怎么做吗?
您没有正确地构建 JSON,通过构建字符串手动做到这一点绝非易事
简单选项
首先更改 sql 以获取具有您希望存在于 JSON
中的名称的列
$sql = "SELECT account,
(SELECT fullname
from employees as d
WHERE r.emp_id = d.employee_code) as employee,
count(username) as total
From logins as r
group by account
ORDER BY emp_id ASC
LIMIT 10";
$most_sold = '';
$most_arr = [];
$labels = [];
while($row_most= mysqli_fetch_array($most_link)){
$most_arr[] = $row_most;
$labels[] = $row_most['account'];
}
$most_sold = json_encode($most_arr);
$json_labels = json_encode($labels);
然后
labels: <?php echo $json_labels; ?>,
我终于找到解决办法了
只需获取值并将其存储在变量
变量存储在这里 $account = $row_most["account"];
然后在labels字段中回显
labels: ['<?php echo $account ?>'],
得到动态数据
$conn = mysqli_connect("localhost", "root", "", "database_name");
$sql = "SELECT account,
(SELECT fullname from employees as d WHERE r.emp_id = d.employee_code) as emp_id, count(username) as total
From logins as r group by account ORDER BY emp_id ASC LIMIT 10";
$most_link =mysqli_query($conn,$sql);
$chart_data = '';
$most_sold = '';
while($row_most= mysqli_fetch_array($most_link)){
$most_sold .= "{ account:'".$row_most["account"]."', employee:'".$row_most["emp_id"]."', total:".$row_most["total"].", }, ";
$account = $row_most["account"];
}
$most_sold = substr($most_sold, 0, -2);
?>
<script>
new Morris.Bar({
element : 'most',
data:[<?php echo $most_sold; ?>],
xkey:'employee',
ykeys:['total'],
labels: ['<?php echo $account ?>'],
hideHover:'auto',
barColors:['#F5761A'],
});
</script>```
我正在使用莫里斯条形图来显示用户登录系统的次数。我想要动态标签(悬停时)。我正在通过 PHP.
获取数据<?php
$conn = mysqli_connect("localhost", "root", "", "database_name");
$sql = "SELECT account,
(SELECT fullname
from employees as d
WHERE r.emp_id = d.employee_code) as emp_id,
count(username) as total
From logins as r
group by account
ORDER BY emp_id ASC
LIMIT 10";
$most_link =mysqli_query($conn,$sql);
$chart_data = '';
$most_sold = '';
while($row_most= mysqli_fetch_array($most_link)){
$most_sold .= "{ accounnt:'".$row_most["account"]."', employee:'".$row_most["emp_id"]."', total:".$row_most["total"].", }, ";
}
$most_sold = substr($most_sold, 0, -2);
?>
<script>
new Morris.Bar({
element : 'most',
data:[<?php echo $most_sold; ?>],
xkey:'employee',
ykeys:['total'],
labels:'account',
hideHover:'auto',
barColors:['#F5761A'],
});
</script>
如果您注意到,标签被硬编码为 'account(the label text words)',但是,我希望它是动态的(PHP 中的 'account' 属性)。知道怎么做吗?
您没有正确地构建 JSON,通过构建字符串手动做到这一点绝非易事
简单选项
首先更改 sql 以获取具有您希望存在于 JSON
中的名称的列$sql = "SELECT account,
(SELECT fullname
from employees as d
WHERE r.emp_id = d.employee_code) as employee,
count(username) as total
From logins as r
group by account
ORDER BY emp_id ASC
LIMIT 10";
$most_sold = '';
$most_arr = [];
$labels = [];
while($row_most= mysqli_fetch_array($most_link)){
$most_arr[] = $row_most;
$labels[] = $row_most['account'];
}
$most_sold = json_encode($most_arr);
$json_labels = json_encode($labels);
然后
labels: <?php echo $json_labels; ?>,
我终于找到解决办法了
只需获取值并将其存储在变量
变量存储在这里 $account = $row_most["account"];
然后在labels字段中回显labels: ['<?php echo $account ?>'],
得到动态数据
$conn = mysqli_connect("localhost", "root", "", "database_name");
$sql = "SELECT account,
(SELECT fullname from employees as d WHERE r.emp_id = d.employee_code) as emp_id, count(username) as total
From logins as r group by account ORDER BY emp_id ASC LIMIT 10";
$most_link =mysqli_query($conn,$sql);
$chart_data = '';
$most_sold = '';
while($row_most= mysqli_fetch_array($most_link)){
$most_sold .= "{ account:'".$row_most["account"]."', employee:'".$row_most["emp_id"]."', total:".$row_most["total"].", }, ";
$account = $row_most["account"];
}
$most_sold = substr($most_sold, 0, -2);
?>
<script>
new Morris.Bar({
element : 'most',
data:[<?php echo $most_sold; ?>],
xkey:'employee',
ykeys:['total'],
labels: ['<?php echo $account ?>'],
hideHover:'auto',
barColors:['#F5761A'],
});
</script>```