Search code examples
phpmysqlcodeigniter

Invalid argument supplied for foreach() while filling CodeIgniter MVC table


I'm new to PHP/CodeIgniter and am making a table that retrieves the count of values from Mysql within a particular timeframe.

Controller Class:

public function totallistings($slug='')
    {
        $this->load->library('pagination');
        $main['page_title']=$this->config->item('site_name').' - Listings Reports';
        $main['header']=$this->adminheader();
        $main['footer']=$this->adminfooter();
        $main['left']=$this->adminleftmenu();  
        $content='';
        $fdate = $this->input->post("fdate");
        $content['tdate'] = $tdate = $this->input->post("tdate");
        if(isset($fdate)){
            $content['fdate'] =$fdate;
        }else{
            $content['fdate'] = '';
        }
        if(isset($tdate)){
            $content['tdate'] =$tdate;
        }else{
            $content['tdate'] ='';
        }
        $main['content']=$this->load->view('crm/reports/totallistings',$content,true);
        $main['jsArray'] = array('public/assets/plugins/datatables/jquery.dataTables.min.js' );  
        $main['cssArray'] = array('public/assets/plugins/datatables/jquery.dataTables.min.css','public/assets/css/reports.css');
        $this->load->view('crm/main',$main);
        $content['groupedleads'] = $this->leads_model->get_listingstatus($fdate,$tdate);
    }

But this gave me the a Invalid argument supplied for foreach() error on my webpage:

enter image description here

Basically I want to have it as something like this, where you just pass the start date and end date, and it returns a table with each status_to value count:

enter image description here


Solution

  • You need to set the data into the array before you load the view or the value groupedleads won't exist inside the view.

    In your totallistings()-method, move the last line:

    $content['groupedleads'] = $this->leads_model->get_listingstatus($fdate,$tdate);
    

    and put it before you load the view:

    $content['groupedleads'] = $this->leads_model->get_listingstatus($fdate,$tdate);
    $main['content'] = $this->load->view('crm/reports/totallistings',$content,true);
    ...