如何从使用 CodeIgniter 存储的变量获取 JQuery 中的值

我有一个图表,我想从存储在模型中的变量中获取值。我的 Jquery 代码:


var ctx = document.getElementById('myChart').getContext('2d');

   var myChart = new Chart(ctx, {

     type: 'bar',

     data: {

       labels: ['Quality', 'Service', 'Cleanliness', 'Staff'],

       datasets: [{

         label: 'Average Ratings',

         data: [0,1,2,4,5],

         backgroundColor: "rgba(153,255,62,0.6)"

       }]},


   });

我的型号:


 public function chart_data()

    {

        $total = $this->db->query("SELECT 

                     ROUND(count((service_ans_1)+(service_ans_2)+(service_ans_3)+(quality_ans_1)+(quality_ans_2)+(quality_ans_3)+(quality_ans_4)+(cleanliness_ans_1)+(cleanliness_ans_2)+(cleanliness_ans_3)+(cleanliness_ans_4)+(cleanliness_ans_5)+(staff_ans_1)+(staff_ans_2)+(staff_ans_3))) as total from `dine_in_feedback` as f")->row()->total;


        $service    = $this->db->query("SELECT 

                        ROUND(avg(((service_ans_1)+(service_ans_2)+(service_ans_3))/3)) as service from `dine_in_feedback` as f")->row()->service;

        $quality    = $this->db->query("SELECT 

                    ROUND(avg(((quality_ans_1)+(quality_ans_2)+(quality_ans_3)+(quality_ans_4))/4)) as quality from `dine_in_feedback` as f")->row()->quality;

        $clean    = $this->db->query("SELECT 

                    ROUND(avg(((cleanliness_ans_1)+(cleanliness_ans_2)+(cleanliness_ans_3)+(cleanliness_ans_4)+(cleanliness_ans_5))/5)) as clean from `dine_in_feedback` as f")->row()->clean;

        $staff   = $this->db->query("SELECT 

                    ROUND(avg(((staff_ans_1)+(staff_ans_2)+(staff_ans_3))/15)) as staff from `dine_in_feedback` as f")->row()->staff;



        $result['service'] = $service / $total * 100;

        $result['quality'] = $quality / $total * 100;

        $result['clean'] = $clean / $total * 100;

        $result['staff'] = $staff / $total * 100;

        return $result;


    }

我想要“数据:[0,1,2,4,5]”中的服务,质量,清洁和员工价值,例如数据:[服务价值,质量价值,清洁价值,员工价值]


料青山看我应如是
浏览 161回答 1
1回答

暮色呼如

首先,您需要从控制器调用模型函数“chart_data”,例如:$chart_data= $this->model_name->chart_data();然后将 $chart_data 变量发送到您的视图,如下所示:$this->load->view('your view name', $chart_data);之后,您可以在视图中使用 chart_data 变量,如下所示:&nbsp; &nbsp; var ctx = document.getElementById('myChart').getContext('2d');&nbsp; &nbsp;var myChart = new Chart(ctx, {&nbsp; &nbsp; &nbsp;type: 'bar',&nbsp; &nbsp; &nbsp;data: {&nbsp; &nbsp; &nbsp; &nbsp;labels: ['Quality', 'Service', 'Cleanliness', 'Staff'],&nbsp; &nbsp; &nbsp; &nbsp;datasets: [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;label: 'Average Ratings',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;data: ['<?php echo $chart_data['service'] ?>','<?php echo $chart_data['quality'] ?>','<?php echo $chart_data['clean'] ?>','<?php echo $chart_data['staff'] ?>'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;backgroundColor: "rgba(153,255,62,0.6)"&nbsp; &nbsp; &nbsp; &nbsp;}]},&nbsp; &nbsp;});
打开App,查看更多内容
随时随地看视频慕课网APP