phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


CodeIgniter - Form: Updating Data

Tag: CodeIgniter, framework, MVC, database, update   Category: PHP Framework
post: 03 Mar 2008 read: 3,060


CodeIgniter Form Step By Step Tutorial - Part 18: After create edit form, now we add updating function.

First, create entry_update()at model.


  function entry_update(){
    $this->load->database();
	$data = array(
	          'title'=>$this->input->post('title'),
			  'author'=>$this->input->post('author'),
			  'publisher'=>$this->input->post('publisher'),
			  'year'=>$this->input->post('year'),
			  'available'=>$this->input->post('available'),
			  'summary'=>$this->input->post('summary'),
	        );
	$this->db->where('id',$this->input->post('id'));
	$this->db->update('books',$data);  
  }

Then, update our input() at controller:


  function input($id = 0){

	$this->load->helper('form');  
	$this->load->helper('html');  	
	$this->load->model('books_model');
	
	if($this->input->post('mysubmit')){
	  if($this->input->post('id')){
		$this->books_model->entry_update();	  
	  }else{
		$this->books_model->entry_insert();
	  }
	}
	$data = $this->books_model->general();
	
    if((int)$id > 0){
      $query = $this->books_model->get($id);
	  $data['fid']['value'] = $query['id'];
	  $data['ftitle']['value'] = $query['title'];
	  $data['fauthor']['value'] = $query['author'];
	  $data['fpublisher']['value'] = $query['publisher'];
	  $data['fyear']['value'] = $query['year'];
	  if($query['available']=='yes'){
	    $data['favailable']['checked'] = TRUE;
	  }else{
	    $data['favailable']['checked'] = FALSE;	  
	  }
	  $data['fsummary']['value'] = $query['summary'];
	}
	
				
	$this->load->view('books_input',$data);	
  }

If there is id that be sent by post, it will load entry_update (line 9).



Series this article:
CodeIgniter - Form: Creating Skeleton
CodeIgniter - Form: File Structure
CodeIgniter - Form: Creating Layout Code
CodeIgniter - Form: Creating Menu Library
CodeIgniter - Form: Putting Text at Header and Footer
CodeIgniter - Form: Centralizing $data
CodeIgniter - Form: Adding CSS
CodeIgniter - Form: Creating Form HTML
CodeIgniter - Form: Creating Beautiful Form Code
CodeIgniter - Form: CodeIgniter HTML Style
CodeIgniter - Form: Preparing Table at Database
CodeIgniter - Form: Creating Insert Data
CodeIgniter - Form: Creating List Data Use Table Library
CodeIgniter - Form: Creating Table List Without Table Library
CodeIgniter - Form: Using URL Helper
CodeIgniter - Form: Showing Single Data for Form Edit
CodeIgniter - Form: Something Happen With Form Parameters
CodeIgniter - Form: Updating Data
CodeIgniter - Form: Deleting Data

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Joomla: Fast Road to Understand Component Programming
Chart: How to Build Cool Animation Real Time Chart
Email: Send Attachement Mail
SMS : Sending SMS with PHP and ActiveXperts (Part 1)

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


624
posting