2 回答
TA贡献1772条经验 获得超8个赞
问题出在 $dates 变量中,因为它返回带有 [ ] 的数组并且需要 SQL 查询 ( ),所以我曾经str_replace
用 ( ) 替换 [ ] 并且查询现在运行良好。
TA贡献1829条经验 获得超6个赞
更改代码如下:
public function reactionsAnalytics() {
global $wpdb;
$tableName = $wpdb->prefix.'reactions';
$dates = str_replace("\\", "", $_POST['dates']);
$reacts = $wpdb->get_results("SELECT reactedDate, count(*) AS count FROM {$tableName} WHERE reactedDate IN ({$dates}) GROUP BY reactedDate", ARRAY_A);
$result = array();
foreach ($reacts as $react) {
$result[] = $react['count'];
}
wp_send_json_success($result);
}
修改js代码如下:
jQuery.ajax({
url: ajaxurl,
dataType : "json",
type: 'POST',
data: {
action: 'reactions_analytics',
dates: lastWeek.reverse()
},
success: function(response, textStatus, jqXhr) {
if ( response.success === true ) {
var reactionsChart = new Chart(ctx, {
type: 'line',
data: {
labels: lastWeek,
datasets: [{
data: response.data,
}
});
} else {
// error
alert('error occured !');
}
}
}});
- 2 回答
- 0 关注
- 165 浏览
添加回答
举报