Professional CodeIgniter

(singke) #1

Chapter 6: Creating a Dashboard


178


The deleteProduct() function looks suspiciously like the deleteCategory() function created in the
previous section. And it should, too, as it was basically copied and the table name changed to products.

function deleteProduct($id){
$data = array(‘status’ = > ‘inactive’);
$this- > db- > where(‘id’, $id);
$this- > db- > update(‘products’, $data);
}

Creating the User Management Tools


Creating the User management tools will take the very same paths you took in creating Product and
Category management tools. In fact, you should be able to copy and paste your controllers, models, and
views, and with judicious name changing, create a basic framework in just a few minutes.

In fact, you already know a lot of what you ’ ll need in your model right now, so you might as well build
it out before you get started. For example, you know that you ’ ll need a way to retrieve one and all users.
You also need a way to add, update, and delete/deactivate a user.

Here ’ s the complete MAdmins model (remembering that you created the verifyUser() function
previously in the chapter). At this stage of the game, nothing in this model should be a surprise to you,
as the functions are all the standard ones for retrieving, adding, and updating database records.

< ?php

class MAdmins extends Model{

function MAdmins(){
parent::Model();
}

function verifyUser($u,$pw){
$this- > db- > select(‘id,username’);
$this- > db- > where(‘username’,$u);
$this- > db- > where(‘password’, $pw);
$this- > db- > where(‘status’, ‘active’);
$this- > db- > limit(1);
$Q = $this- > db- > get(‘admins’);
if ($Q- > num_rows() > 0){
$row = $Q- > row_array();
$_SESSION[‘userid’] = $row[‘id’];
$_SESSION[‘username’] = $row[‘username’];
}else{
$this- > session- > set_flashdata(‘error’, ‘Sorry, your username or password is
incorrect!’);
}
}

function getUser($id){
$data = array();
$options = array(‘id’ = > $id);
Free download pdf