为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
料青山看我应如是 2021-12-03 19:11:56
我有一个图表,我想从存储在模型中的变量中获取值。我的 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]”中的服务,质量,清洁和员工价值,例如数据:[服务价值,质量价值,清洁价值,员工价值]
查看完整描述

1 回答

?
暮色呼如

TA贡献1853条经验 获得超9个赞

首先,您需要从控制器调用模型函数“chart_data”,例如:


$chart_data= $this->model_name->chart_data();

然后将 $chart_data 变量发送到您的视图,如下所示:


$this->load->view('your view name', $chart_data);

之后,您可以在视图中使用 chart_data 变量,如下所示:


    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: ['<?php echo $chart_data['service'] ?>','<?php echo $chart_data['quality'] ?>','<?php echo $chart_data['clean'] ?>','<?php echo $chart_data['staff'] ?>'],

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

       }]},


   });


查看完整回答
反对 回复 2021-12-03
  • 1 回答
  • 0 关注
  • 160 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信