[eluser]soluicius[/eluser]
Hi guys.
I have a problem with dynamic dropdown. I get this error:
Code:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: helpers/form_helper.php
Line Number: 331
my view:
Code:
<tr>
<td>Parent</td>
<td><?php echo form_dropdown('parent_id', $pages_no_parents, $this->input->post('parent_id') ? $this->input->post('parent_id') : $page->parent_id); ?></td>
</tr>
here is my model:
Code:
class Page_m extends MY_Model {
protected $_table_name = 'pages';
protected $_order_by = 'order';
public $rules = array(
'parent_id' => array(
'field' => 'parent_id',
'label' => 'Parent',
'rules' => 'trim|intval'
),
'title' => array(
'field' => 'title',
'label' => 'Title',
'rules' => 'trim|required|max_length[100]|xss_clean'
),
'slug' => array(
'field' => 'slug',
'label' => 'Slug',
'rules' => 'trim|required|max_length[100]|url_title|callback__unique_slug|xss_clean'
),
'body' => array(
'field' => 'body',
'label' => 'Body',
'rules' => 'trim|required'
),
);
public function get_new() {
$page = new stdClass();
$page->title = '';
$page->slug = '';
$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;
}
}
}
}
and finally controller:
Code:
//Pages for dropdown
$this->data['pages_no_parents'] = $this->page_m->get_no_parents();
dump($this->data['pages_no_parents']);