CodeIgniter CRUD Example



Blogs congroller blogs.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Blogs extends CI_Controller {
public function index() {
$data['blogs_result'] = $this->db->get('userlogin')->result();
$this->load->view('view_blogs',$data);
}

public function blogsave(){
$getUser = $this->input->post('username');
$getPass = $this->input->post('password');
$saveData = array(
'username'=>$getUser,
'userpass'=>$getPass
);
$this->db->insert('userlogin',$saveData);
$this->index();
}

public function blogsdel(){
$delId = $this->uri->segment(3);
$this->db->delete('userlogin', array('id' => $delId)); 
$this->index();
}

public function blogsedit(){
$getId = $this->uri->segment(3);
$data['result'] = $this->db->get_where('userlogin', array('id' =>$getId))->result();
$this->load->view('edit_blogs',$data);
}

public function blogsupdate(){
$getId = $this->input->post('postid');
$getUser = $this->input->post('username');
$getPass = $this->input->post('password');

$data = array(
               'username' => $getUser,
               'userpass' => $getPass
            );
$this->db->where('id', $getId);
$this->db->update('userlogin', $data);
$this->index(); 
}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

views->views_blogs.php

<!DOCTYPE html>
<html>
<head>
<title>CI</title>
<style type="text/css">
*{
font-family: arial;
font-size: 13px;
}
</style>
</head>
<body>
<form method="post" action="<?php echo base_url('blogs/blogsave') ?>">
<table>
<tr>
<td>Username</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="password"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Create" name="submit"/>
</td>
<tr>
</table>
</form>
<table border="1">
<tr>
<th>#</th>
<th>Username</th>
<th>Password</th>
<th>Action</th>
</tr>
<?php foreach ($blogs_result as $key => $value) {?>
<tr>
<td><?= $value->id; ?></td>
<td><?= $value->username; ?></td>
<td><?= $value->userpass; ?></td>
<td>
<a href="<?php echo base_url('blogs/blogsdel')?>/<?php echo $value->id ?>">Delete</a>
<a href="<?php echo base_url('blogs/blogsedit')?>/<?php echo $value->id ?>">Edit</a>
</td>
</tr>
<?php } ?>
</table>
</body>
</html>

Views->edit_blogs.php

<!DOCTYPE html>
<html>
<head>
<title>Edit Blogs</title>
</head>
<body>
<form method="post" action="<?php echo base_url('blogs/blogsupdate') ?>">
<input type="hidden" name="postid" value="<?php echo $result[0]->id; ?>"/>
<table>
<tr>
<td>Username</td>
<td><input type="text" name="username" value="<?php echo $result[0]->username; ?>"/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="password" value="<?php echo $result[0]->userpass; ?>"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Create" name="submit"/>
</td>
<tr>
</table>
</form>
</body>
</html>
Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment