JSON rating chart is not loading











up vote
-3
down vote

favorite












I'm trying to create a rating chart which is powered by node js. However, it's not loading as there is some sort of problem in the code and I couldn't find it. Anyway I looked up through the "inspect elements" and located the error.
According to that, following code segment carries the error in jquery-2.1.4 :



try {
// Do send the request (this may raise an exception)
xhr.send(options.hasContent && options.data || null);
} catch (e) {
// #14683: Only rethrow if this hasn't been notified as an error yet
if (callback) {
throw e;
}


chat_data.php



    <?php
//address of the server where db is installed
$servername = "localhost";

//username to connect to the db
//the default value is root
$username = "root";

//password to connect to the db
//this is the value you would have specified during installation of WAMP stack
$password = "";

//name of the db under which the table is created
$dbName = "niwarana1";

//establishing the connection to the db.
$conn = new mysqli($servername, $username, $password, $dbName);

//checking if there were any error during the last connection attempt
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

//the SQL query to be executed
$query = "SELECT doctor.did ,dfname , doctor.avg_vote as avgrate
from post_rating
INNER JOIN posts ON post_rating.postid=doctor.did
group by doctor.did
order by avgrate Desc";

//storing the result of the executed query
$result = $conn->query($query);

//initialize the array to store the processed data
$jsonArray = array();

//check if there is any data returned by the SQL Query
if ($result->num_rows > 0) {
//Converting the results into an associative array
while($row = $result->fetch_assoc()) {
$jsonArrayItem = array();
$jsonArrayItem['label'] = $row['dfname'];
$jsonArrayItem['value'] = $row['avgrate'];
//append the above created object into the main array.
array_push($jsonArray, $jsonArrayItem);
}
}

//Closing the connection to DB
$conn->close();

//set the response content type as JSON
header('Content-type: application/json');
//output the return value of json encode using the echo function.
echo json_encode($jsonArray);
?>

index.php

<?php

include "config.php";
//session_start();

//if(!$_SESSION['email'])
//{

// header("Location: login/login.php");//redirect to login page to secure the welcome page without login access.
//}


?>
<html>
<head>
<title>Ayurvedic Doc</title>
<h4><a href="http://localhost/6_2/aprofile.php">ආපසු </a> </h4>
<!-- CSS -->
<link href="style.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css">
<link href='jquery-bar-rating-master/dist/themes/fontawesome-stars.css' rel='stylesheet' type='text/css'>

<!-- Script -->
<script src="jquery-3.0.0.js" type="text/javascript"></script>
<script src="jquery-bar-rating-master/dist/jquery.barrating.min.js" type="text/javascript"></script>
<script type="text/javascript">


</script>
</head>
<body>
<div id="chart-container" align ="center">තක්සේරුගත වෛද්‍යවරුන් ලේඛනය </div>
<script src="js/jquery-2.1.4.js"></script>
<script src="js/fusioncharts.js"></script>
<script src="js/fusioncharts.charts.js"></script>
<script src="js/themes/fusioncharts.theme.zune.js"></script>
<script src="js/app.js"></script>
<script src="js/app2.js"></script>





<div id="piechart"></div>

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

<script type="text/javascript">
// Load google charts
google.charts.load('current', {
'packages': ['corechart']
});
google.charts.setOnLoadCallback(drawChart);

// Draw the chart and set the chart values
function drawChart() {
$.ajax({

url: 'http://localhost/testoshadi_2/chart_data.php',
type: 'GET',
success: function(data) {
chartData = data;


$.ajax({

url: 'http://localhost/testoshadi_2/chart_data.php',
type: 'GET',
success: function(data) {
chartData = data;
var data = new google.visualization.DataTable(chartData);
// var data = google.visualization.arrayToDataTable([
// chartData
// ]);

// Optional; add a title and set the width and height of the chart
var options = {
'title': 'My Average Day',
'width': 550,
'height': 400
};

// Display the chart inside the <div> element with id="piechart"
// var chart = new google.visualization.PieChart(document.getElementById('piechart'));
// chart.draw(data, options);

}
});


}
});

}
</script>


<div class="content">

<?php

$userid = '3';
$query = "SELECT * FROM comments";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){
$like_count= $row['like_count'];
$dislike_count= $row['dislike_count'];

$comment_score= $row['score'];
$userRating=Bernoulli_parameter($like_count,$dislike_count)*$comment_score;
$uid= $row['userid'];

// echo "uid--".$uid." ".$userRating."</br>";
$query_insert = "SELECT * FROM post_rating where userid=".$uid;
$result_insert = mysqli_query($con,$query_insert);
while($row_ins = mysqli_fetch_array($result_insert)){
// $postid_ins = $row_ins['id'];
// print_r($row_ins);
// $avgrate = $row_ins['avgrate'];
$updatequery = "UPDATE post_rating SET rating=".$userRating." where userid=".$uid ;
// echo mysqli_query($con,$updatequery);
if ($con->query($updatequery) === TRUE) {
// echo "Record updated successfully";
} else {
// echo "Error updating record: " . $conn->error;
}
}
}



$query_insert = "SELECT doctor.did, SUM(post_rating.rating) as avgrate
from post_rating
INNER JOIN doctor ON post_rating.postid=doctor.did
group by doctor.did";
$result_insert = mysqli_query($con,$query_insert);
while($row_ins = mysqli_fetch_array($result_insert)){
$postid_ins = $row_ins['did'];
$avgrate = $row_ins['avgrate'];
$updatequery = "UPDATE doctor SET avg_vote=" . $avgrate . " where did=" . $postid_ins ;
mysqli_query($con,$updatequery);

}

// echo $_SESSION['id'];
$userid = '3';
$query = "SELECT * FROM doctor order by avg_vote DESC ";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){

$postid = $row['did'];
$dfname = $row['dfname'];


// User rating
$query = "SELECT * FROM post_rating WHERE postid=".$postid." and userid=".$userid;
$userresult = mysqli_query($con,$query) or die(mysqli_error());
$fetchRating = mysqli_fetch_array($userresult);
$rating = $fetchRating['rating'];
echo $rating;
// get average
$query = "SELECT ROUND(AVG(rating),1) as averageRating FROM post_rating WHERE postid=".$postid;
$avgresult = mysqli_query($con,$query) or die(mysqli_error());
$fetchAverage = mysqli_fetch_array($avgresult);
$averageRating = $fetchAverage['averageRating'];

if($averageRating <= 0){
$averageRating = "No rating yet.";
}


$query = "SELECT count(rating) as cusnt ,rating
from post_rating
where postid =".$postid."
GROUP BY rating
ORDER BY rating ASC";
$rse = mysqli_query($con,$query);






}


/*
|--------------------------------------------------------------------------
| 5 Star Rating
|--------------------------------------------------------------------------
|
| Lower bound of Wilson score confidence interval for a Bernoulli parameter (0.9604)
|
| See:
| * http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
| * https://gist.github.com/richardkundl/2950196
| * https://onextrapixel.com/how-to-build-a-5-star-rating-system-with-wilson-interval-in-mysql/
|
*/
function Bernoulli_parameter($positive, $negative) {
return ((($positive + 1.9208) / ($positive + $negative) - 1.96 * sqrt((($positive * $negative) / ($positive + $negative)) + 0.9604) / ($positive + $negative)) / (1 + 3.8416 / ($positive + $negative)));
}


?>

</div>


</body>
</html>


rating_ajax.php



<?php

include "config.php";
session_start();

$userid = $_SESSION['patient_id'];
$postid = $_POST['postid'];
$rating = $_POST['rating'];

// Check entry within table
$query = "SELECT COUNT(*) AS cntpost FROM post_rating WHERE postid=".$postid." and userid=".$userid;

$result = mysqli_query($con,$query);
$fetchdata = mysqli_fetch_array($result);
$count = $fetchdata['cntpost'];

if($count == 0){
$insertquery = "INSERT INTO post_rating(userid,postid,rating) values(".$userid.",".$postid.",".$rating.")";
mysqli_query($con,$insertquery);
}else {
$updatequery = "UPDATE post_rating SET rating=" . $rating . " where userid=" . $userid . " and postid=" . $postid;
mysqli_query($con,$updatequery);
}


// get average
$query = "SELECT ROUND(AVG(rating),1) as averageRating FROM post_rating WHERE postid=".$postid;
$result = mysqli_query($con,$query) or die(mysqli_error());
$fetchAverage = mysqli_fetch_array($result);
$averageRating = $fetchAverage['averageRating'];

$return_arr = array("averageRating"=>$averageRating);

echo json_encode($return_arr);









share|improve this question









New contributor




Oshadi Guruge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2




    Error messages? Anything in your browser console? Is the request failing? Check the network tab etc.
    – Jon Stirling
    Nov 8 at 10:37










  • The error itself won't be in jQuery, that's just where it manifests. The problem is much more likely to be in your own code where you're sending incorrect parameters or values to a jQuery method call. Without seeing your code which makes the call, we can't really help you debug the problem.
    – Rory McCrossan
    Nov 8 at 10:39








  • 1




    give us more information? what chart library used? how is it used in code? what are error messages? Without this information you question will be down voted and/or closed.
    – SET
    Nov 8 at 10:49










  • I have uploaded my project into google drive and here's the link of it : drive.google.com/file/d/1hP4uFTGR4m3VVjgRh8u4rEJFUJIoi4Xn/… This shared folder includes the project and the database which is used by the chart to load its data.
    – Oshadi Guruge
    Nov 8 at 11:01








  • 1




    @OshadiGuruge Please add all relevant code as text into your question. You'll be hard pressed to find somebody that will go through an entire project of code, and it doesn't help anyone looking at your question later.
    – Jon Stirling
    Nov 8 at 11:23















up vote
-3
down vote

favorite












I'm trying to create a rating chart which is powered by node js. However, it's not loading as there is some sort of problem in the code and I couldn't find it. Anyway I looked up through the "inspect elements" and located the error.
According to that, following code segment carries the error in jquery-2.1.4 :



try {
// Do send the request (this may raise an exception)
xhr.send(options.hasContent && options.data || null);
} catch (e) {
// #14683: Only rethrow if this hasn't been notified as an error yet
if (callback) {
throw e;
}


chat_data.php



    <?php
//address of the server where db is installed
$servername = "localhost";

//username to connect to the db
//the default value is root
$username = "root";

//password to connect to the db
//this is the value you would have specified during installation of WAMP stack
$password = "";

//name of the db under which the table is created
$dbName = "niwarana1";

//establishing the connection to the db.
$conn = new mysqli($servername, $username, $password, $dbName);

//checking if there were any error during the last connection attempt
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

//the SQL query to be executed
$query = "SELECT doctor.did ,dfname , doctor.avg_vote as avgrate
from post_rating
INNER JOIN posts ON post_rating.postid=doctor.did
group by doctor.did
order by avgrate Desc";

//storing the result of the executed query
$result = $conn->query($query);

//initialize the array to store the processed data
$jsonArray = array();

//check if there is any data returned by the SQL Query
if ($result->num_rows > 0) {
//Converting the results into an associative array
while($row = $result->fetch_assoc()) {
$jsonArrayItem = array();
$jsonArrayItem['label'] = $row['dfname'];
$jsonArrayItem['value'] = $row['avgrate'];
//append the above created object into the main array.
array_push($jsonArray, $jsonArrayItem);
}
}

//Closing the connection to DB
$conn->close();

//set the response content type as JSON
header('Content-type: application/json');
//output the return value of json encode using the echo function.
echo json_encode($jsonArray);
?>

index.php

<?php

include "config.php";
//session_start();

//if(!$_SESSION['email'])
//{

// header("Location: login/login.php");//redirect to login page to secure the welcome page without login access.
//}


?>
<html>
<head>
<title>Ayurvedic Doc</title>
<h4><a href="http://localhost/6_2/aprofile.php">ආපසු </a> </h4>
<!-- CSS -->
<link href="style.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css">
<link href='jquery-bar-rating-master/dist/themes/fontawesome-stars.css' rel='stylesheet' type='text/css'>

<!-- Script -->
<script src="jquery-3.0.0.js" type="text/javascript"></script>
<script src="jquery-bar-rating-master/dist/jquery.barrating.min.js" type="text/javascript"></script>
<script type="text/javascript">


</script>
</head>
<body>
<div id="chart-container" align ="center">තක්සේරුගත වෛද්‍යවරුන් ලේඛනය </div>
<script src="js/jquery-2.1.4.js"></script>
<script src="js/fusioncharts.js"></script>
<script src="js/fusioncharts.charts.js"></script>
<script src="js/themes/fusioncharts.theme.zune.js"></script>
<script src="js/app.js"></script>
<script src="js/app2.js"></script>





<div id="piechart"></div>

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

<script type="text/javascript">
// Load google charts
google.charts.load('current', {
'packages': ['corechart']
});
google.charts.setOnLoadCallback(drawChart);

// Draw the chart and set the chart values
function drawChart() {
$.ajax({

url: 'http://localhost/testoshadi_2/chart_data.php',
type: 'GET',
success: function(data) {
chartData = data;


$.ajax({

url: 'http://localhost/testoshadi_2/chart_data.php',
type: 'GET',
success: function(data) {
chartData = data;
var data = new google.visualization.DataTable(chartData);
// var data = google.visualization.arrayToDataTable([
// chartData
// ]);

// Optional; add a title and set the width and height of the chart
var options = {
'title': 'My Average Day',
'width': 550,
'height': 400
};

// Display the chart inside the <div> element with id="piechart"
// var chart = new google.visualization.PieChart(document.getElementById('piechart'));
// chart.draw(data, options);

}
});


}
});

}
</script>


<div class="content">

<?php

$userid = '3';
$query = "SELECT * FROM comments";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){
$like_count= $row['like_count'];
$dislike_count= $row['dislike_count'];

$comment_score= $row['score'];
$userRating=Bernoulli_parameter($like_count,$dislike_count)*$comment_score;
$uid= $row['userid'];

// echo "uid--".$uid." ".$userRating."</br>";
$query_insert = "SELECT * FROM post_rating where userid=".$uid;
$result_insert = mysqli_query($con,$query_insert);
while($row_ins = mysqli_fetch_array($result_insert)){
// $postid_ins = $row_ins['id'];
// print_r($row_ins);
// $avgrate = $row_ins['avgrate'];
$updatequery = "UPDATE post_rating SET rating=".$userRating." where userid=".$uid ;
// echo mysqli_query($con,$updatequery);
if ($con->query($updatequery) === TRUE) {
// echo "Record updated successfully";
} else {
// echo "Error updating record: " . $conn->error;
}
}
}



$query_insert = "SELECT doctor.did, SUM(post_rating.rating) as avgrate
from post_rating
INNER JOIN doctor ON post_rating.postid=doctor.did
group by doctor.did";
$result_insert = mysqli_query($con,$query_insert);
while($row_ins = mysqli_fetch_array($result_insert)){
$postid_ins = $row_ins['did'];
$avgrate = $row_ins['avgrate'];
$updatequery = "UPDATE doctor SET avg_vote=" . $avgrate . " where did=" . $postid_ins ;
mysqli_query($con,$updatequery);

}

// echo $_SESSION['id'];
$userid = '3';
$query = "SELECT * FROM doctor order by avg_vote DESC ";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){

$postid = $row['did'];
$dfname = $row['dfname'];


// User rating
$query = "SELECT * FROM post_rating WHERE postid=".$postid." and userid=".$userid;
$userresult = mysqli_query($con,$query) or die(mysqli_error());
$fetchRating = mysqli_fetch_array($userresult);
$rating = $fetchRating['rating'];
echo $rating;
// get average
$query = "SELECT ROUND(AVG(rating),1) as averageRating FROM post_rating WHERE postid=".$postid;
$avgresult = mysqli_query($con,$query) or die(mysqli_error());
$fetchAverage = mysqli_fetch_array($avgresult);
$averageRating = $fetchAverage['averageRating'];

if($averageRating <= 0){
$averageRating = "No rating yet.";
}


$query = "SELECT count(rating) as cusnt ,rating
from post_rating
where postid =".$postid."
GROUP BY rating
ORDER BY rating ASC";
$rse = mysqli_query($con,$query);






}


/*
|--------------------------------------------------------------------------
| 5 Star Rating
|--------------------------------------------------------------------------
|
| Lower bound of Wilson score confidence interval for a Bernoulli parameter (0.9604)
|
| See:
| * http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
| * https://gist.github.com/richardkundl/2950196
| * https://onextrapixel.com/how-to-build-a-5-star-rating-system-with-wilson-interval-in-mysql/
|
*/
function Bernoulli_parameter($positive, $negative) {
return ((($positive + 1.9208) / ($positive + $negative) - 1.96 * sqrt((($positive * $negative) / ($positive + $negative)) + 0.9604) / ($positive + $negative)) / (1 + 3.8416 / ($positive + $negative)));
}


?>

</div>


</body>
</html>


rating_ajax.php



<?php

include "config.php";
session_start();

$userid = $_SESSION['patient_id'];
$postid = $_POST['postid'];
$rating = $_POST['rating'];

// Check entry within table
$query = "SELECT COUNT(*) AS cntpost FROM post_rating WHERE postid=".$postid." and userid=".$userid;

$result = mysqli_query($con,$query);
$fetchdata = mysqli_fetch_array($result);
$count = $fetchdata['cntpost'];

if($count == 0){
$insertquery = "INSERT INTO post_rating(userid,postid,rating) values(".$userid.",".$postid.",".$rating.")";
mysqli_query($con,$insertquery);
}else {
$updatequery = "UPDATE post_rating SET rating=" . $rating . " where userid=" . $userid . " and postid=" . $postid;
mysqli_query($con,$updatequery);
}


// get average
$query = "SELECT ROUND(AVG(rating),1) as averageRating FROM post_rating WHERE postid=".$postid;
$result = mysqli_query($con,$query) or die(mysqli_error());
$fetchAverage = mysqli_fetch_array($result);
$averageRating = $fetchAverage['averageRating'];

$return_arr = array("averageRating"=>$averageRating);

echo json_encode($return_arr);









share|improve this question









New contributor




Oshadi Guruge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2




    Error messages? Anything in your browser console? Is the request failing? Check the network tab etc.
    – Jon Stirling
    Nov 8 at 10:37










  • The error itself won't be in jQuery, that's just where it manifests. The problem is much more likely to be in your own code where you're sending incorrect parameters or values to a jQuery method call. Without seeing your code which makes the call, we can't really help you debug the problem.
    – Rory McCrossan
    Nov 8 at 10:39








  • 1




    give us more information? what chart library used? how is it used in code? what are error messages? Without this information you question will be down voted and/or closed.
    – SET
    Nov 8 at 10:49










  • I have uploaded my project into google drive and here's the link of it : drive.google.com/file/d/1hP4uFTGR4m3VVjgRh8u4rEJFUJIoi4Xn/… This shared folder includes the project and the database which is used by the chart to load its data.
    – Oshadi Guruge
    Nov 8 at 11:01








  • 1




    @OshadiGuruge Please add all relevant code as text into your question. You'll be hard pressed to find somebody that will go through an entire project of code, and it doesn't help anyone looking at your question later.
    – Jon Stirling
    Nov 8 at 11:23













up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











I'm trying to create a rating chart which is powered by node js. However, it's not loading as there is some sort of problem in the code and I couldn't find it. Anyway I looked up through the "inspect elements" and located the error.
According to that, following code segment carries the error in jquery-2.1.4 :



try {
// Do send the request (this may raise an exception)
xhr.send(options.hasContent && options.data || null);
} catch (e) {
// #14683: Only rethrow if this hasn't been notified as an error yet
if (callback) {
throw e;
}


chat_data.php



    <?php
//address of the server where db is installed
$servername = "localhost";

//username to connect to the db
//the default value is root
$username = "root";

//password to connect to the db
//this is the value you would have specified during installation of WAMP stack
$password = "";

//name of the db under which the table is created
$dbName = "niwarana1";

//establishing the connection to the db.
$conn = new mysqli($servername, $username, $password, $dbName);

//checking if there were any error during the last connection attempt
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

//the SQL query to be executed
$query = "SELECT doctor.did ,dfname , doctor.avg_vote as avgrate
from post_rating
INNER JOIN posts ON post_rating.postid=doctor.did
group by doctor.did
order by avgrate Desc";

//storing the result of the executed query
$result = $conn->query($query);

//initialize the array to store the processed data
$jsonArray = array();

//check if there is any data returned by the SQL Query
if ($result->num_rows > 0) {
//Converting the results into an associative array
while($row = $result->fetch_assoc()) {
$jsonArrayItem = array();
$jsonArrayItem['label'] = $row['dfname'];
$jsonArrayItem['value'] = $row['avgrate'];
//append the above created object into the main array.
array_push($jsonArray, $jsonArrayItem);
}
}

//Closing the connection to DB
$conn->close();

//set the response content type as JSON
header('Content-type: application/json');
//output the return value of json encode using the echo function.
echo json_encode($jsonArray);
?>

index.php

<?php

include "config.php";
//session_start();

//if(!$_SESSION['email'])
//{

// header("Location: login/login.php");//redirect to login page to secure the welcome page without login access.
//}


?>
<html>
<head>
<title>Ayurvedic Doc</title>
<h4><a href="http://localhost/6_2/aprofile.php">ආපසු </a> </h4>
<!-- CSS -->
<link href="style.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css">
<link href='jquery-bar-rating-master/dist/themes/fontawesome-stars.css' rel='stylesheet' type='text/css'>

<!-- Script -->
<script src="jquery-3.0.0.js" type="text/javascript"></script>
<script src="jquery-bar-rating-master/dist/jquery.barrating.min.js" type="text/javascript"></script>
<script type="text/javascript">


</script>
</head>
<body>
<div id="chart-container" align ="center">තක්සේරුගත වෛද්‍යවරුන් ලේඛනය </div>
<script src="js/jquery-2.1.4.js"></script>
<script src="js/fusioncharts.js"></script>
<script src="js/fusioncharts.charts.js"></script>
<script src="js/themes/fusioncharts.theme.zune.js"></script>
<script src="js/app.js"></script>
<script src="js/app2.js"></script>





<div id="piechart"></div>

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

<script type="text/javascript">
// Load google charts
google.charts.load('current', {
'packages': ['corechart']
});
google.charts.setOnLoadCallback(drawChart);

// Draw the chart and set the chart values
function drawChart() {
$.ajax({

url: 'http://localhost/testoshadi_2/chart_data.php',
type: 'GET',
success: function(data) {
chartData = data;


$.ajax({

url: 'http://localhost/testoshadi_2/chart_data.php',
type: 'GET',
success: function(data) {
chartData = data;
var data = new google.visualization.DataTable(chartData);
// var data = google.visualization.arrayToDataTable([
// chartData
// ]);

// Optional; add a title and set the width and height of the chart
var options = {
'title': 'My Average Day',
'width': 550,
'height': 400
};

// Display the chart inside the <div> element with id="piechart"
// var chart = new google.visualization.PieChart(document.getElementById('piechart'));
// chart.draw(data, options);

}
});


}
});

}
</script>


<div class="content">

<?php

$userid = '3';
$query = "SELECT * FROM comments";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){
$like_count= $row['like_count'];
$dislike_count= $row['dislike_count'];

$comment_score= $row['score'];
$userRating=Bernoulli_parameter($like_count,$dislike_count)*$comment_score;
$uid= $row['userid'];

// echo "uid--".$uid." ".$userRating."</br>";
$query_insert = "SELECT * FROM post_rating where userid=".$uid;
$result_insert = mysqli_query($con,$query_insert);
while($row_ins = mysqli_fetch_array($result_insert)){
// $postid_ins = $row_ins['id'];
// print_r($row_ins);
// $avgrate = $row_ins['avgrate'];
$updatequery = "UPDATE post_rating SET rating=".$userRating." where userid=".$uid ;
// echo mysqli_query($con,$updatequery);
if ($con->query($updatequery) === TRUE) {
// echo "Record updated successfully";
} else {
// echo "Error updating record: " . $conn->error;
}
}
}



$query_insert = "SELECT doctor.did, SUM(post_rating.rating) as avgrate
from post_rating
INNER JOIN doctor ON post_rating.postid=doctor.did
group by doctor.did";
$result_insert = mysqli_query($con,$query_insert);
while($row_ins = mysqli_fetch_array($result_insert)){
$postid_ins = $row_ins['did'];
$avgrate = $row_ins['avgrate'];
$updatequery = "UPDATE doctor SET avg_vote=" . $avgrate . " where did=" . $postid_ins ;
mysqli_query($con,$updatequery);

}

// echo $_SESSION['id'];
$userid = '3';
$query = "SELECT * FROM doctor order by avg_vote DESC ";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){

$postid = $row['did'];
$dfname = $row['dfname'];


// User rating
$query = "SELECT * FROM post_rating WHERE postid=".$postid." and userid=".$userid;
$userresult = mysqli_query($con,$query) or die(mysqli_error());
$fetchRating = mysqli_fetch_array($userresult);
$rating = $fetchRating['rating'];
echo $rating;
// get average
$query = "SELECT ROUND(AVG(rating),1) as averageRating FROM post_rating WHERE postid=".$postid;
$avgresult = mysqli_query($con,$query) or die(mysqli_error());
$fetchAverage = mysqli_fetch_array($avgresult);
$averageRating = $fetchAverage['averageRating'];

if($averageRating <= 0){
$averageRating = "No rating yet.";
}


$query = "SELECT count(rating) as cusnt ,rating
from post_rating
where postid =".$postid."
GROUP BY rating
ORDER BY rating ASC";
$rse = mysqli_query($con,$query);






}


/*
|--------------------------------------------------------------------------
| 5 Star Rating
|--------------------------------------------------------------------------
|
| Lower bound of Wilson score confidence interval for a Bernoulli parameter (0.9604)
|
| See:
| * http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
| * https://gist.github.com/richardkundl/2950196
| * https://onextrapixel.com/how-to-build-a-5-star-rating-system-with-wilson-interval-in-mysql/
|
*/
function Bernoulli_parameter($positive, $negative) {
return ((($positive + 1.9208) / ($positive + $negative) - 1.96 * sqrt((($positive * $negative) / ($positive + $negative)) + 0.9604) / ($positive + $negative)) / (1 + 3.8416 / ($positive + $negative)));
}


?>

</div>


</body>
</html>


rating_ajax.php



<?php

include "config.php";
session_start();

$userid = $_SESSION['patient_id'];
$postid = $_POST['postid'];
$rating = $_POST['rating'];

// Check entry within table
$query = "SELECT COUNT(*) AS cntpost FROM post_rating WHERE postid=".$postid." and userid=".$userid;

$result = mysqli_query($con,$query);
$fetchdata = mysqli_fetch_array($result);
$count = $fetchdata['cntpost'];

if($count == 0){
$insertquery = "INSERT INTO post_rating(userid,postid,rating) values(".$userid.",".$postid.",".$rating.")";
mysqli_query($con,$insertquery);
}else {
$updatequery = "UPDATE post_rating SET rating=" . $rating . " where userid=" . $userid . " and postid=" . $postid;
mysqli_query($con,$updatequery);
}


// get average
$query = "SELECT ROUND(AVG(rating),1) as averageRating FROM post_rating WHERE postid=".$postid;
$result = mysqli_query($con,$query) or die(mysqli_error());
$fetchAverage = mysqli_fetch_array($result);
$averageRating = $fetchAverage['averageRating'];

$return_arr = array("averageRating"=>$averageRating);

echo json_encode($return_arr);









share|improve this question









New contributor




Oshadi Guruge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm trying to create a rating chart which is powered by node js. However, it's not loading as there is some sort of problem in the code and I couldn't find it. Anyway I looked up through the "inspect elements" and located the error.
According to that, following code segment carries the error in jquery-2.1.4 :



try {
// Do send the request (this may raise an exception)
xhr.send(options.hasContent && options.data || null);
} catch (e) {
// #14683: Only rethrow if this hasn't been notified as an error yet
if (callback) {
throw e;
}


chat_data.php



    <?php
//address of the server where db is installed
$servername = "localhost";

//username to connect to the db
//the default value is root
$username = "root";

//password to connect to the db
//this is the value you would have specified during installation of WAMP stack
$password = "";

//name of the db under which the table is created
$dbName = "niwarana1";

//establishing the connection to the db.
$conn = new mysqli($servername, $username, $password, $dbName);

//checking if there were any error during the last connection attempt
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

//the SQL query to be executed
$query = "SELECT doctor.did ,dfname , doctor.avg_vote as avgrate
from post_rating
INNER JOIN posts ON post_rating.postid=doctor.did
group by doctor.did
order by avgrate Desc";

//storing the result of the executed query
$result = $conn->query($query);

//initialize the array to store the processed data
$jsonArray = array();

//check if there is any data returned by the SQL Query
if ($result->num_rows > 0) {
//Converting the results into an associative array
while($row = $result->fetch_assoc()) {
$jsonArrayItem = array();
$jsonArrayItem['label'] = $row['dfname'];
$jsonArrayItem['value'] = $row['avgrate'];
//append the above created object into the main array.
array_push($jsonArray, $jsonArrayItem);
}
}

//Closing the connection to DB
$conn->close();

//set the response content type as JSON
header('Content-type: application/json');
//output the return value of json encode using the echo function.
echo json_encode($jsonArray);
?>

index.php

<?php

include "config.php";
//session_start();

//if(!$_SESSION['email'])
//{

// header("Location: login/login.php");//redirect to login page to secure the welcome page without login access.
//}


?>
<html>
<head>
<title>Ayurvedic Doc</title>
<h4><a href="http://localhost/6_2/aprofile.php">ආපසු </a> </h4>
<!-- CSS -->
<link href="style.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css">
<link href='jquery-bar-rating-master/dist/themes/fontawesome-stars.css' rel='stylesheet' type='text/css'>

<!-- Script -->
<script src="jquery-3.0.0.js" type="text/javascript"></script>
<script src="jquery-bar-rating-master/dist/jquery.barrating.min.js" type="text/javascript"></script>
<script type="text/javascript">


</script>
</head>
<body>
<div id="chart-container" align ="center">තක්සේරුගත වෛද්‍යවරුන් ලේඛනය </div>
<script src="js/jquery-2.1.4.js"></script>
<script src="js/fusioncharts.js"></script>
<script src="js/fusioncharts.charts.js"></script>
<script src="js/themes/fusioncharts.theme.zune.js"></script>
<script src="js/app.js"></script>
<script src="js/app2.js"></script>





<div id="piechart"></div>

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

<script type="text/javascript">
// Load google charts
google.charts.load('current', {
'packages': ['corechart']
});
google.charts.setOnLoadCallback(drawChart);

// Draw the chart and set the chart values
function drawChart() {
$.ajax({

url: 'http://localhost/testoshadi_2/chart_data.php',
type: 'GET',
success: function(data) {
chartData = data;


$.ajax({

url: 'http://localhost/testoshadi_2/chart_data.php',
type: 'GET',
success: function(data) {
chartData = data;
var data = new google.visualization.DataTable(chartData);
// var data = google.visualization.arrayToDataTable([
// chartData
// ]);

// Optional; add a title and set the width and height of the chart
var options = {
'title': 'My Average Day',
'width': 550,
'height': 400
};

// Display the chart inside the <div> element with id="piechart"
// var chart = new google.visualization.PieChart(document.getElementById('piechart'));
// chart.draw(data, options);

}
});


}
});

}
</script>


<div class="content">

<?php

$userid = '3';
$query = "SELECT * FROM comments";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){
$like_count= $row['like_count'];
$dislike_count= $row['dislike_count'];

$comment_score= $row['score'];
$userRating=Bernoulli_parameter($like_count,$dislike_count)*$comment_score;
$uid= $row['userid'];

// echo "uid--".$uid." ".$userRating."</br>";
$query_insert = "SELECT * FROM post_rating where userid=".$uid;
$result_insert = mysqli_query($con,$query_insert);
while($row_ins = mysqli_fetch_array($result_insert)){
// $postid_ins = $row_ins['id'];
// print_r($row_ins);
// $avgrate = $row_ins['avgrate'];
$updatequery = "UPDATE post_rating SET rating=".$userRating." where userid=".$uid ;
// echo mysqli_query($con,$updatequery);
if ($con->query($updatequery) === TRUE) {
// echo "Record updated successfully";
} else {
// echo "Error updating record: " . $conn->error;
}
}
}



$query_insert = "SELECT doctor.did, SUM(post_rating.rating) as avgrate
from post_rating
INNER JOIN doctor ON post_rating.postid=doctor.did
group by doctor.did";
$result_insert = mysqli_query($con,$query_insert);
while($row_ins = mysqli_fetch_array($result_insert)){
$postid_ins = $row_ins['did'];
$avgrate = $row_ins['avgrate'];
$updatequery = "UPDATE doctor SET avg_vote=" . $avgrate . " where did=" . $postid_ins ;
mysqli_query($con,$updatequery);

}

// echo $_SESSION['id'];
$userid = '3';
$query = "SELECT * FROM doctor order by avg_vote DESC ";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){

$postid = $row['did'];
$dfname = $row['dfname'];


// User rating
$query = "SELECT * FROM post_rating WHERE postid=".$postid." and userid=".$userid;
$userresult = mysqli_query($con,$query) or die(mysqli_error());
$fetchRating = mysqli_fetch_array($userresult);
$rating = $fetchRating['rating'];
echo $rating;
// get average
$query = "SELECT ROUND(AVG(rating),1) as averageRating FROM post_rating WHERE postid=".$postid;
$avgresult = mysqli_query($con,$query) or die(mysqli_error());
$fetchAverage = mysqli_fetch_array($avgresult);
$averageRating = $fetchAverage['averageRating'];

if($averageRating <= 0){
$averageRating = "No rating yet.";
}


$query = "SELECT count(rating) as cusnt ,rating
from post_rating
where postid =".$postid."
GROUP BY rating
ORDER BY rating ASC";
$rse = mysqli_query($con,$query);






}


/*
|--------------------------------------------------------------------------
| 5 Star Rating
|--------------------------------------------------------------------------
|
| Lower bound of Wilson score confidence interval for a Bernoulli parameter (0.9604)
|
| See:
| * http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
| * https://gist.github.com/richardkundl/2950196
| * https://onextrapixel.com/how-to-build-a-5-star-rating-system-with-wilson-interval-in-mysql/
|
*/
function Bernoulli_parameter($positive, $negative) {
return ((($positive + 1.9208) / ($positive + $negative) - 1.96 * sqrt((($positive * $negative) / ($positive + $negative)) + 0.9604) / ($positive + $negative)) / (1 + 3.8416 / ($positive + $negative)));
}


?>

</div>


</body>
</html>


rating_ajax.php



<?php

include "config.php";
session_start();

$userid = $_SESSION['patient_id'];
$postid = $_POST['postid'];
$rating = $_POST['rating'];

// Check entry within table
$query = "SELECT COUNT(*) AS cntpost FROM post_rating WHERE postid=".$postid." and userid=".$userid;

$result = mysqli_query($con,$query);
$fetchdata = mysqli_fetch_array($result);
$count = $fetchdata['cntpost'];

if($count == 0){
$insertquery = "INSERT INTO post_rating(userid,postid,rating) values(".$userid.",".$postid.",".$rating.")";
mysqli_query($con,$insertquery);
}else {
$updatequery = "UPDATE post_rating SET rating=" . $rating . " where userid=" . $userid . " and postid=" . $postid;
mysqli_query($con,$updatequery);
}


// get average
$query = "SELECT ROUND(AVG(rating),1) as averageRating FROM post_rating WHERE postid=".$postid;
$result = mysqli_query($con,$query) or die(mysqli_error());
$fetchAverage = mysqli_fetch_array($result);
$averageRating = $fetchAverage['averageRating'];

$return_arr = array("averageRating"=>$averageRating);

echo json_encode($return_arr);






jquery charts jscharts






share|improve this question









New contributor




Oshadi Guruge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Oshadi Guruge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 8 at 11:39





















New contributor




Oshadi Guruge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 8 at 10:35









Oshadi Guruge

11




11




New contributor




Oshadi Guruge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Oshadi Guruge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Oshadi Guruge is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2




    Error messages? Anything in your browser console? Is the request failing? Check the network tab etc.
    – Jon Stirling
    Nov 8 at 10:37










  • The error itself won't be in jQuery, that's just where it manifests. The problem is much more likely to be in your own code where you're sending incorrect parameters or values to a jQuery method call. Without seeing your code which makes the call, we can't really help you debug the problem.
    – Rory McCrossan
    Nov 8 at 10:39








  • 1




    give us more information? what chart library used? how is it used in code? what are error messages? Without this information you question will be down voted and/or closed.
    – SET
    Nov 8 at 10:49










  • I have uploaded my project into google drive and here's the link of it : drive.google.com/file/d/1hP4uFTGR4m3VVjgRh8u4rEJFUJIoi4Xn/… This shared folder includes the project and the database which is used by the chart to load its data.
    – Oshadi Guruge
    Nov 8 at 11:01








  • 1




    @OshadiGuruge Please add all relevant code as text into your question. You'll be hard pressed to find somebody that will go through an entire project of code, and it doesn't help anyone looking at your question later.
    – Jon Stirling
    Nov 8 at 11:23














  • 2




    Error messages? Anything in your browser console? Is the request failing? Check the network tab etc.
    – Jon Stirling
    Nov 8 at 10:37










  • The error itself won't be in jQuery, that's just where it manifests. The problem is much more likely to be in your own code where you're sending incorrect parameters or values to a jQuery method call. Without seeing your code which makes the call, we can't really help you debug the problem.
    – Rory McCrossan
    Nov 8 at 10:39








  • 1




    give us more information? what chart library used? how is it used in code? what are error messages? Without this information you question will be down voted and/or closed.
    – SET
    Nov 8 at 10:49










  • I have uploaded my project into google drive and here's the link of it : drive.google.com/file/d/1hP4uFTGR4m3VVjgRh8u4rEJFUJIoi4Xn/… This shared folder includes the project and the database which is used by the chart to load its data.
    – Oshadi Guruge
    Nov 8 at 11:01








  • 1




    @OshadiGuruge Please add all relevant code as text into your question. You'll be hard pressed to find somebody that will go through an entire project of code, and it doesn't help anyone looking at your question later.
    – Jon Stirling
    Nov 8 at 11:23








2




2




Error messages? Anything in your browser console? Is the request failing? Check the network tab etc.
– Jon Stirling
Nov 8 at 10:37




Error messages? Anything in your browser console? Is the request failing? Check the network tab etc.
– Jon Stirling
Nov 8 at 10:37












The error itself won't be in jQuery, that's just where it manifests. The problem is much more likely to be in your own code where you're sending incorrect parameters or values to a jQuery method call. Without seeing your code which makes the call, we can't really help you debug the problem.
– Rory McCrossan
Nov 8 at 10:39






The error itself won't be in jQuery, that's just where it manifests. The problem is much more likely to be in your own code where you're sending incorrect parameters or values to a jQuery method call. Without seeing your code which makes the call, we can't really help you debug the problem.
– Rory McCrossan
Nov 8 at 10:39






1




1




give us more information? what chart library used? how is it used in code? what are error messages? Without this information you question will be down voted and/or closed.
– SET
Nov 8 at 10:49




give us more information? what chart library used? how is it used in code? what are error messages? Without this information you question will be down voted and/or closed.
– SET
Nov 8 at 10:49












I have uploaded my project into google drive and here's the link of it : drive.google.com/file/d/1hP4uFTGR4m3VVjgRh8u4rEJFUJIoi4Xn/… This shared folder includes the project and the database which is used by the chart to load its data.
– Oshadi Guruge
Nov 8 at 11:01






I have uploaded my project into google drive and here's the link of it : drive.google.com/file/d/1hP4uFTGR4m3VVjgRh8u4rEJFUJIoi4Xn/… This shared folder includes the project and the database which is used by the chart to load its data.
– Oshadi Guruge
Nov 8 at 11:01






1




1




@OshadiGuruge Please add all relevant code as text into your question. You'll be hard pressed to find somebody that will go through an entire project of code, and it doesn't help anyone looking at your question later.
– Jon Stirling
Nov 8 at 11:23




@OshadiGuruge Please add all relevant code as text into your question. You'll be hard pressed to find somebody that will go through an entire project of code, and it doesn't help anyone looking at your question later.
– Jon Stirling
Nov 8 at 11:23

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Oshadi Guruge is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205939%2fjson-rating-chart-is-not-loading%23new-answer', 'question_page');
}
);

Post as a guest





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes








Oshadi Guruge is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Oshadi Guruge is a new contributor. Be nice, and check out our Code of Conduct.













Oshadi Guruge is a new contributor. Be nice, and check out our Code of Conduct.












Oshadi Guruge is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205939%2fjson-rating-chart-is-not-loading%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff