| PHP Error Message |
Warning: Invalid argument supplied for foreach() in /home/a3178292/public_html/article.php on line 213
CodeIgniter - Form: Showing Single Data for Form Edit
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:
