├── README.md ├── application ├── config │ └── routes.php ├── controllers │ └── News.php ├── models │ └── News_model.php └── views │ ├── news │ ├── create.php │ ├── edit.php │ ├── index.php │ ├── success.php │ └── view.php │ └── templates │ ├── footer.php │ └── header.php └── database.sql /README.md: -------------------------------------------------------------------------------- 1 | CodeIgniter: Simple Add, Edit, Delete, View – MVC CRUD Application 2 | ======== 3 | 4 | A simple and basic CRUD application using CodeIgniter PHP Framework. 5 | 6 | Blog Article: [CodeIgniter: Simple Add, Edit, Delete, View – MVC CRUD Application](http://blog.chapagain.com.np/codeigniter-simple-mvc-crud-add-edit-delete-view/) 7 | -------------------------------------------------------------------------------- /application/config/routes.php: -------------------------------------------------------------------------------- 1 | 12 | -------------------------------------------------------------------------------- /application/controllers/News.php: -------------------------------------------------------------------------------- 1 | load->model('news_model'); 8 | $this->load->helper('url_helper'); 9 | } 10 | 11 | public function index() 12 | { 13 | $data['news'] = $this->news_model->get_news(); 14 | $data['title'] = 'News archive'; 15 | 16 | $this->load->view('templates/header', $data); 17 | $this->load->view('news/index', $data); 18 | $this->load->view('templates/footer'); 19 | } 20 | 21 | public function view($slug = NULL) 22 | { 23 | $data['news_item'] = $this->news_model->get_news($slug); 24 | 25 | if (empty($data['news_item'])) 26 | { 27 | show_404(); 28 | } 29 | 30 | $data['title'] = $data['news_item']['title']; 31 | 32 | $this->load->view('templates/header', $data); 33 | $this->load->view('news/view', $data); 34 | $this->load->view('templates/footer'); 35 | } 36 | 37 | public function create() 38 | { 39 | $this->load->helper('form'); 40 | $this->load->library('form_validation'); 41 | 42 | $data['title'] = 'Create a news item'; 43 | 44 | $this->form_validation->set_rules('title', 'Title', 'required'); 45 | $this->form_validation->set_rules('text', 'Text', 'required'); 46 | 47 | if ($this->form_validation->run() === FALSE) 48 | { 49 | $this->load->view('templates/header', $data); 50 | $this->load->view('news/create'); 51 | $this->load->view('templates/footer'); 52 | 53 | } 54 | else 55 | { 56 | $this->news_model->set_news(); 57 | $this->load->view('templates/header', $data); 58 | $this->load->view('news/success'); 59 | $this->load->view('templates/footer'); 60 | } 61 | } 62 | 63 | public function edit() 64 | { 65 | $id = $this->uri->segment(3); 66 | 67 | if (empty($id)) 68 | { 69 | show_404(); 70 | } 71 | 72 | $this->load->helper('form'); 73 | $this->load->library('form_validation'); 74 | 75 | $data['title'] = 'Edit a news item'; 76 | $data['news_item'] = $this->news_model->get_news_by_id($id); 77 | 78 | $this->form_validation->set_rules('title', 'Title', 'required'); 79 | $this->form_validation->set_rules('text', 'Text', 'required'); 80 | 81 | if ($this->form_validation->run() === FALSE) 82 | { 83 | $this->load->view('templates/header', $data); 84 | $this->load->view('news/edit', $data); 85 | $this->load->view('templates/footer'); 86 | 87 | } 88 | else 89 | { 90 | $this->news_model->set_news($id); 91 | //$this->load->view('news/success'); 92 | redirect( base_url() . 'index.php/news'); 93 | } 94 | } 95 | 96 | public function delete() 97 | { 98 | $id = $this->uri->segment(3); 99 | 100 | if (empty($id)) 101 | { 102 | show_404(); 103 | } 104 | 105 | $news_item = $this->news_model->get_news_by_id($id); 106 | 107 | $this->news_model->delete_news($id); 108 | redirect( base_url() . 'index.php/news'); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /application/models/News_model.php: -------------------------------------------------------------------------------- 1 | load->database(); 7 | } 8 | 9 | public function get_news($slug = FALSE) 10 | { 11 | if ($slug === FALSE) 12 | { 13 | $query = $this->db->get('news'); 14 | return $query->result_array(); 15 | } 16 | 17 | $query = $this->db->get_where('news', array('slug' => $slug)); 18 | return $query->row_array(); 19 | } 20 | 21 | public function get_news_by_id($id = 0) 22 | { 23 | if ($id === 0) 24 | { 25 | $query = $this->db->get('news'); 26 | return $query->result_array(); 27 | } 28 | 29 | $query = $this->db->get_where('news', array('id' => $id)); 30 | return $query->row_array(); 31 | } 32 | 33 | public function set_news($id = 0) 34 | { 35 | $this->load->helper('url'); 36 | 37 | $slug = url_title($this->input->post('title'), 'dash', TRUE); 38 | 39 | $data = array( 40 | 'title' => $this->input->post('title'), 41 | 'slug' => $slug, 42 | 'text' => $this->input->post('text') 43 | ); 44 | 45 | if ($id == 0) { 46 | return $this->db->insert('news', $data); 47 | } else { 48 | $this->db->where('id', $id); 49 | return $this->db->update('news', $data); 50 | } 51 | } 52 | 53 | public function delete_news($id) 54 | { 55 | $this->db->where('id', $id); 56 | return $this->db->delete('news'); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /application/views/news/index.php: -------------------------------------------------------------------------------- 1 |

2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 18 | 19 | 20 |
TitleContentAction
14 | View | 15 | Edit | 16 | Delete 17 |
21 | -------------------------------------------------------------------------------- /application/views/news/success.php: -------------------------------------------------------------------------------- 1 |

News added successfully!

2 | -------------------------------------------------------------------------------- /application/views/news/view.php: -------------------------------------------------------------------------------- 1 | '.$news_item['title'].''; 3 | echo $news_item['text']; 4 | -------------------------------------------------------------------------------- /application/views/templates/footer.php: -------------------------------------------------------------------------------- 1 |

Copyright © 2016

2 | 3 | 4 | -------------------------------------------------------------------------------- /application/views/templates/header.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | CodeIgniter Tutorial 4 | 5 | 6 |

Simple CRUD

7 |

Home | Add News

8 | -------------------------------------------------------------------------------- /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 | PRIMARY KEY (`id`), 19 | KEY `slug` (`slug`) 20 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; 21 | 22 | -- 23 | -- Dumping data for table `news` 24 | -- 25 | 26 | INSERT INTO `news` (`id`, `title`, `slug`, `text`) VALUES 27 | (1, 'Test', 'test', 'Hello World !!'), 28 | (2, 'What is Lorem Ipsum?', 'what-is-lorem-ipsum', 'Lorem Ipsum is simply dummy text.'); 29 | --------------------------------------------------------------------------------