This site uses cookies from Google to deliver its services, to personalize ads and to analyze traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies. Learn More

[Web-development (jquery)] Insert – Edit – Delete Codeigniter

Insert – Edit – Delete Codeigniter

Insert – Edit – Delete Codeigniter

Create Database

CREATE TABLE IF NOT EXISTS `users` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`username` varchar(15) NOT NULL,
`password` text NOT NULL,
`fullname` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
)

Setup Controller

application\controllers\user.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class User extends CI_Controller{ /** * The __construct function is called so that I don't have to load the model each and every time. * And any change while refactoring or something else would mean change in only one place. */ function __construct() { parent::__construct(); $this->load->model('Model_user'); $this->load->library('form_validation'); $this->load->helper('form'); } function index(){ $data['query'] = $this->Model_user->getAll(); $this->load->view('view_user/input',$data); } function submit(){ if ($this->input->post('submit')){ if ($this->input->post('id')){ $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]'); $this->form_validation->set_rules('password', 'Password', 'matches[passconf]'); $this->form_validation->set_rules('fullname', 'Fullname', 'required|min_length[5]|max_length[25]'); if ($this->form_validation->run() == FALSE){ $data['id'] = $this->input->post('id'); $data['username'] = set_value('username'); $data['fullname'] = set_value('fullname'); $this->load->view('view_user/edit',$data); }else{ $this->Model_user->update(); redirect('view_user/index'); } }else{ $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]'); $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]'); $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required'); $this->form_validation->set_rules('fullname', 'Fullname', 'required|min_length[5]|max_length[25]'); if ($this->form_validation->run() == FALSE){ $data['query'] = $this->Model_user->getAll(); $this->load->view('view_user/input',$data); }else{ $this->Model_user->save(); redirect('user/index'); } } } } function edit(){ $id=$this->uri->segment(3); $data['result']=$this->Model_user->getUser($id); if (empty($id) or count($data['result'])==0 ){ redirect('user/index'); }else{ $result=$this->Model_user->getUser($id); $data['id'] = $result['id']; $data['username'] = $result['username']; $data['fullname'] = $result['fullname']; $this->load->view('view_user', $data); } } function delete($id){ $this->db->delete('user', array('id' => $id)); redirect('user/index'); } } 
Model application\models\Model_user.php
<?php class Model_user extends CI_Model{ function getAll(){ $this->db->select('id,username,fullname'); $this->db->from('users'); $this->db->limit(10); $this->db->order_by('id','ASC'); $query = $this->db->get(); return $query->result(); } function getUser($id){ $this->db->where('id', $id); $query = $this->db->get('users'); return $query->row_array(); } function save(){ $username = $this->input->post('username'); $password = $this->input->post('password'); $fullname = $this->input->post('fullname'); $data = array( 'username'=>$username, 'password'=>md5($password), 'fullname'=>$fullname ); $this->db->insert('users',$data); } function update(){ $id = $this->input->post('id'); $username = $this->input->post('username'); $password = $this->input->post('password'); $fullname = $this->input->post('fullname'); if ($password==''){ $data = array( 'username'=>$username, 'fullname'=>$fullname ); }else{ $data = array( 'username'=>$username, 'password'=>md5($password), 'fullname'=>$fullname ); } $this->db->where('id',$id); $this->db->update('users',$data); } } 
View application\views\view_user\input.php
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Data User</title> </head> <body> <div> <table> <?php echo validation_errors(); ?> <?php echo form_open('user/submit'); ?> <tr > <td> <?php echo form_label('User Name : '); ?></td> <td> <?php echo form_input('username',set_value('username'),'id="username"'); ?></td> </tr> <tr> <td> <?php echo form_label('Password : ');?> </td> <td> <?php echo form_password('password','','id="password"'); ?></td> </tr> <tr> <td> <?php echo form_label('Repeat Password : ');?> </td> <td> <?php echo form_password('passconf','','id="passconf"'); ?></td> </tr> <tr> <td> <?php echo form_label('Full Name : ');?> </td> <td> <?php echo form_input('fullname', set_value('fullname'),'id="fullname"'); ?></td> </tr> <tr> <td><?php echo form_submit('submit','Save')?> </tr> </table> </div> <div> <?php $this->load->view('view_user/show'); ?> </div> </div> </body> </html> 
application\views\view_user\show.php
<?php if (isset($query) && count($query) > 0){ ?> <h1>Data User</h1> <table width="400"> <tr > <th>No</th> <th>Username</th> <th>Fullname</th> <th>Edit</th> <th>Delete</th> </tr> <?php $i=0; foreach ($query as $row){ $i++; echo "<tr class=\"record\">"; echo "<td>$i</td>"; echo "<td>$row->username</td>"; echo "<td>$row->fullname</td>"; echo "<td><a href=".base_url()."index.php/user/edit/$row->id>Edit</a></td>"; echo "<td><a href=".base_url()."index.php/user/delete/$row->id>Delete</a></td>"; echo "</tr>"; } ?> </table> <?php } ?> 

source : http://viva.co.id, http://tutorial101.blogspot.com, http://kompas.com

0 Response to "[Web-development (jquery)] Insert – Edit – Delete Codeigniter"

Posting Komentar

Contact

Nama

Email *

Pesan *