C:\xampp\htdocs\mycrud\application\config\config.php
$config['base_url'] = 'http://localhost/mycrud/';
$config['index_page'] = 'index.php';
C:\xampp\htdocs\mycrud\application\config\autoload.php
$autoload['libraries'] = array('database', 'session', 'form_validation');
$autoload['helper'] = array('url');
C:\xampp\htdocs\mycrud\application\config\database.php
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mycrud',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mycrud',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
C:\xampp\htdocs\mycrud\application\controllers\User.php
<?php
class User extends CI_controller
{
function index(){
$this->load->model('User_model');
$users = $this->User_model->all();
$data = array();
$data['users'] = $users;
$this->load->view('list',$data);
}
function create(){
$this->load->model('User_model');
$this->form_validation->set_rules('name','Name','required');
$this->form_validation->set_rules('email','Email','required|valid_email');
if ($this->form_validation->run() == false) {
$this->load->view('create');
}else{
$formArray = array();
$formArray['name']=$this->input->post('name');
$formArray['email']=$this->input->post('email');
$formArray['created_at']=date('Y-m-d');
$this->User_model->create($formArray);
$this->session->set_flashdata('success','Record inserted Successfully..');
redirect(base_url().'index.php/user/index');
}
}
function edit($userid){
$this->load->model('User_model');
$user = $this->User_model->getUser($userid);
$data = array();
$data['user'] = $user;
$this->form_validation->set_rules('name','Name','required');
$this->form_validation->set_rules('email','Email','required|valid_email');
if ($this->form_validation->run() == false) {
$this->load->view('edit', $data);
}else{
//update user data
$formArray = array();
$formArray['name']=$this->input->post('name');
$formArray['email']=$this->input->post('email');
$this->User_model->updateUser($userid, $formArray);
$this->session->set_flashdata('success','Record updated Successfully..');
redirect(base_url().'index.php/user/index');
}
}
function delete($userid){
$this->load->model('User_model');
$user = $this->User_model->getUser($userid);
if(empty($user)){
$this->session->set_flashdata('failure','Record Not Found..');
redirect(base_url().'index.php/user/index');
}else{
$this->User_model->deleteUser($userid);
$this->session->set_flashdata('success','Record Deleted Successfully..');
redirect(base_url().'index.php/user/index');
}
}
}
?>
class User extends CI_controller
{
function index(){
$this->load->model('User_model');
$users = $this->User_model->all();
$data = array();
$data['users'] = $users;
$this->load->view('list',$data);
}
function create(){
$this->load->model('User_model');
$this->form_validation->set_rules('name','Name','required');
$this->form_validation->set_rules('email','Email','required|valid_email');
if ($this->form_validation->run() == false) {
$this->load->view('create');
}else{
$formArray = array();
$formArray['name']=$this->input->post('name');
$formArray['email']=$this->input->post('email');
$formArray['created_at']=date('Y-m-d');
$this->User_model->create($formArray);
$this->session->set_flashdata('success','Record inserted Successfully..');
redirect(base_url().'index.php/user/index');
}
}
function edit($userid){
$this->load->model('User_model');
$user = $this->User_model->getUser($userid);
$data = array();
$data['user'] = $user;
$this->form_validation->set_rules('name','Name','required');
$this->form_validation->set_rules('email','Email','required|valid_email');
if ($this->form_validation->run() == false) {
$this->load->view('edit', $data);
}else{
//update user data
$formArray = array();
$formArray['name']=$this->input->post('name');
$formArray['email']=$this->input->post('email');
$this->User_model->updateUser($userid, $formArray);
$this->session->set_flashdata('success','Record updated Successfully..');
redirect(base_url().'index.php/user/index');
}
}
function delete($userid){
$this->load->model('User_model');
$user = $this->User_model->getUser($userid);
if(empty($user)){
$this->session->set_flashdata('failure','Record Not Found..');
redirect(base_url().'index.php/user/index');
}else{
$this->User_model->deleteUser($userid);
$this->session->set_flashdata('success','Record Deleted Successfully..');
redirect(base_url().'index.php/user/index');
}
}
}
?>
C:\xampp\htdocs\mycrud\application\models\User_model.php
<?php
class User_model extends CI_model{
function create($formArray){
$this->db->insert('users',$formArray); // insert into users (formArray data)
}
function all(){
return $result = $this->db->get('users')->result_array(); // select * from users
}
function getUser($user_id){
$this->db->where('user_id', $user_id);
return $user = $this->db->get('users')->row_array(); //select * from users where user_id = ?
}
function updateUser($user_id, $formArray){
$this->db->where('user_id',$user_id);
$this->db->update('users', $formArray); // update users set (formarray data) where user_id = ?
}
function deleteUser($user_id){
$this->db->where('user_id', $user_id);
$this->db->delete('users'); // delete from users where user_id = ?
}
}
?>
class User_model extends CI_model{
function create($formArray){
$this->db->insert('users',$formArray); // insert into users (formArray data)
}
function all(){
return $result = $this->db->get('users')->result_array(); // select * from users
}
function getUser($user_id){
$this->db->where('user_id', $user_id);
return $user = $this->db->get('users')->row_array(); //select * from users where user_id = ?
}
function updateUser($user_id, $formArray){
$this->db->where('user_id',$user_id);
$this->db->update('users', $formArray); // update users set (formarray data) where user_id = ?
}
function deleteUser($user_id){
$this->db->where('user_id', $user_id);
$this->db->delete('users'); // delete from users where user_id = ?
}
}
?>
C:\xampp\htdocs\mycrud\application\views\create.php
<!DOCTYPE html>
<html>
<head>
<title>Crud Operation: Create User</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url().'assets/css/bootstrap.min.css'; ?>">
</head>
<body>
<div class="navbar navbar-dark bg-dark">
<div class="container">
<a href="#" class="navbar-brand">Crud Application</a>
</div>
</div>
<div class="container" style="padding-top: 10px;">
<div class="row">
<div class="col-8"><h3>Create User Data:-</h3></div>
<div class="col-4 text-right pull-right">
<a href="<?php echo base_url().'index.php/user/index'; ?>" title="" class="btn btn-primary">Show All Users</a>
</div>
</div>
<hr>
<div class="row">
<form name="createUser" action="<?php echo base_url().'index.php/user/create'; ?>" method="post" >
<div class="col-md-6">
<div class="form-group">
<label for="name">Name : </label>
<input type="text" name="name" id="name" value="<?php echo set_value('name'); ?>" placeholder="Enter Name Here" class="form-control">
<?php echo form_error('name'); ?>
</div>
<div class="form-group">
<label for="name">Email : </label>
<input type="email" name="email" id="email" value="<?php echo set_value('email'); ?>" placeholder="Enter Email ID Here" class="form-control">
<?php echo form_error('email'); ?>
</div>
<div class="form-group">
<button class="btn btn-primary">Create</button>
<a href="<?php echo base_url().'index.php/user/index' ?>" class="btn btn-secondary">Cancel</a>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
<html>
<head>
<title>Crud Operation: Create User</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url().'assets/css/bootstrap.min.css'; ?>">
</head>
<body>
<div class="navbar navbar-dark bg-dark">
<div class="container">
<a href="#" class="navbar-brand">Crud Application</a>
</div>
</div>
<div class="container" style="padding-top: 10px;">
<div class="row">
<div class="col-8"><h3>Create User Data:-</h3></div>
<div class="col-4 text-right pull-right">
<a href="<?php echo base_url().'index.php/user/index'; ?>" title="" class="btn btn-primary">Show All Users</a>
</div>
</div>
<hr>
<div class="row">
<form name="createUser" action="<?php echo base_url().'index.php/user/create'; ?>" method="post" >
<div class="col-md-6">
<div class="form-group">
<label for="name">Name : </label>
<input type="text" name="name" id="name" value="<?php echo set_value('name'); ?>" placeholder="Enter Name Here" class="form-control">
<?php echo form_error('name'); ?>
</div>
<div class="form-group">
<label for="name">Email : </label>
<input type="email" name="email" id="email" value="<?php echo set_value('email'); ?>" placeholder="Enter Email ID Here" class="form-control">
<?php echo form_error('email'); ?>
</div>
<div class="form-group">
<button class="btn btn-primary">Create</button>
<a href="<?php echo base_url().'index.php/user/index' ?>" class="btn btn-secondary">Cancel</a>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
C:\xampp\htdocs\mycrud\application\views\edit.php
<!DOCTYPE html>
<html>
<head>
<title>Crud Operation: Update User</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url().'assets/css/bootstrap.min.css'; ?>">
</head>
<body>
<div class="navbar navbar-dark bg-dark">
<div class="container">
<a href="#" class="navbar-brand">Crud Application</a>
</div>
</div>
<div class="container" style="padding-top: 10px;">
<div class="row">
<div class="col-8"><h3>Update User:-</h3></div>
<div class="col-4 text-right pull-right">
<a href="<?php echo base_url().'index.php/user/index'; ?>" title="" class="btn btn-primary">Show All Users</a>
</div>
</div>
<hr>
<div class="row">
<form name="createUser" action="<?php echo base_url().'index.php/user/edit/'.$user['user_id']; ?>" method="post" >
<div class="col-md-6">
<div class="form-group">
<label for="name">Name : </label>
<input type="text" name="name" id="name" value="<?php echo set_value('name', $user['name']); ?>" placeholder="Enter Name Here" class="form-control">
<?php echo form_error('name'); ?>
</div>
<div class="form-group">
<label for="name">Email : </label>
<input type="email" name="email" id="email" value="<?php echo set_value('email', $user['email']); ?>" placeholder="Enter Email ID Here" class="form-control">
<?php echo form_error('email'); ?>
</div>
<div class="form-group">
<button class="btn btn-primary">Update</button>
<a href="<?php echo base_url().'index.php/user/index' ?>" class="btn btn-secondary">Cancel</a>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
<html>
<head>
<title>Crud Operation: Update User</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url().'assets/css/bootstrap.min.css'; ?>">
</head>
<body>
<div class="navbar navbar-dark bg-dark">
<div class="container">
<a href="#" class="navbar-brand">Crud Application</a>
</div>
</div>
<div class="container" style="padding-top: 10px;">
<div class="row">
<div class="col-8"><h3>Update User:-</h3></div>
<div class="col-4 text-right pull-right">
<a href="<?php echo base_url().'index.php/user/index'; ?>" title="" class="btn btn-primary">Show All Users</a>
</div>
</div>
<hr>
<div class="row">
<form name="createUser" action="<?php echo base_url().'index.php/user/edit/'.$user['user_id']; ?>" method="post" >
<div class="col-md-6">
<div class="form-group">
<label for="name">Name : </label>
<input type="text" name="name" id="name" value="<?php echo set_value('name', $user['name']); ?>" placeholder="Enter Name Here" class="form-control">
<?php echo form_error('name'); ?>
</div>
<div class="form-group">
<label for="name">Email : </label>
<input type="email" name="email" id="email" value="<?php echo set_value('email', $user['email']); ?>" placeholder="Enter Email ID Here" class="form-control">
<?php echo form_error('email'); ?>
</div>
<div class="form-group">
<button class="btn btn-primary">Update</button>
<a href="<?php echo base_url().'index.php/user/index' ?>" class="btn btn-secondary">Cancel</a>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
C:\xampp\htdocs\mycrud\application\views\list.php
<!DOCTYPE html>
<html>
<head>
<title>Crud Operation: Display All Users</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url().'assets/css/bootstrap.min.css'; ?>">
</head>
<body>
<div class="navbar navbar-dark bg-dark">
<div class="container">
<a href="#" class="navbar-brand">Crud Application</a>
</div>
</div>
<div class="container" style="padding-top: 10px;">
<div class="row">
<div class="col-md-12">
<?php $success = $this->session->userdata('success');
if($success != ""){
?>
<div class="alert alert-success">
<?php echo $success; ?>
</div>
<?php }
?>
<?php $failure = $this->session->userdata('failure');
if($failure != ""){
?>
<div class="alert alert-danger">
<?php echo $failure; ?>
</div>
<?php }
?>
</div>
</div>
<div class="row">
<div class="col-8"><h3>View User Data:-</h3></div>
<div class="col-4 text-right pull-right">
<a href="<?php echo base_url().'index.php/user/create'; ?>" title="" class="btn btn-primary">Create</a>
</div>
</div>
<hr>
<div class="row">
<div class="col-md-8">
<table class="table table-striped">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<tbody>
<?php
if(!empty($users)) {
foreach($users as $user) {
?>
<tr>
<td><?php echo $user['user_id']; ?></td>
<td><?php echo $user['name']; ?></td>
<td><?php echo $user['email']; ?></td>
<td><a href="<?php echo base_url().'index.php/user/edit/'.$user['user_id'] ; ?>" title="Edit" class="btn btn-primary">Edit</a> </td>
<!-- <td><a href="<?php //echo base_url().'index.php/user/delete/'.$user['user_id'] ; ?>" title="Delete" class="btn btn-danger">Delete</a> </td> -->
<td><a href="javascript:void(0);>" onclick="deleteThis(<?php echo $user['user_id']; ?>);" title="Delete" class="btn btn-danger">Delete</a> </td>
</tr>
<?php } } else{ ?>
<tr>
<td colspan="5" >No Data Found!</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
<script type="text/javascript">
var url="<?php echo base_url();?>";
function deleteThis(id){
var r=confirm("Do you want to delete this?")
if (r==true)
window.location = url+"index.php/user/delete/"+id;
else
return false;
}
</script>
</body>
</html>
<html>
<head>
<title>Crud Operation: Display All Users</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url().'assets/css/bootstrap.min.css'; ?>">
</head>
<body>
<div class="navbar navbar-dark bg-dark">
<div class="container">
<a href="#" class="navbar-brand">Crud Application</a>
</div>
</div>
<div class="container" style="padding-top: 10px;">
<div class="row">
<div class="col-md-12">
<?php $success = $this->session->userdata('success');
if($success != ""){
?>
<div class="alert alert-success">
<?php echo $success; ?>
</div>
<?php }
?>
<?php $failure = $this->session->userdata('failure');
if($failure != ""){
?>
<div class="alert alert-danger">
<?php echo $failure; ?>
</div>
<?php }
?>
</div>
</div>
<div class="row">
<div class="col-8"><h3>View User Data:-</h3></div>
<div class="col-4 text-right pull-right">
<a href="<?php echo base_url().'index.php/user/create'; ?>" title="" class="btn btn-primary">Create</a>
</div>
</div>
<hr>
<div class="row">
<div class="col-md-8">
<table class="table table-striped">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<tbody>
<?php
if(!empty($users)) {
foreach($users as $user) {
?>
<tr>
<td><?php echo $user['user_id']; ?></td>
<td><?php echo $user['name']; ?></td>
<td><?php echo $user['email']; ?></td>
<td><a href="<?php echo base_url().'index.php/user/edit/'.$user['user_id'] ; ?>" title="Edit" class="btn btn-primary">Edit</a> </td>
<!-- <td><a href="<?php //echo base_url().'index.php/user/delete/'.$user['user_id'] ; ?>" title="Delete" class="btn btn-danger">Delete</a> </td> -->
<td><a href="javascript:void(0);>" onclick="deleteThis(<?php echo $user['user_id']; ?>);" title="Delete" class="btn btn-danger">Delete</a> </td>
</tr>
<?php } } else{ ?>
<tr>
<td colspan="5" >No Data Found!</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
<script type="text/javascript">
var url="<?php echo base_url();?>";
function deleteThis(id){
var r=confirm("Do you want to delete this?")
if (r==true)
window.location = url+"index.php/user/delete/"+id;
else
return false;
}
</script>
</body>
</html>
https://drive.google.com/file/d/1r8BWZzmiInRV5CY4Hjd2hgTxOP-jeG8e/view?usp=sharing
No comments:
Post a Comment