{"id":3970,"date":"2019-03-28T17:55:38","date_gmt":"2019-03-28T12:25:38","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=3970"},"modified":"2019-03-29T17:48:53","modified_gmt":"2019-03-29T12:18:53","slug":"codeigniter-simple-add-edit-delete-view","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/codeigniter-simple-add-edit-delete-view\/","title":{"rendered":"CODEIGNITER: SIMPLE ADD, EDIT, DELETE, VIEW"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><strong>Date :28\/03\/2019<\/strong><\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">What is codeigniter?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Codeigniter is a php framework to develop the application. Therefore, it  provides the many libraries for connecting to the database.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Codeigniter simple Add, Edit, Delete, View<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">However, we create database and table. <br><br>Firstly, database name is considered to be as &#8216;form&#8217;.<br><br>similarly, creating a table named &#8216;register&#8217;.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Database Config Settings<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In order to connect with the database, the below instructions must be followed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The file is located at application\/config\/database.php.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$active_group = 'default';\n$query_builder = TRUE;\n\n$db['default'] = array(\n\t'dsn'\t=> '',\n\t'hostname' => 'localhost',\n\t'username' => 'root',\n\t'password' => '',\n\t'database' => 'form',\n\t'dbdriver' => 'mysqli',\n\t'dbprefix' => '',\n\t'pconnect' => FALSE,\n\t'db_debug' => (ENVIRONMENT !== 'production'),\n\t'cache_on' => FALSE,\n\t'cachedir' => '',\n\t'char_set' => 'utf8',\n\t'dbcollat' => 'utf8_general_ci',\n\t'swap_pre' => '',\n\t'encrypt' => FALSE,\n\t'compress' => FALSE,\n\t'stricton' => FALSE,\n\t'failover' => array(),\n\t'save_queries' => TRUE\n);\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"> <br>Controller (application\/controllers\/Welcome.php) <\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Controller is a process which get the input request from user, communicates with Model and then loads appropriate Views file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the below Controller class, we have functions for different tasks. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public function list ()\n  {\n    $this->load->database();\n    $this->load->model('Insert_model');\n    $data['table_items']=$this->Insert_model->list();\n    $this->load->view('list_client', $data);\n  }\n\t\n  public function create (){\n\t\t$this->load->view('display.php');\n\t}\n\tpublic function add()\n\t{\n\t\t$this->load->model('Insert_model');\n\t\t$postData = array(\n\t\t'id' => $this->input->post('id'),\n    'first_name' => $this->input->post('firstname'),\n    'gender' => $this->input->post('gender'),\n    'habbies' => $this->input->post('hobbies'),\n    'state' => $this->input->post('state'),\n    'descrite' => $this->input->post('discription'));    \n    $insert = $this->Insert_model->insert($postData);\n  }\n  \n  public function retrive_update_data($id)\n  {\n    $this->load->model('Insert_model');\n    $da= $this->Insert_model->retrive_update_data($id);\n    $data['value1'] = $da->row();       \n    $this->load->view('update_view', $data);\n  }\n \n  public function update_data($id)\n  {\n    $this->load->model('Insert_model');\n    $postData = array(\n\t\t'first_name' => $this->input->post('firstname'),\n    'gender' => $this->input->post('gender'),\n    'habbies' => $this->input->post('habbies'),\n    'state' => $this->input->post('state'),\n    'descrite' => $this->input->post('describe'));    \n     $da= $this->Insert_model->update_data_model($id, $postData);\n  }\n\n   public function delete($id)\n  {\n    $this->load->model('Insert_model');\n    $delete = $this->Insert_model<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Model (application\/models\/Insert_model.php) <\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"> This class is responsible for interacting with database for data select, insert, update and delete purposes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> -&gt; To  list() function fetches\/selects register items by $slug name.<br> -&gt;In addition to  insert() function insert new item to register  <br>-&gt; In order to update_data_model() function either update register item. <br> -&gt; Most importantly,  retrive_update_data()  function fetches register item  by it\u2019s ID<br>  -&gt;Finally, delete() function delets any particular register item.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">  public function list($data = array())\n  {\n    $insert = $this->db->get('register')->result();\n    return $insert;\n  }\n\n  public function insert($data = array())\n\t{\n    $insert=$this->db->insert('register',$data);\n  }\n\t\n  public function retrive_update_data($id)\n  {\n   \t\t$this->db->select('*');\n       return $query = $this->db->get_where('register',array('id'=>$id));\n  }\n\n  public function update_data_model($id, $postData)\n  {\n      $this->db->where('id', $id);\n      $this->db->update('register', $postData);\n      echo \"data updated successfully\";\n  }\n\n  public function delete($id)\n  {\n    $this->db->where('id', $id);\n    $this->db->delete('register');\n    echo \"value deleted successfully\";\n  }\n\n}\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><br>Index View  (application\/views\/list_client.php) <\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This view file is called by list() function of our Controller class because it  will lists out all the register items.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;body>\n  &lt;center>\n&lt;table border=\"1\">\n&lt;thead>\n      &lt;tr>\n          &lt;th>ID&lt;\/th>\n          &lt;th>first-name&lt;\/th>\n          &lt;th>gender&lt;\/th>\n          &lt;th>hobbies&lt;\/th>\n          &lt;th>state&lt;\/th>\n          &lt;th>discription&lt;\/th>\n          &lt;th>Delete&lt;\/th>\n      &lt;\/tr>\n    &lt;\/thead>\n    &lt;tbody>\n       &lt;?php foreach($table_items as $row){ ?>\n         &lt;tr>\n            &lt;td>&lt;?php echo $row->id; ?>&lt;\/td>\n                &lt;td>&lt;?php echo $row->first_name; ?>&lt;\/td>\n                &lt;td>&lt;?php echo $row->gender; ?>&lt;\/td>\n                &lt;td>&lt;?php echo $row->habbies; ?>&lt;\/td>\n                &lt;td>&lt;?php echo $row->state; ?>&lt;\/td>\n                &lt;td>&lt;?php echo $row->descrite; ?>&lt;\/td>\n&lt;td>&lt;a href=\"&lt;?php echo site_url('Welcome\/delete\/'.$row->id);?>\">Delete&lt;\/a>&lt;\/td>\n&lt;td>&lt;a href=\"&lt;?php echo site_url('Welcome\/retrive_update_data\/'.$row->id);?>\">Edit&lt;\/a>&lt;\/td>&lt;\/tr>\n  &lt;?php } ?>\n&lt;\/tbody>\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Add View(application\/views\/add_new.php)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This view file is called by create() function of our Controller class and then most certainly, it will show the new form to add item in register.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;body>\n\n&lt;form role=\"form\" method=\"post\" action=\"&lt;?php echo site_url('Welcome\/add');?>\">\n First name:&lt;br> &lt;input type=\"text\" name=\"firstname\">\n &lt;br>\n gender&lt;input type=\"radio\" name=\"gender\" value=\"male\" checked> Male&lt;br>\n &lt;input type=\"radio\" name=\"gender\" value=\"female\"> Female&lt;br>\n &lt;input type=\"radio\" name=\"gender\" value=\"other\"> Other\n &lt;br>\n Hobbies&lt;br>&lt;input type=\"checkbox\" name=\"hobbies\" value=\"dancing\">dancing&lt;br>\n&lt;input type=\"checkbox\" name=\"hobbies\" value=\"dancing\">playing&lt;br>\n &lt;br>\nState&lt;br> &lt;input type=\"text\" name=\"state\" value=\"\">\n &lt;br>\n  Discription&lt;br>&lt;input type=\"text area\" name=\"discription\">\n&lt;\/textarea>\n &lt;br>\n\n &lt;input type=\"submit\">\n&lt;\/form>\n\n&lt;\/body>\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Update View <br> (application\/views\/update_view.php) <br><br><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This view file is called by retrive_update_data() function of our Controller class. It show the form with user data.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;body>\n\t&lt;form role=\"form\" method=\"post\" action=\"&lt;?php echo site_url('Welcome\/update_data\/' .$value1->id);?>\">\n\n&lt;label> First name:&lt;\/label>&lt;br> &lt;input type=\"text\" name=\"firstname\" value=\"&lt;?php echo $value1->first_name;?>;\">  &lt;br>\n &lt;label> Gender:&lt;\/label>&lt;br> &lt;input type=\"text\" name=\"gender\" value=\"&lt;?php echo $value1->gender;?>;\">  &lt;br>\n\n\n&lt;label> Hobbies:&lt;\/label>&lt;br> &lt;input type=\"text\" name=\"hobies\" value=\"&lt;?php echo $value1->habbies;?>;\">  &lt;br>\n\nstate&lt;br> &lt;input type=\"text\" name=\"state\" value=\"&lt;?php echo $value1->state;?>;\">  &lt;br>\n  Discription&lt;br>&lt;input type=\"text area\" name=\"discription\" value=\"&lt;?php echo $value1->descrite;?>;\">  &lt;br>\n&lt;button> Submit&lt;\/button>\n&lt;\/form><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> Thanks for using pheonix solutions. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You find this tutorial helpful? Share with your friends to keep it alive.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Date :28\/03\/2019 What is codeigniter? Codeigniter is a php framework to develop the application. Therefore, it provides the many libraries [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[506,601,600,507],"class_list":["post-3970","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-delete","tag-insert","tag-list","tag-update","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-122","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=3970"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3970\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=3970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=3970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=3970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}