$(document).ready(function(){
$.ajax({
url:"http://127.0.0.1/vacvans/admin_data.php",
method: "GET",
success: function(data){
console.log(data);
},
error: function(data) {
console.log(data);
var t = [];
var total = [];
for (var i in data) {
t.push(data[i].t);
total.push(data[i].total);
}
var data = {
labels : t ,
datasets : [
{
fillColor : "rgba(220,220,220,0.5)",
strokeColor : "rgba(220,220,220,1)",
pointColor : "rgba(220,220,220,1)",
pointStrokeColor : "#fff",
data : total
}
]
}
var ctx = document.getElementById("mycanvas").getContext("2d");
new Chart(ctx).Bar(data);
}
});
});
admin_data.php
Code (PHP)
<?php include('admin_header.php');
?>
<body>
<?php
session_start();
ob_start();
header('Content-Type: application/json');
mysqli_set_charset($link, "utf8");
$sql = "SELECT * from (SELECT src,dst ,count(*) as total FROM user_search
group by src , dst) as t order by total ";
$queryprovince = $mysqli->query("SELECT * FROM province");
$i = 1;
while($result2 = $queryprovince->fetch_object())
{
$province[$i] = $result2->province;
$i++;
}
$query = $mysqli->query($sql);
$result = $query->fetch_object();
$result3 = mysqli_query($link,$sql);
if (!result3) {
echo "error";
exit();
}
$data = array();
foreach ($result3 as $row) {
$data[] = $row;
}
print json_encode($data);
?>
ตอนนี้จำนวนข้อมูลขึ้นเเล้วค่ะเเต่ชื่อข้อมูลไม่ขึ้น
ชื่อข้อมูลมาจาก src dst ที่ดึง id มาจากอีกตารางค่ะ
รบกวนพี่ๆช่วยชี้เเนะทีค่ะว่าต้องดึงยังไงถึงจะนำอข้อมูลจากอีกตารางนึงเข้ามา
admin_data.php Code (PHP)
<?php
include('connect.php');
session_start();
ob_start();
header('Content-Type: application/json');
mysqli_set_charset($mysqli, "utf8");
$sql = "SELECT * from (SELECT src,dst ,count(*) as total FROM user_search
group by src , dst) as t order by total ";
$queryprovince = $mysqli->query("SELECT * FROM province");
$i = 1;
while($result2 = $queryprovince->fetch_object())
{
$province[$i] = $result2->province;
$i++;
}
$query = $mysqli->query($sql);
$result = $query->fetch_object();
$result3 = mysqli_query($mysqli,$sql);
if (!$result3) {
echo "error";
exit();
}
$data = array();
foreach ($result3 as $row) {
$data[] = $row;
}
$jsonData["data"] = $data;
echo json_encode($jsonData);
graph.js
Code (JavaScript)
$(document).ready(function(){
$.ajax({
url:"http://127.0.0.1/vacvans/admin_data.php",
method: "GET",
success: function(data){
console.log(data);
data = data['data'];
var t = [];
var total = [];
for (var i in data) {
t.push(data[i].t);
total.push(data[i].total);
}
var data = {
labels : t ,
datasets : [
{
fillColor : "rgba(220,220,220,0.5)",
strokeColor : "rgba(220,220,220,1)",
pointColor : "rgba(220,220,220,1)",
pointStrokeColor : "#fff",
data : total
}
]
}
var ctx = document.getElementById("mycanvas").getContext("2d");
new Chart(ctx).Bar(data);
},
error: function(data) {
console.log(data);
}
});
});