Showing posts with label bootstrap. Show all posts
Showing posts with label bootstrap. Show all posts

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>

Friday, December 3, 2021

run any ajax oj js function when bootstrap modal open

For any bootstrap modal open, these are by default class. These classes are not mention but this is worked.   

bs modal

These are default class.

js code for call when modal is open.

$(document).ready(function () {

$( '#editModel87' ).on('show.bs.modal', function(){

  //alert("I want this to appear after the modal has opened!");

  setTimeout(

    function() {

      var orgsid = $('#editForm87 input[name="orgs_id"]').attr('data-selectedid');

      var ticketId = $('#editForm87 input[name="key_id"]').val();

      getSeverityListEdit(orgsid,ticketId);

    }, 3000); // run after 3 seconds

  });

});