├── application ├── views │ ├── news │ │ ├── success.php │ │ ├── view.php │ │ ├── create.php │ │ ├── edit.php │ │ └── index.php │ ├── templates │ │ ├── footer.php │ │ └── header.php │ └── user │ │ ├── login.php │ │ └── register.php ├── config │ └── routes.php ├── models │ ├── User_model.php │ └── News_model.php └── controllers │ ├── User.php │ └── News.php ├── README.md └── database.sql /application/views/news/success.php: -------------------------------------------------------------------------------- 1 |

News added successfully!

2 | -------------------------------------------------------------------------------- /application/views/templates/footer.php: -------------------------------------------------------------------------------- 1 |

Copyright © 2016

2 | 3 | 4 | -------------------------------------------------------------------------------- /application/views/news/view.php: -------------------------------------------------------------------------------- 1 | '.$news_item['title'].''; 3 | echo $news_item['text']; 4 | -------------------------------------------------------------------------------- /application/config/routes.php: -------------------------------------------------------------------------------- 1 | 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | CodeIgniter: Add, Edit, Delete, View with Login & Registration – MVC CRUD Application 2 | ======== 3 | 4 | A simple and basic CRUD application with login and registration feature using CodeIgniter PHP Framework. 5 | 6 | Blog Article: [CodeIgniter: Add, Edit, Delete, View with Login & Registration – MVC CRUD Application](http://blog.chapagain.com.np/codeigniter-add-edit-delete-view-with-login-registration-mvc-crud-application/) 7 | -------------------------------------------------------------------------------- /application/views/templates/header.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | CodeIgniter Tutorial 4 | 5 | 6 |

Simple CRUD

7 |

8 | Home | 9 | Add News | 10 | 11 | session->userdata('is_logged_in')) { 12 | echo 'Logged in as: ' . $this->session->userdata('email'); 13 | echo ' | ' . "Logout"; 14 | } else { 15 | ?> 16 | Register | 17 | Login 18 | 19 |

20 | -------------------------------------------------------------------------------- /application/views/news/create.php: -------------------------------------------------------------------------------- 1 |

2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /application/views/news/edit.php: -------------------------------------------------------------------------------- 1 |

2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /application/views/news/index.php: -------------------------------------------------------------------------------- 1 |

2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 23 | 24 | 25 |
TitleContentAction
14 | View 15 | 16 | session->userdata('is_logged_in')) { ?> 17 | | 18 | Edit | 19 | Delete 20 | 21 | 22 |
26 | -------------------------------------------------------------------------------- /application/views/user/login.php: -------------------------------------------------------------------------------- 1 |

2 | session->flashdata('verify_msg'); ?> 3 |

4 | 5 |

User Login Form

6 | 7 | "loginform"); 8 | echo form_open("user/login", $attributes);?> 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 |

session->flashdata('msg_success'); ?>

24 |

session->flashdata('msg_error'); ?>

25 | -------------------------------------------------------------------------------- /database.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Create database `test` 3 | -- 4 | 5 | CREATE DATABASE `test`; 6 | 7 | use `test`; 8 | 9 | -- 10 | -- Table structure for table `news` 11 | -- 12 | 13 | CREATE TABLE IF NOT EXISTS `news` ( 14 | `id` int(11) NOT NULL AUTO_INCREMENT, 15 | `title` varchar(128) NOT NULL, 16 | `slug` varchar(128) NOT NULL, 17 | `text` text NOT NULL, 18 | `user_id` int(11) NOT NULL DEFAULT '0', 19 | PRIMARY KEY (`id`), 20 | KEY `slug` (`slug`) 21 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; 22 | 23 | -- 24 | -- Dumping data for table `news` 25 | -- 26 | 27 | INSERT INTO `news` (`id`, `title`, `slug`, `text`, `user_id`) VALUES 28 | (1, 'Test', 'test', 'Hello World !!', 1), 29 | (2, 'What is Lorem Ipsum?', 'what-is-lorem-ipsum', 'Lorem Ipsum is simply dummy text.', 1), 30 | (3, 'My test', 'my-test', 'hello there', 2); 31 | 32 | -- 33 | -- Table structure for table `user` 34 | -- 35 | 36 | CREATE TABLE `user` ( 37 | `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, 38 | `firstname` varchar(255) NOT NULL, 39 | `lastname` varchar(255) NOT NULL, 40 | `email` varchar(255) NOT NULL, 41 | `password` varchar(255) NOT NULL, 42 | `updated_at`varchar(255) NULL DEFAULT NULL, 43 | PRIMARY_KEY (`id`) 44 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1; 45 | 46 | -- 47 | -- Indexes for table `user` 48 | -- 49 | ALTER TABLE `user` 50 | ADD UNIQUE KEY `user_email_unique` (`email`); 51 | -------------------------------------------------------------------------------- /application/models/User_model.php: -------------------------------------------------------------------------------- 1 | load->database(); 7 | } 8 | 9 | public function get_user($id = 0) 10 | { 11 | if ($id === 0) 12 | { 13 | $query = $this->db->get('user'); 14 | return $query->result_array(); 15 | } 16 | 17 | $query = $this->db->get_where('user', array('id' => $id)); 18 | return $query->row_array(); 19 | } 20 | 21 | public function get_user_login($email, $password) 22 | { 23 | $query = $this->db->get_where('user', array('email' => $email, 'password' => md5($password))); 24 | //return $query->num_rows(); 25 | return $query->row_array(); 26 | } 27 | 28 | public function set_user($id = 0) 29 | { 30 | $data = array( 31 | 'firstname' => $this->input->post('firstname'), 32 | 'lastname' => $this->input->post('lastname'), 33 | 'email' => $this->input->post('email'), 34 | 'password' => $this->input->post('password'), 35 | 'updated_at' => date('Y-m-d H:i:s') 36 | ); 37 | 38 | if ($id == 0) { 39 | return $this->db->insert('user', $data); 40 | } else { 41 | $this->db->where('id', $id); 42 | return $this->db->update('user', $data); 43 | } 44 | } 45 | 46 | public function delete_user($id) 47 | { 48 | $this->db->where('id', $id); 49 | return $this->db->delete('user'); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /application/views/user/register.php: -------------------------------------------------------------------------------- 1 |

2 | session->flashdata('verify_msg'); ?> 3 |

4 | 5 |

User Registration Form

6 | 7 | "registrationform"); 8 | echo form_open("user/register", $attributes);?> 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 | 32 | 33 |

session->flashdata('msg_success'); ?>

34 |

session->flashdata('msg_error'); ?>

35 | -------------------------------------------------------------------------------- /application/models/News_model.php: -------------------------------------------------------------------------------- 1 | load->database(); 7 | } 8 | 9 | public function record_count() 10 | { 11 | return $this->db->count_all('news'); 12 | } 13 | 14 | public function get_news($slug = FALSE) 15 | { 16 | if ($slug === FALSE) 17 | { 18 | $query = $this->db->get_where('news', array('user_id' => $this->session->userdata('user_id'))); 19 | return $query->result_array(); // $query->result(); // returns object 20 | } 21 | 22 | $query = $this->db->get_where('news', array('slug' => $slug, 'user_id' => $this->session->userdata('user_id'))); 23 | return $query->row_array(); // $query->row(); // returns object 24 | 25 | // $query->num_rows(); // returns number of rows selected, similar to counting rows 26 | // $query->num_fields(); // returns number of fields selected 27 | } 28 | 29 | public function get_news_by_id($id = 0) 30 | { 31 | if ($id === 0) 32 | { 33 | $query = $this->db->get('news'); 34 | return $query->result_array(); 35 | } 36 | 37 | $query = $this->db->get_where('news', array('id' => $id)); 38 | return $query->row_array(); 39 | } 40 | 41 | public function set_news($id = 0) 42 | { 43 | $this->load->helper('url'); 44 | 45 | $slug = url_title($this->input->post('title'), 'dash', TRUE); 46 | 47 | $data = array( 48 | 'title' => $this->input->post('title'), // $this->db->escape($this->input->post('title')) 49 | 'slug' => $slug, 50 | 'text' => $this->input->post('text'), 51 | 'user_id' => $this->input->post('user_id'), 52 | ); 53 | 54 | if ($id == 0) { 55 | //$this->db->query('YOUR QUERY HERE'); 56 | return $this->db->insert('news', $data); 57 | } else { 58 | $this->db->where('id', $id); 59 | return $this->db->update('news', $data); 60 | } 61 | } 62 | 63 | public function delete_news($id) 64 | { 65 | $this->db->where('id', $id); 66 | return $this->db->delete('news'); // $this->db->delete('news', array('id' => $id)); 67 | 68 | // error() method will return an array containing its code and message 69 | // $this->db->error(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /application/controllers/User.php: -------------------------------------------------------------------------------- 1 | load->model('user_model'); 8 | $this->load->helper(array('form', 'url')); 9 | $this->load->library(array('session', 'form_validation')); 10 | } 11 | 12 | public function index() 13 | { 14 | $this->register(); 15 | } 16 | 17 | public function register() 18 | { 19 | $this->form_validation->set_rules('firstname', 'First Name', 'trim|required|alpha|min_length[3]|max_length[50]'); 20 | $this->form_validation->set_rules('lastname', 'Last Name', 'trim|required|alpha|min_length[3]|max_length[50]'); 21 | $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[user.email]'); 22 | $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[cpassword]|md5'); 23 | $this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required'); 24 | 25 | $data['title'] = 'Register'; 26 | 27 | if ($this->form_validation->run() === FALSE) 28 | { 29 | $this->load->view('templates/header', $data); 30 | $this->load->view('user/register'); 31 | $this->load->view('templates/footer'); 32 | 33 | } 34 | else 35 | { 36 | if ($this->user_model->set_user()) 37 | { 38 | $this->session->set_flashdata('msg_success','Registration Successful!'); 39 | redirect('user/register'); 40 | } 41 | else 42 | { 43 | $this->session->set_flashdata('msg_error','Error! Please try again later.'); 44 | redirect('user/register'); 45 | } 46 | } 47 | } 48 | 49 | public function login() 50 | { 51 | $email = $this->input->post('email'); 52 | $password = $this->input->post('password'); 53 | 54 | $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email'); 55 | $this->form_validation->set_rules('password', 'Password', 'trim|required|md5'); 56 | 57 | $data['title'] = 'Login'; 58 | 59 | if ($this->form_validation->run() === FALSE) 60 | { 61 | $this->load->view('templates/header', $data); 62 | $this->load->view('user/login'); 63 | $this->load->view('templates/footer'); 64 | 65 | } 66 | else 67 | { 68 | if ($user = $this->user_model->get_user_login($email, $password)) 69 | { 70 | /*$user_data = array( 71 | 'email' => $email, 72 | 'is_logged_in' => true 73 | ); 74 | 75 | $this->session->set_userdata($user_data);*/ 76 | 77 | $this->session->set_userdata('email', $email); 78 | $this->session->set_userdata('user_id', $user['id']); 79 | $this->session->set_userdata('is_logged_in', true); 80 | 81 | 82 | $this->session->set_flashdata('msg_success','Login Successful!'); 83 | redirect('news'); 84 | } 85 | else 86 | { 87 | $this->session->set_flashdata('msg_error','Login credentials does not match!'); 88 | 89 | $currentClass = $this->router->fetch_class(); // class = controller 90 | $currentAction = $this->router->fetch_method(); // action = function 91 | 92 | redirect("$currentClass/$currentAction"); 93 | //redirect('user/login'); 94 | } 95 | } 96 | } 97 | 98 | public function logout() 99 | { 100 | if ($this->session->userdata('is_logged_in')) { 101 | 102 | //$this->session->unset_userdata(array('email' => '', 'is_logged_in' => '')); 103 | $this->session->unset_userdata('email'); 104 | $this->session->unset_userdata('is_logged_in'); 105 | $this->session->unset_userdata('user_id'); 106 | } 107 | redirect('news'); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /application/controllers/News.php: -------------------------------------------------------------------------------- 1 | load->model('news_model'); 8 | $this->load->helper('url_helper'); 9 | $this->load->library('session'); 10 | $this->load->library('pagination'); 11 | } 12 | 13 | public function index() 14 | { 15 | $data['news'] = $this->news_model->get_news(); 16 | $data['title'] = 'News archive'; 17 | 18 | $this->load->view('templates/header', $data); 19 | $this->load->view('news/index', $data); 20 | $this->load->view('templates/footer'); 21 | } 22 | 23 | public function view($slug = NULL) 24 | { 25 | $data['news_item'] = $this->news_model->get_news($slug); 26 | 27 | if (empty($data['news_item'])) { 28 | show_404(); 29 | } 30 | 31 | $data['title'] = $data['news_item']['title']; 32 | 33 | $this->load->view('templates/header', $data); 34 | $this->load->view('news/view', $data); 35 | $this->load->view('templates/footer'); 36 | } 37 | 38 | public function create() 39 | { 40 | if (!$this->session->userdata('is_logged_in')) { 41 | redirect(site_url('user/login')); 42 | } else { 43 | $data['user_id'] = $this->session->userdata('user_id'); 44 | } 45 | 46 | $this->load->helper('form'); 47 | $this->load->library('form_validation'); 48 | 49 | $data['title'] = 'Create a news item'; 50 | 51 | $this->form_validation->set_rules('title', 'Title', 'required'); 52 | $this->form_validation->set_rules('text', 'Text', 'required'); 53 | 54 | if ($this->form_validation->run() === FALSE) { 55 | $this->load->view('templates/header', $data); 56 | $this->load->view('news/create'); 57 | $this->load->view('templates/footer'); 58 | } else { 59 | $this->news_model->set_news(); 60 | $this->load->view('templates/header', $data); 61 | $this->load->view('news/success'); 62 | $this->load->view('templates/footer'); 63 | } 64 | } 65 | 66 | public function edit() 67 | { 68 | if (!$this->session->userdata('is_logged_in')) { 69 | redirect(site_url('user/login')); 70 | } else { 71 | $data['user_id'] = $this->session->userdata('user_id'); 72 | } 73 | 74 | $id = $this->uri->segment(3); 75 | //$id = $this->input->post('id'); 76 | 77 | if (empty($id)) { 78 | show_404(); 79 | } 80 | 81 | $this->load->helper('form'); 82 | $this->load->library('form_validation'); 83 | 84 | $data['title'] = 'Edit a news item'; 85 | $data['news_item'] = $this->news_model->get_news_by_id($id); 86 | 87 | if ($data['news_item']['user_id'] != $this->session->userdata('user_id')) { 88 | $currentClass = $this->router->fetch_class(); // class = controller 89 | redirect(site_url($currentClass)); 90 | } 91 | 92 | $this->form_validation->set_rules('title', 'Title', 'required'); 93 | $this->form_validation->set_rules('text', 'Text', 'required'); 94 | 95 | if ($this->form_validation->run() === FALSE) { 96 | $this->load->view('templates/header', $data); 97 | $this->load->view('news/edit', $data); 98 | $this->load->view('templates/footer'); 99 | } else { 100 | $this->news_model->set_news($id); 101 | //$this->load->view('news/success'); 102 | redirect( site_url('news') ); 103 | } 104 | } 105 | 106 | public function delete() 107 | { 108 | if (!$this->session->userdata('is_logged_in')) { 109 | redirect(site_url('user/login')); 110 | } 111 | 112 | $id = $this->uri->segment(3); 113 | 114 | if (empty($id)) { 115 | show_404(); 116 | } 117 | 118 | $news_item = $this->news_model->get_news_by_id($id); 119 | 120 | if ($news_item['user_id'] != $this->session->userdata('user_id')) { 121 | $currentClass = $this->router->fetch_class(); // class = controller 122 | redirect(site_url($currentClass)); 123 | } 124 | 125 | $this->news_model->delete_news($id); 126 | redirect( base_url() . 'index.php/news'); 127 | } 128 | } 129 | --------------------------------------------------------------------------------