Now, most of the functions we require further are already created. We can easily create, insert, delete and update menu tables and post tables by simply creating a separate controller file for them and connecting them to the main Model. Here, we're going to create a news table as there will be few extra features to be added and that'll help you learn more. Let's create a form for the news category before working on the news table then.
We are creating a news category table which has category name as index that'll speed up the search query processing if required in the future.
CREATE TABLE categories( id int unsigned PRIMARY KEY AUTO_INCREMENT, name varchar(100) NOT null, INDEX(name) )
Let's create a form to add news categories then.
<?php if (Request::method() && token::check(Request::post('csrf_token'))){ $obj = new Category(); $obj -> addCategory(); } ?> <form method="POST"> <div class="col-sm-6"> <br> <?= Validation::displayErrors('validationErrors'); ?> <?= Message(); ?> </div> <div class="col-sm-6"> <?=token::input();?> <div class="form-group"> <label>Category name</label> <input type="text" name="name" class="form-control"> </div> <div class="form-group"> <button type="submit" class="btn btn-success"><i class="fa fa-address-book"></i> Add Category</button> </div> </div> </form>
The form contains a single input field that accepts the news category name. To handle the request from this form, we'll create a new controller and to add requested data from this post we'll create a function addCategory(). The controller has already been instantiated on top and the addCategory() function is also called. Let's create the controller and required function then.
here's the validation rules, database and validation class instantiation along with the function to validate and add news category.
<?php class Category extends Model { protected $table = "categories"; protected $key = "id"; protected $field = "*"; public $pagination = null; private $_validation = null; protected $limit = 0; protected $offset = 0; protected $validationRules = [ 'name' => [ 'required' => true, 'unique' => 'categories.name', 'minlength' => 3, 'maxlength' => 50, 'label' => 'Category Name', ] ]; public function __construct() { parent::__construct(); $this->_validation = new Validation(); } public function addCategory() { $data['name'] = Request::post('name'); try{ $this->_validation->validate($this->validationRules); if($this->_validation->isValid()){ if ($this->save($data)) { session::put('success','Category Created Successfully'); return; } }else{ session::put('validationErrors',$this->_validation->getErrors()); return; } }catch(Exception $e){ die($e->getMessage()); } } }
So, the validation rule for category is created and the database instantiated along with the validation class. If the validation process succeeds, the data will be inserted.
Here's the controller function to fetch data from categories table. It'll fetch entire result set if the id is empty while fetching single row data as per id in case the id is sent in parameter.
public function getCategory($id=""){ return $this->get($id); }
Here's the page that displays all data from categories table in a view table.
<?php $obj = new category(); $categories = $obj->getCategory(); ?> <div class="col-sm-12"> <?= Message(); ?> <table class="table table-bordered table-striped table-hover"> <thead> <tr> <th>S.N.</th> <th>Category Name</th> <th>Edit</th> <th>Delete</th> </tr> </thead> <tbody> <?php if(empty($categories)){ ?> <td colspan="6">No Data Found <a href="main.php?page=addCategory">Add Category</a></td> <?php }else{ ?> <?php foreach($categories as $key => $category): ?> <tr> <td><?= ++$key ?></td> <td><?= ucfirst($category->name)?></td> <td> <a href="main.php?page=editCategory&cid=<?=$category->id?>" class="btn btn-sm btn-success"><i class="fa fa-edit"></i> Edit</a> </td> <td> <a href="delete.php?cid=<?=$category->id?>" onclick="return confirm('Are you sure?')" class="btn btn-sm btn-danger"><i class="fa fa-trash"></i> Delete</a> </td> </tr> <?php endforeach; ?> <?php } ?> </tbody> </table> </div>
Here's the controller function to delete category.
public function deleteCategory() { $id = (int)Request::get('cid'); if(empty($id)) return false; $user = $this->getCategory($id); if ($this->delete($id)){ session::put('success','Category was deleted.'); return Redirect::to('admin/displayCategory/'); }else{ session::put('error','Unable to delete Category'); return Redirect::to('admin/displayCategory/'); } }
We need to modify the delete.php file as well in order to delete any other table's data.
if(!empty(Request::get('u_id'))){ $user = new User(); $user->deleteUser(); }elseif(!empty(Request::get('cid'))){ $category = new Category(); $category->deleteCategory(); }else{ echo 'Id not set'; }
Here's the controller function to validate and update category.
public function updateCategory(){ $id = (int)Request::post('id'); if(empty($id)) return false; $data['name'] = Request::post('name'); $id = (int)Request::post('id'); try{ $this->validationRules['name']['unique'] = "categories.name.id.".$id; $this->_validation->validate($this->validationRules); if($this->_validation->isValid()){ if($this->save($data,$id)){ session::put('success','Category updated Successfully'); return Redirect::to('admin/displayCategory/'); } }else{ session::put('validationErrors',$this->_validation->getErrors()); return; } }catch(Exception $e){ die($e->getMessage()); } }
The page to update category is displayed here.
<?php $obj = new Category(); $id = Request::get('cid'); $category = $obj->getCategory($id); if(Request::method() && token::check(Request::post('csrf_token'))){ $obj->updateCategory(); } ?> <div class="clearfix"></div> <form method="POST"> <div class="col-sm-6"> <br> <?= Validation::displayErrors('validationErrors'); ?> <?= Message(); ?> </div> <div class="clearfix"></div> <div class="col-sm-6"> <?=token::input();?> <input type="hidden" name="id" value="<?=$category->id?>"> <div class="form-group"> <label>Category name</label> <input type="text" value="<?=$category->name?>" name="name" class="form-control"> </div> <div class="form-group"> <button type="submit" class="btn btn-success"><i class="fa fa-address-book"></i> Update Category</button> </div> </div> </form>
Leave a comment