猿问

为 WordPress 中的每个数组项从数据库获取数据作为数组

我正在为 WordPress 编写一个反应插件,并尝试使用 chart.js 为过去 7 天每天反应计数的管理员创建一个图表。用户反应记录在数据库中的以下列:postId、reactedTo和reactedDate。示例行将是182,Haha和27 Jun 2019。


我在 JavaScript 中使用相同格式生成过去 7 天日期的数组,以创建图表标签,并尝试通过 AJAX 将其发送到后端,以从数组中的数据库中获取每个日期记录的计数格式。因此,例如,数据库包含以下数据:


postId   reactedTo   reactedDate

145      Like        22 Jun 2019

182      Haha        24 Jun 2019

182      Haha        27 Jun 2019


我在 JS 中使用此代码生成过去 7 天的数组:


    var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

    var lastWeek = [];

    for (var i = 0; i < 7; i++) {

        var d = new Date();

        d.setDate(d.getDate() - i);

        lastWeek.push(d.getDate() + ' ' + months[d.getMonth()] + ' ' + d.getFullYear());

    }

    lastweek = lastWeek.reverse()


    console.log(lastweek); 

输出 [“2019 年 6 月 21 日”、“2019 年 6 月 22 日”、“2019 年 6 月 23 日”、“2019 年 6 月 24 日”、“2019 年 6 月 25 日”、“2019 年 6 月 26 日”、“2019 年 6 月 27 日”]


然后我通过 AJAX 将它发送到服务器并生成成功图表。


    jQuery.ajax({

        url: ajaxurl,

        dataType: 'text',

        type: 'POST',

        data: {

            action: 'reactions_analytics',

            dates: JSON.stringify(lastWeek)

        },

        success: function(response, textStatus, jqXhr) {

            var reactionsChart = new Chart(ctx, {

                type: 'line',

                data: {

                    labels: lastWeek,

                    datasets: [{

                        data: response,

                }

            });

        }

    });

在response从服务器应该是数字数组计数用于从数据库中每个日期。如果未找到日期,则为 0。所以应该是[0,1,0,1,0,0,1]。

所以我想我需要 PHP 方面的帮助。我的逻辑有什么不正确?它可以做得更好吗?


基本上我想把它发送到服务器: ["21 Jun 2019","22 Jun 2019","23 Jun 2019","24 Jun 2019","25 Jun 2019","26 Jun 2019","27 Jun 2019"]


并为在 DB 中找到的每个日期获取相同的数组 [0,1,0,1,0,0,1]


杨魅力
浏览 170回答 2
2回答

料青山看我应如是

问题出在 $dates 变量中,因为它返回带有 [ ] 的数组并且需要 SQL 查询 ( ),所以我曾经str_replace用 ( ) 替换 [ ] 并且查询现在运行良好。

肥皂起泡泡

更改代码如下: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) {&nbsp; &nbsp; $result[] = $react['count'];}wp_send_json_success($result);}修改js代码如下:jQuery.ajax({url: ajaxurl,dataType : "json",type: 'POST',data: {&nbsp; &nbsp; action: 'reactions_analytics',&nbsp; &nbsp; dates: lastWeek.reverse()},success: function(response, textStatus, jqXhr) {&nbsp; &nbsp; if ( response.success === true ) {&nbsp; &nbsp; &nbsp; &nbsp; var reactionsChart = new Chart(ctx, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'line',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; labels: lastWeek,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; datasets: [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: response.data,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('error occured !');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}});
随时随地看视频慕课网APP
我要回答