I am new to codeigniter and I am trying to build a CMS by some tutorials on YouTube, but I am having a problem with updating/editing pages in admin area.
ERRORS:
- A PHP Error was encountered
Severity: Warning Message: Invalid argument supplied for foreach() Filename: helpers/form_helper.php Line Number: 332
- A PHP Error was encountered
Severity: Notice Message: Undefined property: stdClass::$title Filename: models/page_m.php Line Number: 77
Controller page.php
class Page extends Admin_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('page_m');
}
public function index(){
//Fetch all pages
$this->data['pages'] = $this->page_m->get_with_parents();
// Load view
$this->data['subview']='admin/page/index';
$this->load->view('admin/_layout_main', $this->data);
}
public function edit($id = NULL){
// Fetch a page or set a new one
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();
}
//Pages for dropdown
$this->data['pages_no_parents'] = $this->page_m->get_no_parents();
var_dump($this->data['pages_no_parents']);
//Set up the form
$rules = $this->page_m->rules;
$this->form_validation->set_rules($rules);
//Proccess the form
if ($this->form_validation->run() == TRUE){
$data = $this->page_m->array_from_post(array('title','slug','order','body', 'parent_id'));
$this->page_m->save($data, $id);
redirect('admin/page');
}
//Load the view
$this->data['subview'] = 'admin/page/edit';
$this->load->view('admin/_layout_main', $this->data);
}
public function _unique_slug($str){
// Do NOT validate if slug already exists
//UNLESS its the slug for the current page
$id = $this->uri->segment(5);
$this->db->where('slug', $this->input->post('slug'));
!$id || $this->db->where('id !=', $id);
$page = $this->page_m->get();
if (count($page)){
$this->form_validation->set_message('_unique_slug', '%s should be unique');
return FALSE;
}
return TRUE;
}}
Model page_m.php
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]'
),
'slug' => array(
'field' => 'slug',
'label' => 'Slug',
'rules' => 'trim|required|max_length[100]|url_title|callback__unique_slug'
),
'order' => array(
'field' => 'order',
'label' => 'Order',
'rules' => 'trim|required'
),
'body' => array(
'field' => 'body',
'label' => 'Body',
'rules' => 'trim|required'
),
);
public function get_new()
{
$page = new stdClass();
$page->title = "";
$page->slug = "";
$page->order = "";
$page->body = "";
$page->parent_id = 0;
return $page;
}
public function delete($id)
{
//Delete a page
parent::delete($id);
//Reset parent ID for its children
$this->db->set(array('parent_id' =>0))->where('parent_id', $id)->update($this->_table_name);
}
public function get_with_parents($id = NULL, $single = FALSE){
$this->db->select('pages.*, p.slug as parent_slug, p.title as parent_title');
$this->db->join('pages as p', 'pages.parent_id=p.id', 'left');
return parent::get($id, $single);
}
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;
}
}
}}
View admin/page/edit.php
<h3><?php echo empty($page->id) ? 'Add a new page' : 'Edit page: '.$page->title; ?></h3>
<div class="modal-body">
<?php echo validation_errors(); ?>
<?php echo form_open(); ?>
<table class="table">
<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>
<tr>
<td>Title</td>
<td><?php echo form_input('title', set_value('title',$page->title)); ?></td>
</tr>
<tr>
<td>Slug</td>
<td><?php echo form_input('slug', set_value('slug', $page->slug)); ?></td>
</tr>
<tr>
<td>Order</td>
<td><?php echo form_input('order', set_value('order', $page->order)); ?></td>
</tr>
<tr>
<td>Body</td>
<td><?php echo form_textarea('body', set_value('body', $page->body)); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
</tr>
</table>
<?php echo form_close(); ?>
</div>
View admin/page/index.php
<section>
<h2>Pages</h2>
<?php echo anchor('admin/page/edit' , '<i class="glyphicon glyphicon-plus"></i> Add a page'); ?>
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Parent</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php if (count($pages)): foreach ($pages as $page): ?>
<tr>
<td><?php echo anchor('admin/page/edit/' . $page->id, $page->title); ?></td>
<td><?php echo $page->parent_slug; ?></td>
<td><?php echo btn_edit('admin/page/edit/' . $page->id); ?></td>
<td><?php echo btn_delete('admin/page/delete/' . $page->id); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td class="col-sm-3"> We could not find any pages.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</section>
Thank You for your help.
</div>