Professional CodeIgniter

(singke) #1

Chapter 8: Last-Minute Upgrades


240


Finally, the deletePage() function lets you deactivate a record in the Pages database table. Just like
with products, it ’ s probably not such a good idea to actually delete records. Instead, just setting their
status indicators to “ inactive ” is enough.

function deletePage($id){
$data = array(‘status’ = > ‘inactive’);
$this- > db- > where(‘id’, $id);
$this- > db- > update(‘pages’, $data);
}
}//end class
? >

One last thing before moving on to the controller: Don ’ t forget to add MPages to the autoloaded models
list in your autoload.php.

$autoload[‘model’] = array(‘MCats’, ‘MProducts’, ‘MOrders’, ‘MAdmins’, ‘MSizes’,
‘MColors’, ‘MPages’);

Creating the Admin/Pages Controller


The admin/pages controller is also extremely simple and is based on all the other controllers you ’ ve
created so far in the administrative component. In fact, at this point you could copy and paste any of the
other controllers you ’ ve already built and make serious headway here.

In this Pages controller, you need the standard set of functions: index() , create() , edit() , and
delete(). When you start the Pages controller, don ’ t forget to check for the presence of the session
variable userID. If it hasn ’ t been set, send the user back to the Welcome controller, so he or she can log in.

< ?php

class Pages extends Controller {
function Pages(){
parent::Controller();
session_start();

if ($_SESSION[‘userid’] < 1){
redirect(‘welcome/verify’,’refresh’);
}
}

The index() function is very straightforward. Simply load all the pages from the database with
getAllPages() from the MPages model, and load a view called admin_pages_home.

function index(){
$data[‘title’] = “Manage Pages”;
$data[‘main’] = ‘admin_pages_home’;
$data[‘pages’] = $this- > MPages- > getAllPages();
$this- > load- > vars($data);
$this- > load- > view(‘dashboard’);
}
Free download pdf