Professional CodeIgniter

(singke) #1

Chapter 7: Improving the Dashboard


213


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

Very similar things are in the edit() function. Notice the use of the $id argument to pull out the
appropriate color from the colors database.


function edit($id=0){
if ($this- > input- > post(‘name’)){
$this- > MColors- > updateColor();
$this- > session- > set_flashdata(‘message’,’Color updated’);
redirect(‘admin/colors/index’,’refresh’);
}else{
$data[‘title’] = “Edit Color”;
$data[‘main’] = ‘admin_colors_edit’;
$data[‘color’] = $this- > MColors- > getColor($id);
$this- > load- > vars($data);
$this- > load- > view(‘dashboard’);
}
}

Finally, here ’ s the delete() function. It also uses $id argument to act on the appropriate color ID.

function delete($id){
$this- > MColors- > deleteColor($id);
$this- > session- > set_flashdata(‘message’,’Color deleted’);
redirect(‘admin/colors/index’,’refresh’);
}

}//end class
? >

Now it ’ s time to create your MColors model functions. You ’ re going to need getColor() ,
getAllColors() , createColor() , updateColor() , and deleteColor(). Because you ’ ve built
similar functions in all of your models so far, they ’ re shown on the next page without extraneous context.


For a good refresher on how such functions are built, see the section on “ Creating the Category
Management Tools ” in Chapter 6.
Free download pdf