Codeigniter_Blog_App/application/controllers/Categories.php

68 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2024-07-11 04:23:32 +00:00
<?php
class Categories extends CI_Controller {
public function index()
{
$data['title'] = 'Categories';
$data['categories'] = $this->category_model->get_categories();
$this->load->view('templates/header');
$this->load->view('categories/index', $data);
$this->load->view('templates/footer');
}
//To create a category
public function create()
{
//check login
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['title'] = 'Create category';
$this->form_validation->set_rules('name','Name','required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('categories/create', $data);
$this->load->view('templates/footer');
}
else
{
$this->category_model->create_category();
//flash message
$this->session->set_flashdata('category_created','New category has been created');
redirect('categories');
}
}
//to display the posts of each particular category
public function posts($id)
{
$data['title'] = $this->category_model->get_category($id)->name;
$data['posts'] = $this->post_model->get_posts_by_category($id);
$this->load->view('templates/header');
$this->load->view('posts/index', $data);
$this->load->view('templates/footer');
}
public function delete($id){
// Check login
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$this->category_model->delete_category($id);
// Set message
$this->session->set_flashdata('category_deleted', 'Your category has been deleted');
redirect('categories');
}
}