-3

我想在codeigniter上做一个图表,但我总是失败。请帮助我错误的文本:遇到 PHP 错误

严重性:警告消息:为 foreach() 提供的参数无效 文件名:chart/index.php 块引用行号:16

我的观点 :

      <!-- **********************************************************************************************************************************************************
      MAIN CONTENT
      *********************************************************************************************************************************************************** -->
      <!--main content start-->
      <section id="main-content">
          <section class="wrapper">
            <!-- BASIC FORM ELELEMNTS -->
            <div class="row mt">

                <div class="col-lg-12">

   <?php
   #periksa array
        foreach($data as $data){
            $id_reservasi[] = $data->id_reservasi;
            $stok[] = (float) $data->stok;
        }
    ?>
</head>
<body>

    <canvas id="canvas" width="1000" height="280"></canvas>

    <!--Load chart js-->
    <script type="text/javascript" src="<?php echo base_url().'assets/chartjs/chart.min.js'?>"></script>
    <script>

            var lineChartData = {
                labels : <?php echo json_encode($id_reservasi);?>,
                datasets : [

                    {
                        fillColor: "rgba(60,141,188,0.9)",
                        strokeColor: "rgba(60,141,188,0.8)",
                        pointColor: "#3b8bba",
                        pointStrokeColor: "#fff",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(152,235,239,1)",
                        data : <?php echo json_encode($stok);?>
                    }

                ]

            }

        var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(lineChartData);

    </script>
      </div><!-- /col-lg-4 -->  
            </div><!-- /row -->

        </section>
      </section><!-- /MAIN CONTENT -->

我的模型:

function get_chart(){
    $query = $this->db->query("SELECT id_reservasi,SUM(status_reservasi) AS stok FROM tbl_reservasi GROUP BY id_reservasi");          
        if($query->num_rows() > 0){
            foreach($query->result() as $data){
                $hasil[] = $data;
            }
            return $hasil;
        }
    }

我的控制器:

public function chart() {
        $x['data']=$this->sistem_model->get_chart();
        $this->template_system->load('template_system','sistem/data/chart/index',$x);

    }

请给我解决方案...

4

2 回答 2

3

更改foreach($data as $data){ ....}

if ( is_array($data) ) {
   foreach($data as $item){
      $id_reservasi[] = $item->id_reservasi;
      $stok[] = (float) $item->stok;
   }
}

或将您的函数更改get_chart为始终返回数组:

function get_chart(){
    $hasil = [];
    $query = $this->db->query("SELECT id_reservasi,SUM(status_reservasi) AS stok FROM tbl_reservasi GROUP BY id_reservasi");          
    if($query->num_rows() > 0){
        foreach($query->result() as $data){
             $hasil[] = $data;
        }
    }
    return $hasil;
}

帮助

于 2018-07-30T07:55:17.107 回答
0

看法:

笔记:as $row

if ($data) {
    foreach ($data as $row) {
        $id_reservasi[] = $row->id_reservasi;
        $stok[] = (float) $row->stok;
    }
} else {
    echo 'no data';
}

模型:

注意:我们可以返回result(),因为foreach您的示例中的 是不必要的/冗余的。重要的是我们也返回 false。

function get_chart() {
    $query = $this->db->query("SELECT id_reservasi,SUM(status_reservasi) AS stok FROM tbl_reservasi GROUP BY id_reservasi");
    if ($query->num_rows() > 0) {
        return $query->result();
    }
    return false;
}

你也可以做(更干净一点):

看法:

通知ifendif

<div class="col-lg-12">

   <?php
   /*
   #periksa array
        foreach($data as $data){
            $id_reservasi[] = $data->id_reservasi;
            $stok[] = (float) $data->stok;
        }
    */
    ?>
</head>
<body>
<?php if ($data): ?>
    <canvas id="canvas" width="1000" height="280"></canvas>

    <!--Load chart js-->
    <script type="text/javascript" src="<?php echo base_url().'assets/chartjs/chart.min.js'?>"></script>
    <script>

            var lineChartData = {
                labels : <?php echo json_encode($data['id_reservasi']);?>,
                datasets : [

                    {
                        fillColor: "rgba(60,141,188,0.9)",
                        strokeColor: "rgba(60,141,188,0.8)",
                        pointColor: "#3b8bba",
                        pointStrokeColor: "#fff",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(152,235,239,1)",
                        data : <?php echo json_encode($data['stok']);?>
                    }

                ]

            }

        var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(lineChartData);

    </script>
   <?php endif; ?>
      </div><!-- /col-lg-4 -->  
            </div><!-- /row -->

        </section>
      </section><!-- /MAIN CONTENT -->

模型:

function get_chart() {
    $query = $this->db->query("SELECT id_reservasi,SUM(status_reservasi) AS stok FROM tbl_reservasi GROUP BY id_reservasi");
    if ($query->num_rows() > 0) {
        $data = array();
        foreach ($query->result() as $row) {
            $data['id_reservasi'][] = $row->id_reservasi;
            $data['stok'][] = $row->stok;
        }
        return $data;
    }
    return false;
}
于 2018-07-30T08:28:55.937 回答