为什么总是“为foreach提供了无效的参数”

时间:2018-07-30 07:48:35

标签: php sql codeigniter charts high-traffic

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

  

严重性:警告消息:为foreach()提供了无效的参数   文件名:chart / index.php   Blockquote行号: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);

    }

请给我解决方法...

2 个答案:

答案 0 :(得分:2)

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;
}

foreach help

答案 1 :(得分: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;
}