如何将函数索引和函数数据统一为一个新函数

How to unite function index and function data to one new function?

本文关键字:函数 一个 新函数 索引 数据      更新时间:2023-09-26

我有一个问题,在我的应用程序基于CodeIgniter。我的一个网页以两个函数的形式显示在控制器上的图形或图表:索引函数和数据函数,"函数索引"只包含调用显示,而函数数据则包含所取的数据。

我一直在尝试整合这两个函数,但失败了,该函数无法加载数据函数中包含的JSON数据,以下是我的控制器中的代码:

public function index(){
$this->load->view(index);
}
public function data(){
$nis = 10012589; $tahun = 1;
$category = array();
        $category['name'] = 'Category';
        $category['data'] = array('1','2','3','4','5','6','7','8','9','10','11','12');        
        $series1 = array();
        $series1['name'] = 'Masalah siswa';
        $series1['data'] = array(
            $dt[0]->h1,
            $dt[0]->h2,
            $dt[0]->h3,
            $dt[0]->h4,
            $dt[0]->h5,
            $dt[0]->h6,
            $dt[0]->h7,
            $dt[0]->h8,
            $dt[0]->h9,
            $dt[0]->h10,
            $dt[0]->h11,
            $dt[0]->h12
            ); 
        $result = array();
        array_push($result,$category);
        array_push($result,$series1);
    print json_encode($result, JSON_NUMERIC_CHECK);
}

和我的视图代码:

<script type="text/javascript">
        $(document).ready(function() {
            var options = {
                chart: {
                    renderTo: 'container',
                    type: 'column',
                    borderWidth: 2,
                    borderRadius: 20
                },
                xAxis: {                    
                    categories: [],
                    title: {
                        text: 'Topik Masalah'
                    }
            },
                series: []
            }
            /* */
            $.getJSON("data", function(json) {
                options.xAxis.categories = json[0]['data'];
                options.series[0] = json[1];                
                chart = new Highcharts.Chart(options);
            });
            /* */
        });
        </script>
<div id="container" ></div>
我想把代码统一到一个函数和一个
中但我不知道怎么做,因为我是个新手

我希望你明白我的要求
谢谢

问题是你正在打印json,这导致了这个问题,因为你正在跳过输出缓冲区。您还需要在输出类中设置标题。默认情况下,codeigniter设置HTML标头。但是jquery需要JSON标头。

您还需要使用output类而不是print方法输出数据,永远不要在视图之外的codeigniter中使用print或echo。以下是您需要的文档:http://ellislab.com/codeigniter/user-guide/libraries/output.html您需要的两个方法是set_output(data)set_content_type(content)。好运。