Professional CodeIgniter

(singke) #1

Chapter 8: Last-Minute Upgrades


241


The create() function checks for POST data. If there is a page name in the POST data, then add the
page using your model function addPage(). If there aren ’ t any POST data, show the adminpages
create view.


function create(){
if ($this- > input- > post(‘name’)){
$this- > MPages- > addPage();
$this- > session- > set_flashdata(‘message’,’Page created’);
redirect(‘admin/pages/index’,’refresh’);
}else{
$data[‘title’] = “Create Page”;
$data[‘main’] = ‘admin_pages_create’;
$this- > load- > vars($data);
$this- > load- > view(‘dashboard’);
}
}

The same thing goes for the edit() function. If you detect POST data, use updatePage() to update the
record in the database. If not, load the admin_pages_edit function and retrieve the appropriate
pages record using the passed - in argument $id.

function edit($id=0){
if ($this- > input- > post(‘name’)){
$this- > MPages- > updatePage();
$this- > session- > set_flashdata(‘message’,’Page updated’);
redirect(‘admin/pages/index’,’refresh’);
}else{
$data[‘title’] = “Edit Page”;
$data[‘main’] = ‘admin_pages_edit’;
$data[‘page’] = $this- > MPages- > getPage($id);
$this- > load- > vars($data);
$this- > load- > view(‘dashboard’);
}
}

Finally, the delete() function takes whatever you pass in as an argument ( $id ) and passes that value to
deletePage().

function delete($id){
$this- > MPages- > deletePage($id);
$this- > session- > set_flashdata(‘message’,’Page deleted’);
redirect(‘admin/pages/index’,’refresh’);
}

}//end class
? >
Free download pdf