Thursday, January 20, 2022

CodeIgniter CRUD example with 4 fields with bootstrap

C:\xampp\htdocs\newci2\application\config\config.php
$config['base_url'] = 'http://localhost/newci2/';


C:\xampp\htdocs\newci2\application\config\autoload.php
$autoload['libraries'] = array('database', 'session', 'form_validation');
$autoload['helper'] = array('url');


C:\xampp\htdocs\newci2\application\config\database.php
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'crud',

Database code :-

-- phpMyAdmin SQL Dump
-- version 4.9.2
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jan 20, 2022 at 10:13 AM
-- Server version: 10.4.11-MariaDB
-- PHP Version: 7.2.26
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `crud`
--
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `fname` varchar(50) NOT NULL,
  `lname` varchar(50) NOT NULL,
  `username` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `status` tinyint(1) NOT NULL,
  `created_at` datetime NOT NULL,
  `gender` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;


C:\xampp\htdocs\newci2\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->view('create');
    $this->load->model('User_model');
$this->form_validation->set_rules('fname','First name','required');
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('password','Password','required');
if ($this->form_validation->run() == false) {
$this->load->view('create');
}else{
$formArray = array();
$formArray['fname']=$this->input->post('fname');
$formArray['lname']=$this->input->post('lname');
$formArray['username']=$this->input->post('username');
$formArray['password']=$this->input->post('password');
$formArray['gender']=$this->input->post('gender');
$formArray['status']= 1;
$formArray['created_at']=date('Y-m-d H:i:s');
$this->User_model->create($formArray);
$this->session->set_flashdata('success','Record inserted Successfully..');
redirect(base_url().'index.php/user/index');
}
    }
    function edit($id){
    $this->load->model('User_model');
    $user = $this->User_model->get_user($id);
    $data = array();
    $data['user']= $user;
    //$this->load->view('edit',$data);
    $this->form_validation->set_rules('fname','First Name','required');
    $this->form_validation->set_rules('lname','Last Name','required');
    $this->form_validation->set_rules('username','User Name','required');
    $this->form_validation->set_rules('password','password','required');
    $this->form_validation->set_rules('gender','Gender','required');
    if ( $this->form_validation->run() == false ) {
    $this->load->view('edit',$data);
    }else{
    $formArray = array();
$formArray['fname']=$this->input->post('fname');
$formArray['lname']=$this->input->post('lname');
$formArray['username']=$this->input->post('username');
$formArray['password']=$this->input->post('password');
$formArray['gender']=$this->input->post('gender');
$this->User_model->update_user($id,$formArray);
$this->session->set_flashdata('success','Record updated Successfully..');
redirect(base_url().'index.php/user/index');
    }
    }
    function delete($id){
    $this->load->model('User_model');
$user = $this->User_model->get_user($id);
if(empty($user)){
$this->session->set_flashdata('failure','Record Not Found..');
redirect(base_url().'index.php/user/index');
}else{
$this->User_model->deleteUser($id);
$this->session->set_flashdata('success','Record Deleted Successfully..');
redirect(base_url().'index.php/user/index');
}
    }
}
?>



C:\xampp\htdocs\newci2\application\models\User_model.php
<?php
class User_model extends CI_model
{
function create($formArray){
$this->db->insert('users',$formArray);
}
function all(){
return $result = $this->db->get('users')->result_array();
}
function get_user($id){
$this->db->where('id', $id);
return $user = $this->db->get('users')->row_array();
}
function update_user($id,$formArray){
$this->db->where('id',$id);
$this->db->update('users', $formArray);
}
function deleteUser($id){
$this->db->where('id', $id);
$this->db->delete('users'); 
}
}
?>



C:\xampp\htdocs\newci2\application\views\create.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Create New User</title>
<link rel="stylesheet" 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">
<div class="col-md-12">
<form name="createUser" action="<?php echo base_url().'index.php/user/create'; ?>" method="post" >
<div class="form-group">
<label for="fname">First Name : </label>
<input type="text" name="fname" id="fname" value="<?php echo set_value('fname'); ?>" placeholder="Enter First Name Here" class="form-control">
<?php echo form_error('fname'); ?>
</div>
<div class="form-group">
<label for="lname">Last Name : </label>
<input type="text" name="lname" id="lname" value="<?php echo set_value('lname'); ?>" placeholder="Enter Last Name Here" class="form-control">
<?php echo form_error('lname'); ?>
</div>
<div class="form-group">
<label for="username">Username : </label>
<input type="text" name="username" id="username" value="<?php echo set_value('username'); ?>" placeholder="Enter Username Here" class="form-control">
<?php echo form_error('username'); ?>
</div>
<div class="form-group">
<label for="name">Password : </label>
<input type="password" name="password" id="password" value="<?php echo set_value('password'); ?>" placeholder="Enter Password Here" class="form-control">
<?php echo form_error('password'); ?>
</div>
<div class="form-group">
<label>Gender : </label>
<input type="radio" name="gender" id="male" value="Male"> <label for="male">Male</label>
<input type="radio" name="gender" id="female" value="Female"> <label for="female">Female</label>
<?php echo form_error('gender'); ?>
</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>
</form>
</div>
</div>
</div>
</body>
</html>



C:\xampp\htdocs\newci2\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>First Name</th>
<th>Last Name</th>
<th>Username</th>
<th>password</th>
<th>Gender</th>
<th>Action</th>
</tr>
<tbody>
<?php 
if(!empty($users)) { 
foreach($users as $user) {
?>
<tr>
<td><?php echo $user['id']; ?></td>
<td><?php echo $user['fname']; ?></td>
<td><?php echo $user['lname']; ?></td>
<td><?php echo $user['username']; ?></td>
<td><?php echo $user['password']; ?></td>
<td><?php echo $user['gender']; ?></td>
<td><a href="<?php echo base_url().'index.php/user/edit/'.$user['id'] ; ?>" title="Edit" class="btn btn-primary">Edit</a> 
<!-- <td><a href="<?php //echo base_url().'index.php/user/delete/'.$user['id'] ; ?>" title="Delete" class="btn btn-danger">Delete</a> </td> -->
<a href="javascript:void(0);>" onclick="deleteThis( <?php echo $user['id']; ?> );"  title="Delete" class="btn btn-danger">Delete</a> </td>
</tr>
<?php } } else{ ?>
<tr>
<td colspan="7" >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>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript"> 
      $(document).ready( function() {
        $('.alert').delay(4500).fadeOut();
      });
    </script>
</body>
</html>



C:\xampp\htdocs\newci2\application\views\edit.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Edit User</title>
<link rel="stylesheet" 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>Edit 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">
<div class="col-md-12">
<form name="createUser" action="<?php echo base_url().'index.php/user/edit/'.$user['id']; ?>" method="post" >
<div class="form-group">
<label for="fname">First Name : </label>
<input type="text" name="fname" id="fname" value="<?php echo set_value('fname', $user['fname']); ?>" placeholder="Enter First Name Here" class="form-control">
<?php echo form_error('fname'); ?>
</div>
<div class="form-group">
<label for="lname">Last Name : </label>
<input type="text" name="lname" id="lname" value="<?php echo set_value('lname', $user['lname'] ); ?>" placeholder="Enter Last Name Here" class="form-control">
<?php echo form_error('lname'); ?>
</div>
<div class="form-group">
<label for="username">Username : </label>
<input type="text" name="username" id="username" value="<?php echo set_value('username', $user['username']); ?>" placeholder="Enter Username Here" class="form-control">
<?php echo form_error('username'); ?>
</div>
<div class="form-group">
<label for="name">Password : </label>
<input type="password" name="password" id="password" value="<?php echo set_value('password', $user['password']); ?>" placeholder="Enter Password Here" class="form-control">
<?php echo form_error('password'); ?>
</div>
<div class="form-group">
<label>Gender : </label>
<input type="radio" name="gender" id="male" value="Male" <?php if($user['gender']== 'Male') echo "checked"; ?> > <label for="male">Male</label>
<input type="radio" name="gender" id="female" value="Female" <?php if($user['gender']== 'Female') echo "checked"; ?>> <label for="female">Female</label>
<?php echo form_error('gender'); ?>
</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>
</form>
</div>
</div>
</div>
</body>
</html>

Wednesday, January 19, 2022

CodeIgniter CRUD example

 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
);


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');
}
}
}
?>


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 = ?
}
}
?>



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>


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>


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>


https://drive.google.com/file/d/1r8BWZzmiInRV5CY4Hjd2hgTxOP-jeG8e/view?usp=sharing