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:

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

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:

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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);
...


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x