phpeveryday.com

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


CodeIgniter - Form: Showing Single Data for Form Edit

Tag: CodeIgniter, framework, MVC, database, GET   Category: PHP Framework
post: 28 Feb 2008 read: 3,138


CodeIgniter Form Step By Step Tutorial - Part 16: Now, we want to show single data at form for editing. User can choose a data from table that we create at here.

First, create a function at model(books_model.php) that will display single data.


  function get($id){
	$this->load->database();
    $query = $this->db->getwhere('books',array('id'=>$id));
    return $query->row_array();		  
  }

Then, update input() at controller (books.php). Do like this:


  function input($id = 0){

	$this->load->helper('form');  
	$this->load->helper('html');  	
	$this->load->model('books_model');
	
	if($this->input->post('mysubmit')){
		$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);	
  }

Now, input() can accept parameter from GET. If there is a value from GET, it will retrieve data from get at model (line 13). We enter the value to form (line 14 - 24).

Next, update view books_input() become like this:


<html>
<head>
<link rel="stylesheet" type="text/css" 
      href="<?php echo "$base/$css"?>">
</head>
<body>
<div id="header">
<? $this->load->view('books_header'); ?>
</div>
<div id="menu">
<? $this->load->view('books_menu'); ?>
</div>
<? echo heading($forminput,3) ?>
<? echo form_open('books/input'); ?>
<? echo form_hidden('id',$fid['value']); ?>
<? echo $title		.' : '.
        form_input($ftitle).br(); ?>
<? echo $author		.' : '.
        form_input($fauthor).br(); ?>
<? echo $publisher	.' : '.
        form_input($fpublisher).br(); ?>
<? echo $year		.' : '.
        form_dropdown('year',$years,$fyear['value']).br(); ?>
<? echo $available	.' : '.
        form_checkbox($favailable).br(); ?>
<? echo $summary	.' : '.
        form_textarea($fsummary).br(); ?>
<? echo form_submit('mysubmit','Submit!');  ?>
<? echo form_close(); ?>

<div id="footer">
<? $this->load->view('books_footer'); ?>
</div>

</body>
</html>

We add input type hidden at line 15 for editing key. Then accomodate dropdown object at line 23.

Now, try to edit a data by click edit link at table. May be you get a data like:

CodeIgniter form edit


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