Why always "Invalid argument supplied for foreach"

606 Views Asked by At

i want make a chart on codeigniter but i always fail. pls help me wrong text : A PHP Error was encountered

Severity: Warning Message: Invalid argument supplied for foreach() Filename: chart/index.php Blockquote Line Number : 16

MY VIEW :

      <!-- **********************************************************************************************************************************************************
      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 -->

MY MODEL :

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

MY CONTROLLER :

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

    }

please give me solution...

2

There are 2 best solutions below

3
On BEST ANSWER

Change foreach($data as $data){ ....} to

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

or change your function get_chart to always return array:

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

0
On

VIEW:

Note: as $row

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

MODEL:

Note: we can just return result() as the foreach in your example is unnecessary/redundant. Important that we return false as well.

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

You can also do (a bit cleaner):

VIEW:

Notice if and endif

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

MODEL:

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