Welcome Guest, Not a member yet? Register   Sign In
Invalid argument supplied for foreach()
#1

[eluser]Unknown[/eluser]
dear friends

i have this problem on my edit page


A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: helpers/form_helper.php

Line Number: 331

this the dropdown from the form

Code:
<?php echo form_dropdown('parent_id', $pages_no_parents, $this->input->post('parent_id') ? $this->input->post('parent_id') : $page->parent_id, 'class="form-control"'); ?>

this is the controler

Code:
public function edit($id = NULL){
   // Fetch a page or set a new user
   if($id){
    $this->data['page'] = $this->page_m->get($id);
    count($this->data['page']) || $this->data['errors'][] = 'Page could not be found';
   }else {
    $this->data['page'] = $this->page_m->get_new($id);
   }
   // Pages for dropdown
   $this->data['pages_no_parents'] = $this->page_m->get_no_parents();
   dump($this->data['pages_no_parents']);
   // Set up the form
   $rules = $this->page_m->rules;
   $this->form_validation->set_rules($rules);
   // Load the view
   if($this->form_validation->run() == TRUE){
    $data = $this->page_m->array_from_post(array('title', 'slug', 'order', 'body'));
    $this->page_m->save($data, $id);
    redirect('admin/page');
   }
   $this->data['subview'] = 'admin/page/edit';
   $this->load->view('admin/_layout_main', $this->data);  
  }

and this is the model


Code:
public function get_new(){
   $page = new stdClass();
   $page->title = '';
   $page->slug = '';
   $page->order = '';
   $page->body = '';
   $page->parent_id = 0;
   return $page;
  }
  
  public function get_no_parents(){
   // Fetch pages without parents
   $this->db->select('id, title');
   $this->db->where('parent_id', 0);
   $pages = parent::get();
   // Return key => value pair array
   $array = array(0 => 'No parent');
   if(count($pages)){
    foreach($pages as $page){
     $array[$page->id] = $page->title;
    }
   }
  }

any solution to fix that?
#2

[eluser]jonez[/eluser]
Change:
Code:
if(count($pages)){
To:
Code:
if(count($pages)>0){
#3

[eluser]CroNiX[/eluser]
in your get_no_parents() method, you create an array for the options but don't do anything with it. Looks like you need to return $array after the loop.




Theme © iAndrew 2016 - Forum software by © MyBB